Browser Caching Explained: Cache-Control, ETags, Expiration and Practical Configuration
Browser, server and CDN caches are three different things. Cache-Control in detail, conditional requests, and a strategy that can be invalidated.
In short
Caching means not downloading something you already have. Done properly, a returning visitor loads your site almost instantly, because their browser already holds your CSS, JavaScript, fonts and images.
Before anything else, one distinction that this guide exists to make, because conflating these three is the source of most caching confusion:
| Layer | Where it lives | What it saves |
|---|---|---|
| Browser cache | On the visitor's device | The whole request. Nothing travels at all. |
| CDN edge cache | On a server near the visitor | The trip to your origin. |
| Server-side cache | On your own server | Rebuilding the page — database queries, template rendering. |
They solve different problems and are configured in different places. "Enable caching" is not one task.
This guide covers the browser layer, which is controlled entirely by the HTTP headers your server sends with each response.
What browser caching does not do
Worth saying early, because it manages expectations: it does nothing for a first-time visitor. They have an empty cache, so every file is downloaded regardless of your headers.
This matters because Lighthouse measures a first visit. You can configure caching perfectly and see no change in your score, while every returning visitor benefits substantially. Both facts are true at once.
Cache-Control, in practical order
Cache-Control is the header that matters. The others are refinements.
Cache-Control: public, max-age=31536000, immutable
| Directive | What it means |
|---|---|
max-age=<seconds> | How long the response is fresh. 31536000 is one year. |
public | Any cache may store it, including shared caches like a CDN. |
private | Only the visitor's browser may store it. Use for personalised responses. |
no-cache | May be stored, but must be revalidated before reuse. Not "do not cache". |
no-store | Must not be stored at all. For genuinely sensitive responses. |
immutable | This will never change, so do not revalidate it even on a reload. |
s-maxage=<seconds> | A different lifetime for shared caches. Lets you cache longer at the edge than in the browser. |
stale-while-revalidate=<seconds> | Serve the stale copy immediately while fetching a fresh one in the background. |
Two of these are routinely misunderstood. no-cache does not mean no caching — it means revalidate before use, which is usually what people actually want. The one that means "do not store this anywhere" is no-store.
ETag, Last-Modified and what a 304 saves
When a cached response goes stale, the browser does not have to download it again blindly. It can ask whether anything changed.
# First response
ETag: "a7c3f91"
Last-Modified: Tue, 18 Nov 2026 09:12:04 GMT
# The browser's next request
If-None-Match: "a7c3f91"
# The server's answer, if nothing changed
HTTP/1.1 304 Not Modified
A 304 has no body, so you save the download. You do not save the round trip — the browser still had to ask, and on a mobile connection that question can cost 200 ms or more.
This is the argument for long max-age with immutable on files that genuinely never change: no question, no round trip, nothing.
ETag is a fingerprint of the content. Last-Modified is a timestamp, and only has one-second precision, so ETag is the more reliable of the two.
The strategy that works
Browser caching only becomes powerful once you separate files by whether they change.
Fingerprinted assets: cache for a year
A fingerprinted file has a hash of its contents in its name — app.7f3a91c.js. If the contents change, the name changes, so the old name can be cached forever without any risk of serving stale content.
location ~* \.[0-9a-f]{8,}\.(js|css|woff2|png|jpg|webp|avif|svg)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
This is the single highest-value caching rule there is, and it is only safe with fingerprinting. Applying a one-year lifetime to app.js means visitors keep a stale copy for a year.
Most build tools produce fingerprinted filenames by default. If yours does not, adding it is the prerequisite for everything in this section.
HTML: short, or revalidated
Your HTML is how visitors discover new asset filenames, so it must stay reasonably fresh.
location / {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
That costs a revalidation round trip per navigation and guarantees content updates are seen. For a content site where a few minutes of staleness is acceptable, a short max-age with stale-while-revalidate gives better perceived speed:
Cache-Control: public, max-age=60, stale-while-revalidate=600
Non-fingerprinted static files
Images uploaded through a CMS usually do not have content hashes. A medium lifetime — a week to a month — is a reasonable compromise, with stale-while-revalidate so a return visit is instant while a fresh copy is fetched.
Personalised and authenticated responses
Cart, account and checkout pages must not be stored in a shared cache.
Cache-Control: private, no-store
Configuration
Nginx
# Fingerprinted assets: a year, never revalidated
location ~* \.[0-9a-f]{8,}\.(js|css|woff2)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
access_log off;
}
# Uploaded media: a month, with background refresh
location /uploads/ {
add_header Cache-Control "public, max-age=2592000, stale-while-revalidate=86400";
}
# HTML: always check
location / {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
Apache
<FilesMatch "\.[0-9a-f]{8,}\.(js|css|woff2)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
<FilesMatch "\.(html)$">
Header set Cache-Control "public, max-age=0, must-revalidate"
</FilesMatch>
Hosted platforms
On Shopify, Wix, Squarespace, Webflow and most hosted platforms, you cannot set these headers. The platform sets them, generally sensibly. If a report tells you to improve your cache policy and you are on one of these, the recommendation is not actionable — see the platform permission matrix.
Invalidation: the hard part
Caching is a promise about the future, and sometimes you need to break it.
With fingerprinting, you never have to. A change produces a new filename, so there is nothing to invalidate. This is why fingerprinting is the foundation rather than an optimisation.
Without it, your options are poor. You can wait for the lifetime to expire, add a query string (?v=2, which works but is untidy and not always cached consistently), or rename the file by hand. None scales.
At the CDN layer you can purge, which is a different mechanism from browser caching — a browser holding a file for a year cannot be reached by a purge. One more reason the browser layer should only cache long when the filename guarantees it is safe.
Verifying it
Check the headers
curl -sI https://example.com/assets/app.7f3a91c.js | grep -i "cache-control\|etag\|last-modified"
Watch a warm load
- Load the page with the cache enabled.
- Reload — not a hard reload, which bypasses the cache deliberately.
- In the Network panel, look at the Size column.
What you should see: (memory cache) or (disk cache) for your static assets, and a much lower total transfer. Files showing a size and a 200 are being re-downloaded. Files showing 304 are being revalidated — better than a download, but still a round trip.
Check nothing personal is being cached publicly
Load an account or cart page and confirm the response carries private or no-store. If you use a CDN, browse as two different signed-in users and confirm neither sees the other's data.
Common mistakes
Confusing the three layers. Configuring browser caching and expecting server rendering to get faster.
Long max-age without fingerprinting, so visitors hold stale files.
Thinking no-cache means do not cache. It means revalidate.
Caching personalised responses publicly. The most damaging error here.
Expecting it to improve a first-visit score. It cannot.
Relying on ETag alone with long-lived assets, paying a round trip you could have avoided with immutable.
Hard-reloading to test caching, which deliberately bypasses what you are testing.
Setting headers in two places — server and CDN, or server and application — and getting whichever wins.
When it still does not help
- Assets are re-downloaded despite your headers → check they are actually being sent; a CDN or proxy may be overriding them.
curl -sIthe real public URL, not your origin. - Everything shows 304 → your lifetime is too short, or you lack
immutable. 304s cost round trips. - It works for you and not for visitors → different URLs. Query strings, locale prefixes or tracking parameters create separate cache entries.
- No change in your score → expected. Lighthouse measures a cold load.
- A deploy served stale files → fingerprinting is missing or inconsistent. Fix the build, not the headers.
- You are on a hosted platform → you cannot change this. Spend the effort elsewhere.
Keeping it right
Caching breaks quietly: a new asset pipeline that drops fingerprinting, a CDN rule added that overrides your headers, a new route serving personalised content without private.
Three habits: make fingerprinting a build requirement rather than a convention; check headers on a real public URL after any infrastructure change; and include "is anything personalised cacheable?" in your release check, because that is the failure with real consequences.
Frequently asked questions
What is the difference between browser caching, CDN caching and server caching?
They solve different problems. The browser cache lives on the visitor’s device and saves the whole request. A CDN edge cache lives near the visitor and saves the trip to your origin. A server-side cache lives on your server and saves rebuilding the page. “Enable caching” is not one task.
What does no-cache actually mean?
Not “do not cache”. It means the response may be stored but must be revalidated before it is reused. The directive that means do not store it anywhere is no-store.
How long should I cache static assets?
A year, with immutable, if the filename contains a hash of the contents — because a change produces a new filename, so a stale copy is impossible. Without that fingerprinting a long lifetime means visitors hold stale files, so fix the build first.
Why did fixing my cache policy not improve my PageSpeed score?
Because browser caching does nothing for a first-time visitor, and Lighthouse measures a first visit. Every returning visitor benefits substantially. Both things are true at once.
What does a 304 Not Modified response save?
The download, but not the round trip — the browser still had to ask. On a mobile connection that question can cost 200 ms or more, which is the argument for long lifetimes with immutable on files that genuinely never change.
Can I set cache headers on Shopify or Wix?
No. On hosted platforms the response headers are set by the platform and are not configurable. If a report recommends improving your cache policy, that recommendation is not actionable on those platforms.
References
- HTTP caching — MDN
- Cache-Control — MDN
- RFC 9111: HTTP Caching — IETF