fix: serve internal JS from path containing build ID (#2753)

Previously imported chunks were not cached at all. They are now cached
properly with immutable cache headers.
This commit is contained in:
Luca Casonato
2024-11-08 11:44:15 +01:00
committed by GitHub
parent 96e520a352
commit 6b685dde9f
6 changed files with 42 additions and 27 deletions
+20 -16
View File
@@ -61,26 +61,30 @@ export function staticFiles<T>(): MiddlewareFn<T> {
vary: "If-None-Match",
});
if (cacheKey === null || ctx.config.mode === "development") {
const ifNoneMatch = req.headers.get("If-None-Match");
if (
ifNoneMatch !== null &&
(ifNoneMatch === etag || ifNoneMatch === `W/"${etag}"`)
) {
file.close();
return new Response(null, { status: 304, headers });
} else if (etag !== null) {
headers.set("Etag", `W/"${etag}"`);
}
if (
ctx.config.mode !== "development" &&
(BUILD_ID === cacheKey ||
url.pathname.startsWith(
`${ctx.config.basePath}/_fresh/js/${BUILD_ID}/`,
))
) {
headers.append("Cache-Control", "public, max-age=31536000, immutable");
} else {
headers.append(
"Cache-Control",
"no-cache, no-store, max-age=0, must-revalidate",
);
} else {
const ifNoneMatch = req.headers.get("If-None-Match");
if (
ifNoneMatch !== null &&
(ifNoneMatch === etag || ifNoneMatch === `W/"${etag}"`)
) {
file.close();
return new Response(null, { status: 304, headers });
} else if (etag !== null) {
headers.set("Etag", `W/"${etag}"`);
}
}
if (BUILD_ID === cacheKey) {
headers.append("Cache-Control", "public, max-age=31536000, immutable");
}
headers.set("Content-Length", String(file.size));