In the basics you saw caches: copies kept close to you. You also saw that each copy has a lifetime, and that once a copy is stale the browser asks the server first. Here you read the headers that control all of this: Cache-Control with all its choices. Then you see how to ask the server "is it still the same?" without downloading again.
Fresh first, then stale
This is the server's reply to the request GET /api/user:
HTTP/1.1 200 OK
Cache-Control: max-age=60
ETag: "v3"
{ "user": "Alya" }
200 OKis the status: it worked, and the content comes along.Cache-Control: max-age=60means this copy may be used right away for 60 seconds. During that time the copy is called fresh: the browser uses the copy in its cache and doesn't contact the server at all.ETag: "v3"is a version label for the content, a kind of fingerprint. When the content changes, the server gives it a new ETag.{ "user": "Alya" }is the body, the content of the reply.
After 60 seconds the copy becomes stale. The browser doesn't throw the copy away. Before using it again, the browser asks the server first. Asking again like this is called revalidation.
Revalidation: ETag and 304
To revalidate, the browser sends an If-None-Match header (it means "if none of these match") with the ETag it stored:
GET /api/user HTTP/1.1
If-None-Match: "v3"
The server compares that ETag with the version it has now:
- Still
"v3": the server replies304 Not Modifiedwith no body. It means the content hasn't changed. The browser uses its old copy, and that copy is fresh again formax-age. - Changed, say to
"v4": the server replies200 OKwith the new body and the new ETag. The browser replaces its copy.
If-None-Match may hold several ETags separated by commas, such as "v1", "v3". If one of them matches, the server replies 304. When the header is missing, there is nothing to match, so the server replies 200 with the full content.
Comparing ETags in code
On the server, the If-None-Match header is just text. This code first turns it into an array (a list) of ETags without quote marks, then looks for v3 in that array:
const raw = 'v1, "v3"';
const etags = raw.split(",").map((e) => e.trim().replace(/^W\//, "").replace(/"/g, ""));
// etags: ["v1", "v3"]
etags.includes("v3"); // true, so reply 304
split(",")cuts the text at every comma. Thenmapcleans each pieceewith the steps below.trim()removes spaces at the start and end.replace(/^W\//, "")removes theW/in front of a weak ETag (explained below), thenreplace(/"/g, "")removes the quote marks. SoW/"v3","v3"andv3count as the same.includes("v3")checks whetherv3is in the array.
When the header is missing, raw holds undefined, and undefined.split throws an error. So check first with raw ? ... : []: if raw exists, cut the text, and if not, use an empty array.
no-cache, no-store, immutable
Besides max-age, Cache-Control has other choices:
| Header | What it means |
|---|---|
no-cache |
May be stored, but has to be revalidated every time before use |
no-store |
Don't store it at all |
public / private |
public: shared caches (used by many users) such as a CDN may store it too. private: only that user's browser |
immutable |
The content will never change, so no revalidation while it is fresh |
Look closely at no-cache: its name is easy to misread. With no-cache, the copy is still stored, but the browser always asks the server first before using it. Use no-cache for HTML that changes often, and no-store for secret data such as a bank balance page.
Some files have a hash in their name (a short code calculated from the file's content), such as app.a1b2c3.js built by Vite (a tool that packs website files). So one hashed file name always holds the same content: when the content changes, the hash in the name changes too. The best header for hashed files:
Cache-Control: public, max-age=31536000, immutable
31536000 seconds is one year.
⚠️ Old versions, leaked data and weak ETags
- A long
max-ageon HTML. Users are stuck on the old version until the copy's lifetime has passed. For HTML, useno-cache. Hashed files can be kept for a year. - Personal data without
private. A CDN could store one user's profile and give it to another user. - Weak ETags.
W/"v3"(withW/in front) means "equivalent content" (the same meaning), not "the very same bytes". ForIf-None-Match,W/"v3"and"v3"still count as a match.
💡 The older way:
Last-Modified. Besides an ETag, a server can send the date the content last changed. The browser then asks withIf-Modified-Since, and the server replies 304 if nothing has changed since that date. ETags are more precise, because they compare versions, not clock times.
TL;DR: max-age=N in the Cache-Control header keeps a copy fresh (usable right away) for N seconds, and during that time the browser doesn't contact the server. After that the copy is stale, and the browser revalidates (asks again) by sending If-None-Match with its copy's ETag (the content's version label). When that ETag matches the server's version, the server replies 304 Not Modified with no body. When it differs, or If-None-Match is missing, the server replies 200 with the full content. no-cache means store but always ask first, and no-store means don't store. Hashed files (their name holds a code made from their content) get public, max-age=31536000, immutable: they can be kept for a year, on a CDN too, with no revalidation.
⚡ Fresh first
Write:
Cache-Control: max-age=60
ETag: "v3"
Guess first: the server sends these two headers with its reply, and the browser keeps a copy in its cache. During the first 60 seconds, does the browser contact the server?
No. During that time the copy is fresh, so the browser uses the copy in its cache right away.
Meaning: max-age is the copy's lifetime, counted in seconds: during that time the copy may be used right away. ETag is a label for the content's version. After 60 seconds the copy is stale, and the browser asks the server first before using it again.
⚡ Revalidation and 304
Write:
GET /api/user HTTP/1.1
If-None-Match: "v3"
Meaning: This is revalidation (asking again): the browser sends its copy's ETag in the If-None-Match header. If the version is still "v3", the server replies 304 Not Modified with no body, and the browser uses its old copy. If it has changed, the server replies 200 OK with a new body and a new ETag.
⚡ no-cache is not no-store
Guess first: no-cache is one of the choices in Cache-Control. Does it mean don't keep it?
No. no-cache still keeps it, but always revalidates first. The one that keeps nothing at all is no-store.
Meaning: HTML that changes often uses no-cache, and secret data uses no-store. Hashed files (their name holds a code made from their content) like app.a1b2c3.js can use public, max-age=31536000, immutable: kept for a year, on a CDN too, with no revalidation.