fix: disable static file caching in development (#3561)

When running Fresh 2 in development, the ETag caching was leading to
incorrect assets being served. I'd often get stale assets from days ago
or separate apps. This problem was especially bad with multiple apps
that run on the same port. For example, I may work on App A on the
default port 5173. Then boot up App B. I'd get assets from App A when I
wouldn't want them, leading to a frustrating dev experience.
This commit is contained in:
Brett Chalupa
2025-10-21 11:24:22 +02:00
committed by GitHub
parent 8b7caba444
commit 34aa46aa35
2 changed files with 52 additions and 12 deletions
+14 -10
View File
@@ -68,16 +68,20 @@ export function staticFiles<T>(): Middleware<T> {
vary: "If-None-Match",
});
const ifNoneMatch = req.headers.get("If-None-Match");
if (
ifNoneMatch !== null &&
(ifNoneMatch === etag || ifNoneMatch === `W/"${etag}"`)
) {
file.close();
span.setAttribute("fresh.cache", "not_modified");
return new Response(null, { status: 304, headers });
} else if (etag !== null) {
headers.set("Etag", `W/"${etag}"`);
// In development mode, skip ETag validation to prevent serving stale
// cached assets when switching between different apps on the same port
if (ctx.config.mode !== "development") {
const ifNoneMatch = req.headers.get("If-None-Match");
if (
ifNoneMatch !== null &&
(ifNoneMatch === etag || ifNoneMatch === `W/"${etag}"`)
) {
file.close();
span.setAttribute("fresh.cache", "not_modified");
return new Response(null, { status: 304, headers });
} else if (etag !== null) {
headers.set("Etag", `W/"${etag}"`);
}
}
if (
@@ -84,13 +84,20 @@ Deno.test("static files - HEAD 200", async () => {
expect(content).toEqual("");
});
Deno.test("static files - etag", async () => {
Deno.test("static files - etag in production", async () => {
const buildCache = new MockBuildCache({
"foo.css": { content: "body {}", hash: "123" },
});
const server = serveMiddleware(
staticFiles(),
{ buildCache },
{
buildCache,
config: {
root: "",
basePath: "",
mode: "production",
},
},
);
const cacheUrl = `/foo.css?${ASSET_CACHE_BUST_KEY}=${BUILD_ID}`;
@@ -111,6 +118,35 @@ Deno.test("static files - etag", async () => {
expect(res.status).toEqual(304);
});
Deno.test("static files - no etag in development", async () => {
const buildCache = new MockBuildCache({
"foo.css": { content: "body {}", hash: "123" },
});
const server = serveMiddleware(
staticFiles(),
{
buildCache,
config: {
root: "",
basePath: "",
mode: "development",
},
},
);
const cacheUrl = `/foo.css`;
let res = await server.get(cacheUrl);
await res.body?.cancel();
expect(res.headers.get("Etag")).toBeNull();
// Even if client sends If-None-Match in dev mode, always return 200
const headers = new Headers();
headers.append("If-None-Match", "123");
res = await server.get(cacheUrl, { headers });
await res.body?.cancel();
expect(res.status).toEqual(200);
});
Deno.test("static files - 404 on missing favicon.ico", async () => {
const buildCache = new MockBuildCache({});
const server = serveMiddleware(