Sub topic 13
Caching and conditional requests
This chapter assumes you can read an HTTP request and response, know what headers are, and recognize status codes such as 200 and 304. It covers how a browser avoids downloading the same resource twice.
Why caching exists
Fetching a resource over the network costs time and bandwidth. If the browser already holds a copy that is still good, it can skip the trip. HTTP caching rests on two questions:
- Is my stored copy fresh enough to use without asking the server?
- If not, is it still valid, or must I download a new one?
The first question is answered by freshness rules. The second is answered by conditional requests.
Freshness with Cache-Control
The server tells the browser how long a response stays fresh with the Cache-Control header. max-age=3600 means the copy is fresh for 3600 seconds (one hour) after it was received. During that time the browser serves the resource from its cache with no network request at all.
Other common directives:
| Directive | Meaning |
|---|---|
| `max-age=N` | Fresh for N seconds |
| `no-cache` | May be stored, but must be revalidated before every use |
| `no-store` | Do not store the response at all |
| `public` / `private` | Shared caches (CDNs) may, or may not, store it |
A common mistake is to think no-cache means "don't cache". It means "cache, but check first". Use no-store for sensitive data.
Revalidation with ETag
When a copy is stale, the browser does not have to download it again. The server can attach an ETag, a short identifier for the exact version of the resource. The browser stores it and sends it back later in an If-None-Match header. This is a conditional request: "send the body only if it differs from this version."
If the version still matches, the server replies 304 Not Modified with no body. The browser reuses its stored copy and the freshness timer restarts.
HTTP/1.1 200 OK
Cache-Control: max-age=3600
ETag: "v1"
GET /logo.png HTTP/1.1
Host: example.com
If-None-Match: "v1"
HTTP/1.1 304 Not Modified
ETag: "v1"
The three messages above are the first response, the later revalidation request, and the server's answer.
The full flow
Common errors
- Long max-age on a file that changes. Users keep seeing the old version until the timer runs out. Many sites avoid this by putting a content hash in the file name (
app.3f9a1c.js) and giving it a very longmax-age. A new version gets a new URL. - Caching personalized pages publicly. A response that depends on the logged-in user should be
privateorno-store, or a shared cache may serve one user's page to another. - Confusing reload with navigation. A normal reload usually revalidates. A hard reload skips the cache. Test both when debugging.
Exercise
A response has Cache-Control: max-age=60 and an ETag: "a1". Two minutes later the browser needs the resource again. What does it send, and what reply lets it avoid downloading the body?
Show answer
The copy is stale after 60 seconds, so the browser sends a conditional request with If-None-Match: "a1". If the resource has not changed, the server replies 304 Not Modified with no body, and the browser reuses its stored copy.
Exercise
Which directive should you use for a page showing a user's bank balance: no-cache or no-store, and why?
Show answer
no-store. no-cache still lets the browser keep a copy and only requires revalidation, while no-store prevents the response from being stored.