Short answer: max-age is how many seconds every cache can reuse a response without asking your server. s-maxage replaces it for shared caches (CDNs and proxies) only; browsers ignore it. So Cache-Control: public, max-age=60, s-maxage=3600 means up to one minute in the browser (less any time the copy already spent at the CDN) and one hour at the CDN.
Who uses which
| Cache | Uses | Ignores |
|---|---|---|
| The visitor's browser | max-age, then Expires | s-maxage |
| A CDN or proxy | s-maxage, then max-age, then Expires | — |
The first one present wins, and Expires is ignored whenever there's a max-age (or, for a CDN, an s-maxage). Neither directive stops anything being stored: max-age=0 still stores the response, it's just stale straight away and has to be checked with your server before reuse.
Why have two
You can purge a CDN when you deploy. You can't purge visitors' browsers. So the usual pattern is to cache longer at the CDN and shorter in browsers:
Cache-Control: public, max-age=0, s-maxage=86400, stale-while-revalidate=60
ETag: "v42"
- The CDN serves the page for up to a day without touching your server. Vercel then serves the old copy for up to a minute while refreshing it in the background. Cloudflare ignores
stale-while-revalidatewhens-maxageis set (see below), and CloudFront's docs don't say how it treats the two together, so test it there. - Browsers check with the CDN every time. With an
ETag, that check is a quick304 Not Modified. - When you deploy, purge the CDN and everyone gets the new page on their next request.
For files whose name changes when their content does (app.3f9c2b.js), cache them for a year everywhere instead:
Cache-Control: public, max-age=31536000, immutable
Time spent in the CDN counts
Freshness is counted from when your server generated the response, not from when the browser got it. A CDN tells the browser how old its copy is in the Age header:
Cache-Control: max-age=60, s-maxage=3600
Age: 50
This copy has been at the CDN for 50 seconds, so the browser can reuse it for only 10 more seconds, not 60. If Age is already more than max-age, the browser treats it as stale straight away. This is why a long s-maxage with a short max-age can make browsers revalidate on almost every request.
Things that catch people out
s-maxagealso means "revalidate once stale". It impliesproxy-revalidate: by the spec, a shared cache mustn't serve the response afters-maxageruns out without checking first. CDNs differ onstale-while-revalidateandstale-if-erroralongside it: Vercel honours them, Cloudflare doesn't, and CloudFront supports them but its docs don't cover thes-maxagecase.privatewins.private, s-maxage=3600means no shared cache stores it at all, sos-maxagedoes nothing.- Your host may rewrite it. For responses from Vercel Functions where
Cache-Controlis your only caching header, Vercel usess-maxage,stale-while-revalidateandstale-if-erroritself and removes them before the browser sees the header. Anymax-ageis kept; with none, the browser getspublic, max-age=0, must-revalidate. Static files with headers set invercel.jsonare different: Vercel sends theirCache-Controlunchanged. We checked both live on this site. - Your CDN may cap it. CloudFront keeps the header as it is but caches for no less than the cache policy's minimum TTL and no more than its maximum.
- There are CDN-only headers.
CDN-Cache-Control(RFC 9213) is used by CDNs and ignored by browsers. Vercel and Cloudflare also have their own (Vercel-CDN-Cache-Control,Cloudflare-CDN-Cache-Control) that only they read and don't pass on. These let you set the CDN's lifetime without touchingCache-Controlat all.
Check your headers
Paste a response's headers into the HTTP caching explainer, with your CDN chosen, to see how long the browser and the CDN each keep it, what Age does to that, and what your host changes. Its header builder suggests headers for HTML, assets and APIs.
