Skip to content

Commit

Permalink
fix: etag caching HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Jun 25, 2024
1 parent 4983907 commit d292dc2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/middlewares/static_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ 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}"`)
) {
return new Response(null, { status: 304, headers });
} else if (etag !== null) {
headers.set("Etag", `W/"${etag}"`);
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/middlewares/static_files_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,22 @@ Deno.test("static files - etag", async () => {
{ buildCache },
);

const headers = new Headers();
headers.append("If-None-Match", "123");
const cacheUrl = `/foo.css?${ASSET_CACHE_BUST_KEY}=${BUILD_ID}`;
const res = await server.get(cacheUrl, { headers });
let res = await server.get(cacheUrl);
await res.body?.cancel();
expect(res.headers.get("Etag")).toEqual('W/"123"');

let headers = new Headers();
headers.append("If-None-Match", "123");
res = await server.get(cacheUrl, { headers });
await res.body?.cancel();
expect(res.status).toEqual(304);

const headers2 = new Headers();
headers2.append("If-None-Match", "W/123");
const res2 = await server.get(cacheUrl, { headers });
await res2.body?.cancel();
expect(res2.status).toEqual(304);
headers = new Headers();
headers.append("If-None-Match", 'W/"123"');
res = await server.get(cacheUrl, { headers });
await res.body?.cancel();
expect(res.status).toEqual(304);
});

Deno.test("static files - 404 on missing favicon.ico", async () => {
Expand Down

0 comments on commit d292dc2

Please sign in to comment.