
HTTP caching headers are a few lines on every response that tell browsers and CDNs what to keep and for how long. They decide whether your users get today's code or yesterday's. Get them wrong and a shipped fix can stay invisible to the people who need it.
This guide follows one file, app.js, from your server to the screen. You'll see how max-age, ETag, the 304 response and immutable work together, why a long max-age on an unchanging filename traps users on old code, and the simple two-rule pattern that makes every deploy reach every user.
- Cache-Control max-age sets how many seconds a saved copy counts as fresh.
- Once a copy is stale, ETag or Last-Modified lets the browser ask whether it changed.
- If nothing changed, the server answers 304 Not Modified, with headers and no body.
- Browsers, proxies and CDNs all follow the same headers, so mistakes spread across every layer.
- Fingerprint asset filenames and cache them immutable. Serve HTML with no-cache.
- 0:00Intro
- 0:21Fix shipped. Users still see bugs.
- 1:01Headers that say keep this, this long
- 1:45Every skipped request is a win
- 2:25max-age sets the freshness clock
- 3:09Expired? Ask: did this change?
- 3:59304: same file, almost no bytes
- 4:40Every cache on the path obeys
- 5:14The app.js that wouldn't update
- 5:55Fingerprint files, no-cache the HTML
- 6:34Stale files, solved in six lines
- 7:03Rename the file, not the cache
Why do users still see the bug after I deploy a fix?
Your deploy went green. The fix is live on the server. And the bug reports keep coming anyway. Both things are true at once, and nothing is wrong with your code. The problem is a copy of your code, sitting somewhere you don't control.
That copy lives in the user's browser. It saved yesterday's file and now serves it straight from disk, without ever asking your server. The server has the fix. The browser never asked for it.
The stakes are real. Support tickets pile up. Reviews turn angry. Your team becomes convinced the hotfix failed. And all of it traces back to a few response headers.
- Deploy: green
- Bug report: still there
- Cause: an old saved copy served from disk
What are HTTP caching headers?
Caching headers are metadata on an HTTP response. Every response can carry instructions about itself, and every cache between your server and the screen reads them and follows them to the letter. That's the whole definition.
Follow one file. You open a page. The browser cache checks for a saved copy first. On a miss, the request goes to the CDN edge. Only after that does it reach the origin server. Each stop is a chance to answer without going further.
Two decisions drive everything. First, what to keep: a logo is safe to reuse, a bank balance should never be stored. Second, how long: a cache trusts its copy only until the header says it has expired. After that, it has to check back.
Why caching exists: every skipped request is a win
The fastest request is the one you never send. Every time a cache answers instead of your server, three things improve at once: pages load faster because the file comes from nearby or from disk, servers get lighter because they handle less traffic, and bills shrink because less bandwidth leaves your origin.
The payoff scales for free. One header on a popular file can stop a flood of repeat requests from huge numbers of users, with no new hardware and no code change.
But there's a trade. Caching swaps a little freshness for a lot of speed. Cache too eagerly and users see old content, which is exactly the bug we started with.
- Faster pages
- Lighter servers
- Smaller bills
- Risk: stale content if you cache too eagerly
How does Cache-Control max-age work?
Step one is freshness. The Cache-Control max-age directive decides how long a saved copy counts as good, and it's the one most teams get wrong. It isn't minutes. It isn't a date. It's seconds, counted from when the response was sent.
In the response below, max-age=3600 means the copy stays fresh for one hour. During that hour, the browser reuses it instantly: no network round trip, not even a quick check. Fresh means free.
Then it goes stale. Stale doesn't mean deleted. The copy stays on disk, but the browser now has to ask whether it's still good before using it.
HTTP/1.1 200 OK
Cache-Control: max-age=3600
# fresh for 3600 seconds
# no request until it expires
What happens when a cached file expires? ETag explained
When the clock runs out, the browser doesn't have to download everything again. It can revalidate: check the copy it already has against the server, using a token the server handed over the first time.
Here's the flow. On the first visit, the browser sends GET /app.js. The server replies 200 with ETag v7, along with the file. Later, after max-age expires, the browser sends If-None-Match: v7. In plain words: I have v7, is it still current?
An ETag is a fingerprint of the content itself. Change one byte, and a well-behaved server produces a different ETag. Last-Modified is the older backup, a timestamp. The browser sends it back as If-Modified-Since and asks whether anything changed after that moment.
- ETag: a fingerprint of the content, sent back as If-None-Match
- Last-Modified: a timestamp, sent back as If-Modified-Since
What does 304 Not Modified mean?
Here's the payoff of revalidation. If nothing changed, the server doesn't resend the file at all. The browser sends If-None-Match: v7. The server compares and finds v7 matches v7. It replies 304 Not Modified. The browser reuses app.js from its cache, and the freshness clock resets.
A 304 carries headers only, with no body, so the response is tiny and comes back almost instantly. Not a single byte of the file crosses the wire.
It's still a round trip, though. Cheap, but the browser waited on the network. That's why max-age matters too: a fresh copy skips even this check.
Which caches read your headers? Browsers, proxies and CDNs
Your response doesn't travel straight to the user. It passes through caches you may not even know about. Set a header once on the origin, and every layer along the path makes its own decision from it.
The browser, such as Chrome or Safari, keeps its own private copy. A proxy, like a corporate network gateway, may share one copy across many users. A CDN, such as Cloudflare or Fastly, stores files at edges near users. The origin server, say Nginx, only hears about a request on a miss.
That's the catch: many caches, one header. A mistake isn't stuck in one place. A bad max-age can be copied at every layer.
The classic bug: the app.js that wouldn't update
Now the stale file from the start makes sense. Two decisions that each look reasonable on their own combine into the bug. First, the file keeps the same name every release: app.js. Second, it's served with max-age=31536000. That's one year of freshness.
You deploy a new app.js. The origin has the fix. But the browser cache still holds the old app.js, considers it fresh for a year, and never asks.
Here's the key rule: caches store files by URL. A new file at an old URL looks exactly like the one the browser already trusts. Same name, same copy. Your fix is invisible.
How to fix stale files: fingerprint assets, no-cache HTML
Stop fighting the cache. Change the filename instead. Split your files into two groups with opposite rules. Assets get fingerprinted names, a new one per build, like app.3f9a2c.js. Because that name will never point to different content, you can cache it for a year with immutable.
HTML gets no-cache. That does not mean don't cache. It means store it, but revalidate every time. So the browser always checks for the latest index.html, which links to the latest fingerprinted files.
A new deploy means new HTML and a new filename. The browser has never seen that URL, so it fetches it. The update is instant.
- Unversioned app.js plus long max-age: users stuck until it expires
- Fingerprinted app.3f9a2c.js: cache forever with immutable
- index.html with no-cache: always revalidated, always points to the newest assets
# index.html, always revalidate
Cache-Control: no-cache
# app.3f9a2c.js, never changes
Cache-Control: max-age=31536000, immutable
The bottom line
- max-age sets freshness in seconds; fresh copies are reused with no network at all.
- ETag asks the server: did this change?
- 304 Not Modified comes back with no body, so revalidation is cheap but still a round trip.
- Browsers, CDNs and proxies all obey the same headers.
- Fingerprint asset filenames and cache them immutable.
- Give HTML no-cache so your next fix actually ships.
Quick-fire questions
Is max-age in seconds or minutes?
Seconds, counted from when the response was sent. max-age=3600 is one hour, and max-age=31536000 is one year.
Does no-cache mean the browser won't cache the file?
No. no-cache means the browser can store the file but must revalidate it with the server every time before using it. That's why it suits HTML: users always get the latest page.
What's the difference between ETag and Last-Modified?
An ETag is a fingerprint of the content, sent back as If-None-Match. Last-Modified is a timestamp, sent back as If-Modified-Since. Both let the browser ask whether its copy changed instead of downloading it again.
Why is my CSS or JS not updating after deploy?
Usually the file kept the same URL and was served with a long max-age, so caches consider the old copy fresh and never ask. Fingerprinting the filename on each build gives the new file a URL no cache has seen.
Does a 304 response make caching free?
Almost. A 304 has no body, so very few bytes move, but the browser still waits on a network round trip. A fresh copy under max-age skips even that.