In short
Browser caching stores a copy of a previously fetched resource locally so a later request can be answered instantly or with a lightweight check, instead of downloading the full response again. Every caching decision is governed by a small set of HTTP response headers that the server controls, chiefly Cache-Control, ETag and Last-Modified. Getting these headers right is usually a bigger performance lever than almost any code-level optimisation, because it removes network round trips entirely rather than making them faster.
Every time a browser loads a page, it must decide, resource by resource, whether it already has a usable copy of that file. That decision is not left to guesswork: it follows explicit instructions the server attached to the response the first time the resource was fetched. Those instructions are what the phrase 'HTTP caching' actually refers to, and they apply identically to a stylesheet, an API response, a font file or an entire HTML document.
The two questions every cache answers
A cache handling a repeat request effectively asks two questions in sequence. First: is my stored copy still considered fresh, meaning it can be used immediately without contacting the server at all? Second, only if the answer to the first is no: is my stored copy still valid, meaning the server confirms nothing has actually changed, even though its freshness window has expired? The answer to the first question comes from Cache-Control's max-age or Expires; the answer to the second comes from validators like ETag and Last-Modified.
Cache-Control: the primary instruction
Cache-Control is a response header the server sets to control exactly how and for how long a response may be reused, and it can combine several directives at once.
| Directive | Meaning |
|---|---|
| max-age=N | The response is considered fresh for N seconds from when it was fetched |
| no-cache | The response may be stored, but must be revalidated with the server before each reuse |
| no-store | The response must not be stored anywhere, in any cache, under any circumstances |
| public | The response may be stored by shared caches (such as a CDN), not just the browser |
| private | The response may only be stored by the end user's browser, not by shared intermediaries |
| immutable | The response will never change during its freshness window, so it never needs revalidation even on reload |
A common point of confusion is that no-cache does not mean 'do not cache'; it means 'cache it, but check with the server before reusing it every single time'. The directive that actually prevents storage entirely is no-store, and it is reserved for genuinely sensitive responses that must never persist on disk.
Validators: cheaper than refetching
Once a cached response's freshness window has expired, the browser does not necessarily need to download it again in full. If the server previously supplied an ETag (an opaque fingerprint of the response content) or a Last-Modified date, the browser can send a conditional request containing that value. If the resource has not actually changed, the server replies with a 304 Not Modified status and an empty body, telling the browser to keep using its stored copy and simply refresh its freshness timer.
- ETag is more precise, since it can detect a content change even when a file's modification timestamp is unreliable or unavailable.
- Last-Modified is coarser (typically second-level precision) but requires no extra computation to generate, since it is often just a filesystem timestamp.
- A 304 response typically transfers only a few hundred bytes of headers, versus the full resource body for a fresh 200 response.
- Both validators can be used together; if present, ETag generally takes precedence over Last-Modified for the actual comparison.
Where cached content actually lives
Browsers maintain more than one caching layer, and they are checked in a fairly predictable order for a typical navigation.
- Memory cache: extremely fast, but limited in size and cleared when the tab or browser process closes; used for resources reused within the same page session.
- Disk cache: persists across browser restarts and holds the bulk of cached resources, checked when memory cache misses.
- Service worker cache (Cache API): under explicit developer control via JavaScript, allowing an application to define custom caching logic, including offline availability, independent of standard HTTP caching rules.
- HTTP cache in a shared intermediary such as a CDN or corporate proxy, which sits between the browser and the origin and can serve cached responses to many different users.
A service worker sits in a genuinely different category from the first two: rather than passively obeying Cache-Control headers, it intercepts every network request the page makes and lets developer-written JavaScript decide, request by request, whether to serve from a cache, fetch from the network, or apply a custom strategy such as serving cached content while fetching an update in the background.
The versioned filename pattern
The most reliable way to combine long caching with instant updates is to bake a content hash into the filename of a static asset, for example app.a3f9c1.js, and set an extremely long max-age with the immutable directive on it. Because the filename itself changes whenever the content changes, the browser never needs to ask whether app.a3f9c1.js has changed — it never will, by construction — while a genuinely new version simply arrives under a new filename that was never cached in the first place. The HTML that references the asset is served with little or no caching, so the reference always points to the current filename.
Illustrative repeat-visit load time by caching strategy
Milliseconds for a typical set of static assets, illustrative figures
Figures are illustrative and depend heavily on asset size, connection speed and server response time; they are intended to show the relative ordering of the strategies, not precise real-world values.
Aggressive long-lived caching with hashed filenames
Pros
- Repeat visits can skip the network entirely for unchanged assets, which is the fastest possible outcome
- Removes load from the origin server and any intermediary caches for the vast majority of repeat requests
- Deployment becomes simpler because there is no need to actively purge or invalidate old versions
Cons
- Requires a build process that generates content-hashed filenames and rewrites references automatically
- The HTML entry point itself must still be served with careful, usually short, caching to avoid pointing at stale asset filenames
- Old cached versions can linger indefinitely on a user's device, which is fine for immutable assets but wastes some disk space
Vary and the risk of serving the wrong cached response
A cached response is only useful if it is actually appropriate for the request that reuses it, and the Vary header is what protects against serving the wrong one. Vary tells a cache which request headers must match, in addition to the URL, before a stored response can be reused; a common example is Vary: Accept-Encoding, which prevents a cache from serving a gzip-compressed response to a client that requested an uncompressed one, and Vary: Accept-Language, which stops a cached page in one language being served to a request that asked for another.
Getting Vary wrong causes two opposite failures. Omitting a header that should be included in Vary can cause a cache, particularly a shared one such as a CDN, to serve content intended for one user segment to a different one — a serious defect if the varying content includes anything personalised. Over-including headers in Vary, on the other hand, fragments the cache into so many small variants that the effective hit rate collapses, since a request with a slightly different, functionally irrelevant header value is treated as a completely different resource.
Stale-while-revalidate: serving fast and updating quietly
A more recent Cache-Control extension, stale-while-revalidate, addresses a common tension: strict freshness windows force occasional slow requests when the window expires, while long freshness windows risk serving outdated content. The directive lets a cache serve a stale response immediately while it fetches a fresh copy in the background for next time, within a bounded grace period after the normal freshness window ends.
| Behaviour | Plain max-age only | max-age with stale-while-revalidate |
|---|---|---|
| Request within freshness window | Served instantly from cache | Served instantly from cache |
| Request just after window expires | Waits for a full network round trip | Served instantly from the stale copy; refresh happens in the background |
| Request well past the grace period | Waits for a full network round trip | Waits for a full network round trip |
This pattern suits content that changes periodically but where a few extra seconds of staleness is an acceptable trade for consistently fast responses, such as a news index page or a product listing. It is less appropriate for content where even a brief window of staleness would be actively misleading, such as a live stock price or account balance.
Debugging a caching problem in the browser
A browser's network inspector panel reports, for each request, whether the response came from the memory cache, the disk cache, a service worker, or the network, along with the full set of response headers that produced that outcome. Working through a caching problem is usually faster starting from this panel than from guessing at server configuration, since it shows what the browser actually did rather than what the headers were intended to achieve.
- Check the request's status and size column: a 200 status with a tiny transferred size and a cache label indicates it was served from a local cache rather than the network.
- Inspect the response headers for Cache-Control, ETag and Last-Modified to see exactly what freshness and validation rules were in force.
- Disable the cache temporarily in developer tools to confirm whether an apparent bug disappears, which isolates a caching issue from a genuine application bug.
- For service-worker-related problems, inspect registered service workers and their associated Cache Storage entries directly in the application panel, since these are invisible to the ordinary network cache view.
Key points
- Caching decisions are governed by response headers the server sets, chiefly Cache-Control, ETag and Last-Modified.
- no-cache means always revalidate before reuse; no-store means never store the response at all.
- A 304 Not Modified response lets a browser confirm a cached copy is still valid without re-downloading it.
- Hashed, versioned filenames combined with long max-age values give the best of both long caching and instant updates.
- Service workers intercept requests under explicit developer control and are not bound by standard HTTP caching rules.
Sponsored video
AdvertisementChecking for a video ad…
Frequently asked questions
Sources
- RFC 9111: HTTP Caching — IETF
- HTTP caching — Mozilla Developer Network
- Service Worker API — Mozilla Developer Network
- Cache-Control for Civilians — web.dev, Google Chrome team
- RFC 5861: HTTP Cache-Control Extensions for Stale Content — IETF
About the author
Michael Adeyemi — Senior Technology Writer
Michael writes Inquro's computing and web-infrastructure coverage. He worked for eight years as a platform engineer on high-traffic content systems before moving into full-time technical explanation, and he still prototypes every measurement described in his articles before publishing the numbers.
- Qualifications
- MSc, Distributed Systems — University of Manchester
- BEng, Computer Engineering — University of Lagos
- Experience
- Eight years as a platform engineer on high-traffic content delivery systems
- Four years writing standards-based technical documentation
- Subject areas:
- Web performance, Networking, Systems architecture
- Contact:
- michael@inquro.example
Reviewed for factual accuracy by Jess Tran, Health & Wellbeing Writer (MPH, Public Health), on 30 January 2026.
Whether health claims stay descriptive rather than prescriptive, and whether study limitations are visible in the body text.
Inquro articles are commissioned, sourced, reviewed and re-checked under a published editorial policy. Corrections are made in place and dated — write to editorial@inquro.example if you find an error.






