-
Notifications
You must be signed in to change notification settings - Fork 0
perf: lazy-load and cache frontend assets #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a1734da
perf: split and cache frontend assets
mira-2026 4add294
fix: recover lazy frontend content
mira-2026 0a2d876
fix: keep route preloads non-disruptive
mira-2026 47953bc
fix(frontend): address final bundle review findings
mira-2026 4aec43f
fix(frontend): harden lazy recovery and artifact paths
mira-2026 65d2078
fix(dev): recover empty preview worktree paths
mira-2026 a4c21a4
fix(dev): ignore release manifests in source mode
mira-2026 a54ad87
fix(release): scope missing manifest fallback
mira-2026 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import type { Stats } from "node:fs"; | ||
| import fs from "node:fs/promises"; | ||
|
|
||
| interface StaticFileResponseOptions { | ||
| cacheControl: string; | ||
| contentType?: string; | ||
| } | ||
|
|
||
| type ContentEncoding = "br" | "gzip"; | ||
|
|
||
| interface StaticRepresentation { | ||
| contentEncoding?: ContentEncoding; | ||
| filePath: string; | ||
| stat: Stats; | ||
| } | ||
|
|
||
| const COMPRESSION_SIDECARS: ReadonlyArray<{ | ||
| contentEncoding: ContentEncoding; | ||
| extension: ".br" | ".gz"; | ||
| }> = [ | ||
| { contentEncoding: "br", extension: ".br" }, | ||
| { contentEncoding: "gzip", extension: ".gz" }, | ||
| ]; | ||
|
|
||
| function encodingQuality(headerValue: string | null, encoding: string): number { | ||
| if (!headerValue) return 0; | ||
|
|
||
| let explicitQuality: number | undefined; | ||
| let wildcardQuality: number | undefined; | ||
| for (const item of headerValue.split(",")) { | ||
| const [rawEncoding = "", ...parameters] = item.split(";"); | ||
| const normalizedEncoding = rawEncoding.trim().toLowerCase(); | ||
| let quality = 1; | ||
| for (const parameter of parameters) { | ||
| const normalizedParameter = parameter.trim().toLowerCase(); | ||
| if (normalizedParameter.startsWith("q")) { | ||
| const match = normalizedParameter.match( | ||
| /^q\s*=\s*(0(?:\.\d{0,3})?|1(?:\.0{0,3})?)$/u | ||
| ); | ||
| quality = match ? Number(match[1]) : 0; | ||
| } | ||
| } | ||
| if (normalizedEncoding === encoding) explicitQuality = quality; | ||
| if (normalizedEncoding === "*") wildcardQuality = quality; | ||
| } | ||
|
|
||
| return explicitQuality ?? wildcardQuality ?? 0; | ||
| } | ||
|
|
||
| async function compressedRepresentations( | ||
| filePath: string | ||
| ): Promise<StaticRepresentation[]> { | ||
| const representations: StaticRepresentation[] = []; | ||
| for (const { contentEncoding, extension } of COMPRESSION_SIDECARS) { | ||
| const sidecarPath = `${filePath}${extension}`; | ||
| try { | ||
| const stat = await fs.lstat(sidecarPath); | ||
| if (stat.isFile()) { | ||
| representations.push({ | ||
| contentEncoding, | ||
| filePath: sidecarPath, | ||
| stat, | ||
| }); | ||
| } | ||
| } catch { | ||
| // This representation was not generated for the source file. | ||
| } | ||
| } | ||
| return representations; | ||
| } | ||
|
|
||
| function preferredRepresentation( | ||
| request: Request, | ||
| source: StaticRepresentation, | ||
| compressed: StaticRepresentation[] | ||
| ): StaticRepresentation { | ||
| return ( | ||
| compressed | ||
| .map((representation, priority) => ({ | ||
| priority, | ||
| quality: encodingQuality( | ||
| request.headers.get("accept-encoding"), | ||
| representation.contentEncoding ?? "" | ||
| ), | ||
| representation, | ||
| })) | ||
| .filter(({ quality }) => quality > 0) | ||
| .toSorted( | ||
| (left, right) => | ||
| right.quality - left.quality || left.priority - right.priority | ||
| )[0]?.representation ?? source | ||
| ); | ||
| } | ||
|
|
||
| function entityTagFor(representation: StaticRepresentation): string { | ||
| const encodingSuffix = representation.contentEncoding | ||
| ? `-${representation.contentEncoding}` | ||
| : ""; | ||
| return `W/"${representation.stat.size.toString(16)}-${Math.trunc( | ||
| representation.stat.mtimeMs | ||
| ).toString(16)}${encodingSuffix}"`; | ||
| } | ||
|
|
||
| function normalizedEntityTag(entityTag: string): string { | ||
| return entityTag.trim().replace(/^W\//iu, ""); | ||
| } | ||
|
|
||
| function isEntityTagMatch(headerValue: string, entityTag: string): boolean { | ||
| const normalizedCurrent = normalizedEntityTag(entityTag); | ||
| return headerValue.split(",").some((candidate) => { | ||
| const trimmed = candidate.trim(); | ||
| return trimmed === "*" || normalizedEntityTag(trimmed) === normalizedCurrent; | ||
| }); | ||
| } | ||
|
|
||
| function isNotModified(request: Request, entityTag: string, modifiedAt: Date): boolean { | ||
| if (!["GET", "HEAD"].includes(request.method.toUpperCase())) return false; | ||
|
|
||
| const ifNoneMatch = request.headers.get("if-none-match"); | ||
| if (ifNoneMatch !== null) { | ||
| return isEntityTagMatch(ifNoneMatch, entityTag); | ||
| } | ||
|
|
||
| const ifModifiedSince = request.headers.get("if-modified-since"); | ||
| if (!ifModifiedSince) return false; | ||
| const conditionalTimestamp = Date.parse(ifModifiedSince); | ||
| return ( | ||
| Number.isFinite(conditionalTimestamp) && | ||
| Math.floor(modifiedAt.getTime() / 1000) <= Math.floor(conditionalTimestamp / 1000) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Serves a verified static file with conditional requests and negotiated | ||
| * precompressed representations. | ||
| */ | ||
| export async function staticFileResponse( | ||
| request: Request, | ||
| filePath: string, | ||
| { cacheControl, contentType }: StaticFileResponseOptions | ||
| ): Promise<Response> { | ||
| const sourceStat = await fs.stat(filePath); | ||
| const source: StaticRepresentation = { filePath, stat: sourceStat }; | ||
| const compressed = await compressedRepresentations(filePath); | ||
| const representation = preferredRepresentation(request, source, compressed); | ||
| const entityTag = entityTagFor(representation); | ||
| const headers = new Headers({ | ||
| "Cache-Control": cacheControl, | ||
| ETag: entityTag, | ||
| "Last-Modified": sourceStat.mtime.toUTCString(), | ||
| }); | ||
|
|
||
| const resolvedContentType = contentType ?? Bun.file(filePath).type; | ||
| if (resolvedContentType) headers.set("Content-Type", resolvedContentType); | ||
| if (compressed.length > 0) headers.set("Vary", "Accept-Encoding"); | ||
| if (representation.contentEncoding) { | ||
| headers.set("Content-Encoding", representation.contentEncoding); | ||
| } | ||
|
|
||
| if (isNotModified(request, entityTag, sourceStat.mtime)) { | ||
| return new Response(undefined, { headers, status: 304 }); | ||
| } | ||
| return new Response(Bun.file(representation.filePath), { headers }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.