fix: add cache-bust query param to Vite asset URLs for immutable caching (#3761)
## Summary
- Vite-built assets (JS chunks, CSS) were served with `Cache-Control:
no-cache, no-store` because the static files middleware only recognized
builder-path assets (under `/_fresh/js/{BUILD_ID}/`) as immutable
- Adds an `immutable` flag to `FileSnapshot`, `StaticFile`, and
`PendingStaticFile` interfaces
- The Vite plugin marks manifest chunks and CSS as `immutable: true`
(these have content-hashed filenames), while public directory files
remain non-immutable
- The static files middleware checks `file.immutable` alongside the
existing path and query param checks
Closes #3282
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
f39a198f8d
commit
14f895517e
@@ -11,6 +11,7 @@ export interface FileSnapshot {
|
||||
filePath: string;
|
||||
hash: string | null;
|
||||
contentType: string;
|
||||
immutable?: boolean;
|
||||
}
|
||||
|
||||
export interface BuildSnapshot<State> {
|
||||
@@ -28,6 +29,7 @@ export interface StaticFile {
|
||||
contentType: string;
|
||||
readable: ReadableStream<Uint8Array> | Uint8Array;
|
||||
close(): void;
|
||||
immutable?: boolean;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
@@ -87,6 +89,7 @@ export class ProdBuildCache<State> implements BuildCache<State> {
|
||||
size: stat.size,
|
||||
readable: file.readable,
|
||||
close: () => file.close(),
|
||||
immutable: info.immutable,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -438,6 +438,7 @@ export interface PendingStaticFile {
|
||||
pathname: string;
|
||||
filePath: string;
|
||||
hash: string | null;
|
||||
immutable?: boolean;
|
||||
}
|
||||
|
||||
export async function writeCompiledEntry(outDir: string) {
|
||||
@@ -561,7 +562,13 @@ export async function prepareStaticFile(
|
||||
item: PendingStaticFile,
|
||||
outDir: string,
|
||||
): Promise<
|
||||
{ name: string; hash: string; filePath: string; contentType: string }
|
||||
{
|
||||
name: string;
|
||||
hash: string;
|
||||
filePath: string;
|
||||
contentType: string;
|
||||
immutable?: boolean;
|
||||
}
|
||||
> {
|
||||
const file = await Deno.open(item.filePath);
|
||||
const hash = item.hash ? item.hash : await hashContent(file.readable);
|
||||
@@ -576,6 +583,7 @@ export async function prepareStaticFile(
|
||||
: item.filePath,
|
||||
),
|
||||
contentType: getContentType(item.filePath),
|
||||
immutable: item.immutable,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,8 @@ export function staticFiles<T>(): Middleware<T> {
|
||||
(BUILD_ID === cacheKey ||
|
||||
url.pathname.startsWith(
|
||||
`${ctx.config.basePath}/_fresh/js/${BUILD_ID}/`,
|
||||
))
|
||||
) ||
|
||||
file.immutable)
|
||||
) {
|
||||
span.setAttribute("fresh.cache", "immutable");
|
||||
headers.append("Cache-Control", "public, max-age=31536000, immutable");
|
||||
|
||||
Reference in New Issue
Block a user