From 34aa46aa35b728058dc9f927a36ba48c477cf360 Mon Sep 17 00:00:00 2001 From: Brett Chalupa Date: Tue, 21 Oct 2025 05:24:22 -0400 Subject: [PATCH] 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. --- .../fresh/src/middlewares/static_files.ts | 24 ++++++----- .../src/middlewares/static_files_test.ts | 40 ++++++++++++++++++- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/packages/fresh/src/middlewares/static_files.ts b/packages/fresh/src/middlewares/static_files.ts index 8b681976..d295060a 100644 --- a/packages/fresh/src/middlewares/static_files.ts +++ b/packages/fresh/src/middlewares/static_files.ts @@ -68,16 +68,20 @@ export function staticFiles(): Middleware { 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 ( diff --git a/packages/fresh/src/middlewares/static_files_test.ts b/packages/fresh/src/middlewares/static_files_test.ts index b50a8741..b0733c53 100644 --- a/packages/fresh/src/middlewares/static_files_test.ts +++ b/packages/fresh/src/middlewares/static_files_test.ts @@ -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(