I re-read all 78 of our pages for 36 KB, because must-revalidate plus an unchanged ETag is 199 bytes a page
Our pages are served with this, and I had never once checked what it costs: cache-control: public, max-age=0, must-revalidate etag: "2w4ih8cxzq1oow" x-nextjs-prerender: 1 x-vercel-cache: HIT age: 308145 max-age=
Our pages are served with this, and I had never once checked what it costs:
cache-control: public, max-age=0, must-revalidate
etag: "2w4ih8cxzq1oow"
x-nextjs-prerender: 1
x-vercel-cache: HIT
age: 308145
max-age=0, must-revalidate reads like "do not cache this", and people often write it believing that. It means something else: cache it, and ask before every reuse. The question is what the asking costs, so I crawled all 78 public pages of the site in one browser session and added up the bytes, then did the same with the cache switched off.
78 pages, 5.58 MB of HTML, 36 KB on the wire
| the whole crawl | |
|---|---|
| page documents loaded | 78 |
| HTML decoded | 5,578,583 bytes |
| HTML actually transferred | 36,016 bytes |
| documents that cost exactly 300 bytes | 77 |
| the one that did not | /pricing, 12,916 bytes |
Every prerendered page came back as a 304. The 300 bytes is what Chrome reports for a response with no body, and the real number on the wire is smaller. Measured with curl against the FAQ:
$ curl -s -o /dev/null -w '%{http_code} body=%{size_download} headers=%{size_header}\n' \
-H 'If-None-Match: "2w4ih8cxzq1oow"' https://pub-trivia.app/faq
304 body=0 headers=199
$ curl -s -o /dev/null -w '%{http_code} body=%{size_download} headers=%{size_header}\n' \
https://pub-trivia.app/faq
200 body=78658 headers=725
199 bytes to confirm that a 78,658-byte document has not changed. That is the deal must-revalidate offers, and it is a good one. The reason every ETag still matched is visible in the headers above: age: 308145 is three and a half days, because the deploy has not changed since 14 November, and our ETags are content-derived rather than build-derived.
The exception is the honest kind. /pricing is force-dynamic and answers private, no-cache, no-store, so it re-downloads in full on every view, forever. Two pages on this site are dynamic and both of them are supposed to be.
One page, cold and warm
Zooming in on a single page makes the shape clearer than any site-wide total. The FAQ, same browser, same minute, cache disabled and then enabled through the devtools protocol:
| /faq | cold | warm |
|---|---|---|
| HTML | 12,618 B | 300 B |
| subresources (48 of them) | 501,434 B | 31,184 B |
| total | 514,052 B | 31,484 B |
| subresources at zero bytes | 0 of 48 | 26 of 48 |
| time to first byte | 68 ms | 16 ms |
94 per cent off, and the reason is boring in the best way: everything under /_next/static is served max-age=31536000, immutable, so the 21 script requests that cost 393,855 bytes on a first visit cost zero on the second. Not 304s, not revalidations: zero requests, straight out of the local cache, because the URLs contain a build hash and a hashed URL is a promise that the bytes behind it will never change.
Across the whole 78-page crawl the same effect shows up as a round number:
| subresource loads in the crawl | 3,412 |
|---|---|
| served without touching the network | 1,714 (50.2%) |
| bytes transferred | 2,068,267 |
| bytes those responses actually contain | 35,202,333 |
So 94 per cent of the payload of a 78-page crawl never crossed the network, and that figure is the entire point of having a static asset pipeline with content-hashed filenames.
What a repeat visit still pays for
Here is the bit that interested me most. On the warm load, 31,184 bytes still came down. Broken out by what started the request:
| initiator | requests | warm bytes |
|---|---|---|
script |
21 | 0 |
link (CSS and fonts) |
6 | 300 |
img |
1 | 300 |
fetch |
20 | 30,584 |
Our scripts, styles and fonts are free on a repeat visit. Our CSS costs one revalidation. And 97 per cent of everything a returning visitor downloads is those twenty fetch entries, which are the router prefetching the pages behind the links on screen. I have written about the cost of that prefetching before as a first-visit problem. It turns out to be a much larger share of the second visit, because it is the only thing left that is not cacheable: each prefetch is a per-route payload, requested with the router's own headers, and the response varies on four of them:
vary: rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch
A document that varies on four request headers nobody can predict is a document that will be fetched again. So our cache story ends up inverted from the usual one: the big static assets are perfect, the HTML is close to free, and the expensive part of a warm page load is speculative requests for pages the visitor has not asked for, and if the visitor reads the page they arrived on and leaves, every one of those requests was spent on a page nobody opened.
The thing must-revalidate does not save
It saves bytes, not time. Over a ten-page sample, measuring responseStart - requestStart warm and cold:
| warm (304) | cold (200) | |
|---|---|---|
| median | 20 ms | 18 ms |
| range | 12 to 131 ms | 11 to 52 ms |
A revalidation is a full round trip to the edge. You still pay the latency, you just do not pay the transfer, which on a 12 KB brotli document is most of the win anyway. If you want to skip the round trip you need a real max-age, and for HTML at a stable URL that means serving a reader a stale page with no way to correct it. For a marketing site that gets edited weekly, 199 bytes and one round trip is the right price.
Check it yourself
Open any page on the site with the Network panel showing and look at the Size column, then reload:
- the HTML row says 304 and about 300 B
- every
/_next/static/chunks/...row says (disk cache) or (memory cache) - the summary at the bottom reads something like 31 KB transferred of 486 KB resources
// paste in the console after a reload
const r = performance.getEntriesByType('resource')
;({ requests: r.length,
free: r.filter(x => !x.transferSize).length,
transferred: r.reduce((a, x) => a + (x.transferSize || 0), 0),
contained: r.reduce((a, x) => a + (x.encodedBodySize || 0), 0) })
// { requests: 48, free: 26, transferred: 31184, contained: 486424 }
The ratio of the last two numbers is the most flattering performance statistic any site has, and it is also the one nobody puts in a report. If your transferred is close to your contained on a second visit, something in your pipeline is serving a different URL for the same bytes, and that is worth half an hour of anybody's time.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.