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:
@@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user