Skip to content

.tip

ยท one of the small things

ETag plus If-None-Match turns a repeat fetch into a 304

A client that already has the response shouldn't download it again. Send a hash of the body as an ETag, the client sends it back, and when nothing has changed you answer 304 with no body.

const etag = `"${hash(body)}"`;
if (req.headers.get("if-none-match") === etag) return new Response(null, { status: 304 });
return new Response(body, { headers: { ETag: etag, "Cache-Control": "no-cache" } });

no-cache means "always revalidate", and that's exactly what makes the 304 path fire.

httpcaching