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 url = new URL(req.url);
|
||||||
const key = url.searchParams.get(ASSET_CACHE_BUST_KEY);
|
const key = url.searchParams.get(ASSET_CACHE_BUST_KEY);
|
||||||
if (key !== null && BUILD_ID !== key) {
|
if (key !== null && BUILD_ID !== key) {
|
||||||
return new Response("Not Found", {
|
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
|
||||||
status: 404,
|
const location = url.pathname + url.search;
|
||||||
headers: { "content-type": "text/plain" },
|
return new Response("", {
|
||||||
|
status: 307,
|
||||||
|
headers: {
|
||||||
|
"content-type": "text/plain",
|
||||||
|
location,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (req.headers.get("if-none-match") === etag) {
|
const headers = new Headers({
|
||||||
return new Response(null, { status: 304 });
|
"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 {
|
} else {
|
||||||
const resp = await fetch(localUrl);
|
const file = await Deno.open(localUrl);
|
||||||
const headers = new Headers({
|
headers.set("content-length", String(size));
|
||||||
"content-type": contentType,
|
return new Response(file.readable, { headers });
|
||||||
"content-length": String(size),
|
|
||||||
"etag": etag,
|
|
||||||
});
|
|
||||||
if (key !== null) {
|
|
||||||
headers.set("Cache-Control", "public, max-age=31536000, immutable");
|
|
||||||
}
|
|
||||||
return new Response(resp.body, {
|
|
||||||
headers,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-2
@@ -187,16 +187,34 @@ Deno.test("static file - by file path", async () => {
|
|||||||
assertEquals(resp.status, 200);
|
assertEquals(resp.status, 200);
|
||||||
const body = await resp.text();
|
const body = await resp.text();
|
||||||
assert(body.startsWith("bar"));
|
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(
|
const resp2 = await router(
|
||||||
new Request("https://fresh.deno.dev/foo.txt", {
|
new Request("https://fresh.deno.dev/foo.txt", {
|
||||||
headers: {
|
headers: {
|
||||||
"if-none-match": resp.headers.get("etag")!,
|
"if-none-match": etag,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
assertEquals(resp2.status, 304);
|
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 () => {
|
Deno.test("static file - by 'hashed' path", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user