fix: improved etag handling in static files (#198)
This commit is contained in:
+22
-17
@@ -499,26 +499,31 @@ export class ServerContext {
|
||||
const url = new URL(req.url);
|
||||
const key = url.searchParams.get(ASSET_CACHE_BUST_KEY);
|
||||
if (key !== null && BUILD_ID !== key) {
|
||||
return new Response("Not Found", {
|
||||
status: 404,
|
||||
headers: { "content-type": "text/plain" },
|
||||
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
|
||||
const location = url.pathname + url.search;
|
||||
return new Response("", {
|
||||
status: 307,
|
||||
headers: {
|
||||
"content-type": "text/plain",
|
||||
location,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (req.headers.get("if-none-match") === etag) {
|
||||
return new Response(null, { status: 304 });
|
||||
const headers = new Headers({
|
||||
"content-type": contentType,
|
||||
etag,
|
||||
vary: "If-None-Match",
|
||||
});
|
||||
if (key !== null) {
|
||||
headers.set("Cache-Control", "public, max-age=31536000, immutable");
|
||||
}
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (ifNoneMatch === etag || ifNoneMatch === "W/" + etag) {
|
||||
return new Response(null, { status: 304, headers });
|
||||
} else {
|
||||
const resp = await fetch(localUrl);
|
||||
const headers = new Headers({
|
||||
"content-type": contentType,
|
||||
"content-length": String(size),
|
||||
"etag": etag,
|
||||
});
|
||||
if (key !== null) {
|
||||
headers.set("Cache-Control", "public, max-age=31536000, immutable");
|
||||
}
|
||||
return new Response(resp.body, {
|
||||
headers,
|
||||
});
|
||||
const file = await Deno.open(localUrl);
|
||||
headers.set("content-length", String(size));
|
||||
return new Response(file.readable, { headers });
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+20
-2
@@ -187,16 +187,34 @@ Deno.test("static file - by file path", async () => {
|
||||
assertEquals(resp.status, 200);
|
||||
const body = await resp.text();
|
||||
assert(body.startsWith("bar"));
|
||||
assert(resp.headers.get("etag"));
|
||||
const etag = resp.headers.get("etag");
|
||||
assert(etag);
|
||||
// The etag is not weak, because this did not go through content encoding, so
|
||||
// this is not a real server.
|
||||
assert(!etag.startsWith("W/"), "etag should be weak");
|
||||
assertEquals(resp.headers.get("content-type"), "text/plain");
|
||||
|
||||
const resp2 = await router(
|
||||
new Request("https://fresh.deno.dev/foo.txt", {
|
||||
headers: {
|
||||
"if-none-match": resp.headers.get("etag")!,
|
||||
"if-none-match": etag,
|
||||
},
|
||||
}),
|
||||
);
|
||||
assertEquals(resp2.status, 304);
|
||||
assertEquals(resp2.headers.get("etag"), etag);
|
||||
assertEquals(resp2.headers.get("content-type"), "text/plain");
|
||||
|
||||
const resp3 = await router(
|
||||
new Request("https://fresh.deno.dev/foo.txt", {
|
||||
headers: {
|
||||
"if-none-match": `W/${etag}`,
|
||||
},
|
||||
}),
|
||||
);
|
||||
assertEquals(resp3.status, 304);
|
||||
assertEquals(resp3.headers.get("etag"), etag);
|
||||
assertEquals(resp3.headers.get("content-type"), "text/plain");
|
||||
});
|
||||
|
||||
Deno.test("static file - by 'hashed' path", async () => {
|
||||
|
||||
Reference in New Issue
Block a user