fix: etag caching HTTP headers (#2504)

This commit is contained in:
Marvin Hagemeister
2024-06-25 10:21:43 +02:00
committed by GitHub
parent 0f6b4f624f
commit 72811da29b
4 changed files with 25 additions and 10 deletions
+8 -2
View File
@@ -35,6 +35,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
}
if (req.method !== "GET" && req.method !== "HEAD") {
file.close();
return new Response("Method Not Allowed", { status: 405 });
}
@@ -42,6 +43,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
if (cacheKey !== null && BUILD_ID !== cacheKey) {
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
const location = url.pathname + url.search;
file.close();
return new Response(null, {
status: 307,
headers: {
@@ -67,15 +69,19 @@ export function staticFiles<T>(): MiddlewareFn<T> {
} else {
const ifNoneMatch = req.headers.get("If-None-Match");
if (
etag !== null &&
(ifNoneMatch === etag || ifNoneMatch === "W/" + etag)
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}"`);
}
}
headers.set("Content-Length", String(file.size));
if (req.method === "HEAD") {
file.close();
return new Response(null, { status: 200, headers });
}