Open one book's page, close it, then open it again a few seconds later. The second time, the data appears faster. In DevTools (the browser's built-in inspection tools), the Network tab marks that request (memory cache) or (disk cache). This means the browser didn't ask the server for the data again, because it still had the earlier answer.
Keeping a copy of an answer to use again is called API caching. The goal: the server doesn't have to work on the next request again, or the request doesn't even have to be sent. Think of a bookshop's price leaflet. In its corner it says "valid for 1 minute". While it's valid, you read your own leaflet without going to the shop. After 1 minute has passed, you phone the shop: "My leaflet is edition v3, still valid?" If it is, the shop just says "still valid" without sending a new leaflet. If it changed, the shop sends the latest edition. In this picture, the leaflet is the copy of the answer, you are the browser, and the shop is the server.
Cache-Control: how long, and who may store it
Here is the Bookshop API's answer to GET /books/1:
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: public, max-age=60
ETag: "4b307d096b50f2ed"
Last-Modified: Fri, 09 Oct 2026 08:00:00 GMT
{"id":1,"title":"The Rainbow Troops","author_id":1,"price":89000,"stock":12}
Cache-Control is the main header that sets how long a copy of an answer is used, in the browser and in a CDN. A CDN is a network of servers in between that keeps copies of answers for many users at once. Cache-Control holds a few directives (short rules, separated by commas). In this answer, max-age=60 is the same as "valid for 1 minute" on the leaflet:
| Directive | Meaning |
|---|---|
max-age=60 |
this copy is fresh for 60 seconds; until then it's used without asking the server |
public |
the browser and shared caches (CDN) may store it |
private |
only the browser of the user who asked may store it; a CDN may not |
no-cache |
may be stored, but must be checked with the server every time it's used |
no-store |
don't store it at all |
s-maxage=300 |
a max-age just for shared caches (CDN) |
stale-while-revalidate=300 |
after it expires, the old copy may be used 300 more seconds while the new one is fetched in the background |
An old price after an update
The answer above carries max-age=60. This table shows what happens when the price of The Rainbow Troops changes during those 60 seconds:
| Second | What happens | The price you see |
|---|---|---|
| 0 | GET /books/1, the server answers 200 and the browser stores a copy |
89,000 |
| 10 | the admin changes the price to 79,000 on the server | |
| 20 | GET /books/1 again: the copy is only 20 seconds old, still fresh, no request |
89,000 |
| 65 | the copy has expired, the browser asks the server | 79,000 |
At second 20 you still see 89,000, the old price. During max-age the browser doesn't ask the server at all, so it doesn't know the data changed. So set max-age to how long the data may be out of date. Data that must always be current uses no-cache and validators (markers that check whether a copy still matches the data on the server), which the next section covers.
ETag and If-None-Match: 304 Not Modified
An ETag is a version label for an answer, like the edition number on the leaflet. It's usually a hash of the body: a short code calculated from the content, which changes when the content changes. When the copy expires (its age passes max-age), the browser asks the server again. It sends a conditional request (a request with a condition) that carries its old ETag in the header If-None-Match:
GET /books/1 HTTP/1.1
Host: api.bookshop.test
If-None-Match: "4b307d096b50f2ed"
- The ETag is still the same: the server answers
304 Not Modifiedwith no body, like the shop that just says "still valid". The browser uses its copy again and counts its age from 0. This saves bandwidth (the amount of data sent over the network). - The ETag differs (the data changed): the server answers
200with the new body and a new ETag.
Here is the server's logic, with article data and the simple ETag "v" + version as in the exercise:
function handleRequest(article, ifNoneMatch) {
const etag = "v" + article.version;
if (ifNoneMatch === etag) {
return { status: 304, body: null, etag };
}
return { status: 200, body: article, etag };
}
| Call | etag |
Result |
|---|---|---|
handleRequest(article, null) with version: 3 |
v3 |
200, the article as body |
handleRequest(article, "v3") |
v3 |
304, body null |
handleRequest(article, "v2") |
v3 |
200, the client has an old version |
"v" + article.versionbuilds the ETag from its version, soversion: 9givesv9.ifNoneMatchis the ETag the client sent.ifNoneMatch === etagcompares it with the latest ETag.304always comes without a body (null).200carries the article.
In real HTTP an ETag value is written in quotes (ETag: "v3"). This lesson's Lab uses a SHA-256 (one way to calculate a hash) of the body, through crypto.subtle.digest("SHA-256", ...). Express also makes an ETag for res.json on its own, then answers 304 automatically when If-None-Match matches.
Last-Modified and If-Modified-Since
The second validator, after the ETag, uses time, not a version label. The server sends Last-Modified: Fri, 09 Oct 2026 08:00:00 GMT, which is when the data last changed. The browser then asks with If-Modified-Since: Fri, 09 Oct 2026 08:00:00 GMT. If the data hasn't changed since then, the answer is 304. It's only precise to 1 second, so two changes within the same second can't be told apart. When a request carries both If-None-Match and If-Modified-Since, the server uses If-None-Match.
🤔 Shared caches (CDN) and stale-while-revalidate
- A CDN keeps one copy for many users. When a second user asks for
GET /books/1, the CDN answers without contacting the server. That answer carries anAgeheader, which gives the copy's age. - That's why data belonging to one user (a profile, a cart) must be
privateorno-store. Withpublic, a CDN can send user A's data to user B. Cache-Control: max-age=60, stale-while-revalidate=300lets a page appear right away, with no wait for the server. After 60 seconds, the old copy is used right away, while the new version is fetched in the background. The React library called SWR uses the same idea.- Sometimes the answer for one URL differs by a request header, for example by the language asked for (
Accept-Language). For that, the server sends the headerVary: Accept-Languagein its answer, so the cache keeps one copy per language. Without that header, a shared cache keeps only one copy, and can send the Spanish version to a reader who asked for English. Some CDNs, for example Cloudflare, ignoreVaryunless you configure it. So the surest way is to put the language in the URL, for example/en/books/1.
Cache-Control and ETags are HTTP caching: the copy is kept in the browser or a CDN. There are other options that people also call "API caching":
| Way | When to pick it |
|---|---|
HTTP cache (Cache-Control, ETag) |
The same answer for everyone, for example the book list. The request does not even need to reach the server. This is what this lesson covers. |
| Server-side cache (memory or Redis, with a TTL) | The query is heavy, but the request must still go through the server, for example because it checks the login. Keep the query result for a few seconds or minutes (TTL, the copy's maximum age), then delete the copy when the data changes. In Laravel: Cache::remember('books', 60, fn () => ...). |
no-store |
Secret data or data that belongs to one user, for example a balance or a cart. |
⚠️ Misread directives, badly made ETags
- Thinking
no-cachemeans don't store. An answer withno-cachemay be stored, as long as it's checked with the server before it's used. The one that forbids storing isno-store. publicfor per-user data. Useprivatefor answers that differ per user.- A long
max-agefor data that changes often. Users see the old price until the copy expires. - An ETag that doesn't change with the data. An ETag built from the
idalone is always the same, so the server keeps answering304even after the data changed. Build the ETag from the version or a hash of the content.
TL;DR: API caching keeps copies of answers, so the server doesn't have to work on the next request again, or the request doesn't have to be sent. Cache-Control sets how long a copy is used in the browser and the CDN (a server in between that keeps copies for many users). The rules in Cache-Control: max-age=N means fresh for N seconds, public may be stored by a CDN, private only by the browser, no-cache may be stored but must be checked with the server first, no-store isn't stored. During max-age the browser doesn't ask the server, so a change on the server only shows after the copy expires. Then the browser sends If-None-Match with its old ETag (the version label of the answer). If it still matches the latest ETag, the server answers 304 Not Modified with no body. If not, it answers 200 with the body and a new ETag. Last-Modified and If-Modified-Since do the same, but with time. stale-while-revalidate uses the old copy while fetching the new one in the background.
⚡ Cache-Control: how long, and who may store it
Meaning: The Cache-Control header controls the copies of answers that are kept (the cache). max-age=60 means the copy is fresh for 60 seconds, and during that time it's used without asking the server. public means a CDN (a server in between that keeps copies for many users) may store it. private means only the browser of the user who asked may store it. no-cache means it may be stored, but must be checked with the server first. no-store means it isn't stored at all.
⚡ An old price after an update
Guess first: a max-age=60 copy is stored at second 0. At second 10 the price changes on the server, from 89,000 to 79,000. At second 20, which price do you see?
The old one, 89,000. The copy is only 20 seconds old, still fresh, so the browser doesn't ask at all and doesn't know the data changed.
Meaning: Set max-age to how long the data may be out of date. Data that must always be current uses no-cache, so the browser checks with the server every time.
⚡ ETag and 304 Not Modified
Write:
function handleRequest(article, ifNoneMatch) {
const etag = "v" + article.version;
if (ifNoneMatch === etag) {
return { status: 304, body: null, etag };
}
return { status: 200, body: article, etag };
}
Meaning: An ETag is a version label for an answer, here "v" plus the article's version number. When the copy expires, the browser sends the header If-None-Match holding its old ETag, and that value goes into ifNoneMatch. On a match, the server answers 304 Not Modified with no body, and the browser uses its own copy. On a mismatch, it answers 200 with the body and a new ETag.