Let browsers and proxies reuse a response, and ask cheaply whether it is still fresh.
HTTP caching lets browsers, proxies and CDNs keep a copy of a response and reuse it, so the same file is not downloaded again and again.
Every time you open a page, the browser needs files: HTML, scripts, styles, images. Downloading them all each time is slow and costs bandwidth. HTTP has a built-in way to avoid that. The server adds headers to each response that tell every cache on the way what it may do.
There are two ideas. Freshness: for the next N seconds the copy is good, so use it without asking anyone. Validation: the copy is old, so ask the server a short question, and reuse the copy if the answer is “nothing changed”.
| Header or directive | What it does | Good to know |
|---|---|---|
| max-age=N | The copy is fresh for N seconds | Counted from when the response was made. |
| s-maxage=N | Like max-age, but only for shared caches (CDN, proxy) | Lets a CDN keep it longer than a browser does. |
| no-cache | Store it, but check with the server before each reuse | Often paired with an ETag, so the check is a cheap 304. |
| no-store | Do not keep any copy anywhere | For secrets and sensitive data. |
| public / private | Anyone may store it / only the user’s own browser may | Use private for anything personal. |
| must-revalidate | Once stale, never reuse without checking | Stops caches from serving old copies when the server is down. |
| immutable | This file will never change at this URL | Pair with a long max-age and a fingerprinted file name. |
| stale-while-revalidate | Show the old copy now and refresh it in the background | Fast pages, a little staleness. |
| stale-if-error | If the server is failing, an old copy may be used | Support depends on the cache. Check your CDN docs. |
| Validator | Sent by server | Sent back by client | Notes |
|---|---|---|---|
| ETag | ETag: “v42” | If-None-Match: “v42” | An opaque label for the content. Strong means byte-for-byte equal. Weak (W/) means equal in meaning. |
| Last-Modified | Last-Modified: a date | If-Modified-Since: that date | Only second-level accuracy. Simple, but less exact than an ETag. |
GET /app.js HTTP/1.1 If-None-Match: "v42" HTTP/1.1 304 Not Modified ETag: "v42" Cache-Control: no-cache
A big file that rarely changes by itself. Illustrative size: 200 KB.
A simplified model. File sizes (200, 30, 5 and 20 KB), the 0.5 KB size of a 304 and the reload times are made-up teaching numbers. It follows one browser cache and one URL, and ignores the Vary header, heuristic freshness and the reload button.
| Term | Meaning in easy words |
|---|---|
| Cache-Control | The main header that says how a response may be cached |
| Freshness | The time during which a cached copy can be used without asking the server |
| Validation | Asking the server whether an old copy is still current |
| max-age | How many seconds the copy stays fresh |
| ETag | A version label the server puts on a response |
| 304 Not Modified | The server’s answer: your copy is still good, no body sent |
| Conditional request | A request with If-None-Match or If-Modified-Since |
| Shared cache | A cache used by many people, such as a CDN or proxy |
| Private cache | A cache for one user only, such as the browser |
| Vary | A header listing the request headers that change the response |
| Fingerprinting | Putting a hash of the content in the file name, so a change means a new URL |
| Choice | We gain | We lose |
|---|---|---|
| Long max-age | No requests at all | Updates are slow to appear, unless the URL changes |
| no-cache + ETag | Always correct, and cheap checks | One round trip on every load |
| no-store | Nothing is ever kept | No speed benefit at all |
| public | A CDN can serve everyone | Risk if the content is personal |
| private | Safe for personal data | A CDN cannot help |
| Weak ETag | Cheaper to compute | Not exact to the byte |