Browser cache: Cache-Control and ETag
How the browser decides to reuse a resource: Cache-Control, max-age, no-cache vs no-store, ETag and 304. The cache strategy that ends the stale-asset bug.
You fix a bug, ship the deploy, open the site, and it's perfect. Then the client says they still see the old version, error and all. You tell them to hit Ctrl+Shift+R and the problem vanishes — but you can't ask every visitor for a hard refresh. That scenario, the classic "I updated it and the user still sees the old one," is almost never a code bug: it's misconfigured cache. The browser is doing exactly what you told it to do — you just told it the wrong thing.
This post explains how the browser decides to reuse a resource instead of downloading it again, what each Cache-Control directive actually does, and the strategy that solves the invalidation problem for good.
How the browser decides to reuse a resource
When the browser needs a file (a JS, an image, the HTML), it first checks what it has stored locally. The decision branches three ways:
- Fresh cache — the local copy is still considered valid. The browser serves straight from disk, zero requests hit the network. It's the fastest path that exists: no round trip to the server.
- Stale cache — the copy expired, but the browser has a way to ask "is it still good?" before downloading everything again. That's revalidation.
- No cache — there's no copy, or the policy forbids reuse. Download from scratch.
The server controls which path gets taken through a response header: Cache-Control. It defines how long the copy stays fresh and what to do once it isn't.
Cache-Control: what each directive really does
Cache-Control is a comma-separated list of directives. The ones that matter day to day:
Cache-Control: public, max-age=31536000, immutable
max-age=N— sets the freshness window in seconds.max-age=31536000is 365 days. During that window, the browser serves directly, never touching the server.publicvsprivate—publiclets intermediate caches (CDN, proxy) store the resource.privaterestricts it to the user's browser cache; use it for personalized content, like a page with logged-in account data.immutable— promises the content at this URL will never change while it's fresh. This stops the browser from revalidating even when the user hits F5. Withoutimmutable, a normal reload triggers a conditional revalidation; with it, the browser doesn't even ask.
no-cache vs no-store — the classic confusion
These two names mislead people. no-cache does not mean "don't cache." It means "you may store it, but always revalidate before serving." The browser keeps the copy and, on every use, asks the server whether it's still valid — saving the body download when nothing changed.
no-store is the real "don't store anything." No copy anywhere. It's for sensitive data (tokens, banking responses), not for asset versioning. If you want the HTML to always revalidate but still benefit from the cache when nothing changed, what you want is no-cache, not no-store.
Fresh cache vs revalidation
Worth hammering on the difference, because that's where the performance win lives.
With a fresh cache (inside max-age), the browser doesn't talk to the server. Zero network latency for that resource. It's ideal for anything that doesn't change: libraries, fonts, versioned images.
With revalidation, the copy expired but the browser doesn't throw it away. It makes a conditional request asking "did this change?". If it didn't, the server responds without resending the body — you pay a short network round trip instead of the full download. It's a middle ground: slower than a fresh cache, much faster than downloading everything.
Revalidation is the right mechanism for HTML, which changes on every deploy but which you don't want to re-download on every visit when nothing changed.
ETag and Last-Modified: how the server answers 304
Revalidation needs a way for the server to say "didn't change" without resending the file. There are two mechanisms.
ETag is an opaque identifier of the content — a fingerprint, usually a hash of the response body:
ETag: "abc123"
The next time the browser revalidates, it sends that value back in the If-None-Match header:
If-None-Match: "abc123"
The server compares the ETag it has now against what the browser sent. If they match, it responds:
304 Not Modified
A 304 is a response with no body. The headers go out, the content doesn't. The browser reads it as "use the copy you already have" and the user pays no download. That 304 Not Modified is the heart of revalidation — when debugging cache behavior, looking up what HTTP status codes mean helps you tell a 304 (revalidated, great) from a 200 that re-downloaded everything.
Last-Modified is the older, date-based mechanism. The server sends Last-Modified: <date>, the browser returns If-Modified-Since: <date>, and if the file hasn't been modified since then you get the same 304. ETag is more precise (it detects any byte change, not just a timestamp), but the two coexist and the browser uses whichever is available.
The winning strategy: fingerprint + long max-age + immutable
Here's the configuration that solves the invalidation problem for good, and it's what every modern bundler (Vite, webpack, esbuild) already does by default.
For assets (JS, CSS, images), generate the file with a hash in the name:
app.abc123.js
The abc123 is a fingerprint of the content. If one line of code changes, the hash changes, and the filename changes — it becomes app.def456.js. Then you serve those assets with:
Cache-Control: public, max-age=31536000, immutable
Aggressive cache, a full year, never revalidates. And that's fine precisely because the name carries the version: the browser never needs to ask whether app.abc123.js changed, because by definition it never does. When the content changes, it's a different file, with a different URL, which the browser downloads for the first time.
The invalidation problem disappears because you never invalidate anything — you publish a new name. That's why the hash goes in the filename, not in some magic query string.
For the HTML, the rule inverts:
Cache-Control: no-cache
The HTML is the entry point that points to the versioned assets. It needs to always revalidate, because it's the one that tells the browser "now load app.def456.js instead of app.abc123.js." Combined with an ETag, on most visits the HTML comes back as 304 — cheap — but on the next deploy it picks up the new content immediately, and along with it the references to the new assets.
That pairing — immutable assets with a hash in the name, always-revalidated HTML — is also one of the strongest levers for perceived performance, a topic I dig into in reducing initial load time.
Opinion: cache assets aggressively, never the HTML
My rule, no middle ground: cache versioned assets as aggressively as possible (one-year max-age, immutable) and never treat the HTML as immutable. Almost every "I updated it and the user still sees the old one" bug falls into one of two traps: either the HTML was cached with a long max-age (so the browser serves the old page pointing to the old assets), or the assets have no hash in the name (so the browser reuses the old app.js even after the deploy).
The fix isn't to turn off the cache — turning off caching is throwing away performance to solve a configuration problem. The fix is to version the assets and revalidate the HTML. When that's right, you never ask anyone for a hard refresh again.
Frequently asked questions
What's the difference between no-cache and no-store?
no-cache lets you store the copy but forces revalidation with the server before each use — when nothing changed, you get a 304 and skip the download. no-store forbids storing any copy, anywhere; every request downloads everything again. Use no-cache for HTML and content that changes but benefits from revalidation; reserve no-store for sensitive data that can't sit on disk.
Why does the hash go in the filename and not in a query string?
Because the filename is the most reliable form of cache identity across the whole chain (browser, CDN, proxy). Some intermediate caches ignore or inconsistently handle resources that differ only by query string, so app.js?v=abc123 may not truly invalidate. Changing the filename (app.abc123.js) is unambiguous: it's a new resource, with a new URL, with no ambiguity for anyone in the chain.
Does immutable work in all browsers?
immutable is a hint: browsers that understand it skip revalidation even on a normal reload, avoiding a round of conditional requests when the user hits F5. Browsers that don't understand it simply ignore the directive and fall back to the default max-age behavior — so nothing breaks. Combined with a hash in the name, the gain is real and the risk is zero.
How do I know if revalidation is working?
Open the DevTools Network panel and look at the status and size of each resource. A fresh resource shows as served from cache (no network trip); a successfully revalidated one shows as 304 Not Modified with a zero-size body; one that re-downloaded shows as 200 with the full size. Seeing lots of 200s where there should be 304s signals a missing ETag or a misconfigured Cache-Control.
The practical takeaway
The browser reuses a resource when Cache-Control says it's still fresh, and revalidates with ETag/If-None-Match when it expired, receiving a 304 Not Modified if nothing changed. The strategy that eliminates cache bugs is simple: assets with a hash in the name served with public, max-age=31536000, immutable, and HTML with no-cache to always revalidate. Aggressive cache where the version is in the name, revalidation where the content points to the rest. Do that, and "I updated it and the user sees the old one" stops existing.
- 01 CPU Cores vs. Threads: What Actually Changes in Practice Understand the difference between CPU cores and threads and what actually changes for performance, multitasking, gaming, and development.
- 02 MIT, Apache, and GPL Licenses: Practical Differences for Developers MIT, Apache 2.0, and GPL are not the same thing. Understand permissive vs copyleft, patent protection, and compatibility before shipping code to production.