Cachingcache-control headermax-ages-maxagestale-while-revalidatestale-if-error

Cache-Control

Definition

Cache-Control is an HTTP response (and request) header whose directives, such as max-age, s-maxage, no-store, private, stale-while-revalidate and stale-if-error, define caching rules for each layer between origin and client.

In practice

max-age sets freshness for every cache, s-maxage overrides it for shared caches such as CDNs, and private keeps a response out of shared caches entirely. stale-while-revalidate lets a cache serve a stale copy instantly while it refetches in the background, and stale-if-error lets it keep serving stale content if the origin fails, which turns the CDN into a buffer during outages.

no-cache does not mean "do not cache"; it means revalidate before every use, while no-store forbids storing. Caching a personalised response in a shared cache leaks one user's data to others. Long lifetimes are safe for fingerprinted assets but need versioned URLs or purges for anything that changes.

Example

An API listing returns Cache-Control: public, max-age=60, s-maxage=300, stale-while-revalidate=30, stale-if-error=86400, so browsers keep it for a minute, the CDN for five, and the CDN can serve it for a day if the origin is down.