1.6 HTTP cache headers
The fastest request is the one that never happens.
Set HTTP cache headers properly
Nothing beats not making the request. Cache headers tell the browser it already has what it needs.
The two-tier strategy
Fingerprinted assets — app.4f3a9b.js — never change content under the same
name, so cache them forever:
Cache-Control: public, max-age=31536000, immutableimmutable means the browser will not even revalidate on reload. A new build
produces a new filename, so staleness is impossible.
HTML must not be cached that way, or users never see a new deployment:
Cache-Control: no-cacheno-cache is misleadingly named: it means revalidate before using, not do not
store. The browser asks; the server answers 304 Not Modified if nothing
changed. Cheap and always correct. (no-store is the one that prevents storage
entirely — use it only for sensitive responses.)
stale-while-revalidate
Serves the cached copy instantly and refreshes behind the user:
Cache-Control: public, max-age=60, stale-while-revalidate=86400Ideal for API responses and semi-static pages.
Get fingerprinting right
The scheme depends on the hash changing when content changes. Bundlers handle
this; hand-managed files in a public folder are where it breaks. logo.png
cached for a year is a logo you cannot update.