CodePark/ questions / HTTP caching & CDNs
START PRACTICINGPRACTICE
Frontend question · Browser & web performance › HTTP caching & CDNs

Explain the main Cache-Control directives: max-age, no-cache, no-store, private, public, immutable and stale-while-revalidate.

These directives tell caches whether they may store a response, for how long, and when they must check with the server.

max-age=N makes the response fresh for N seconds. While it is fresh, caches can reuse it without contacting the server. s-maxage does the same, but only for shared caches like CDNs.

no-cache allows storing, but the cache must revalidate with the server before each use. no-store forbids storing the response at all. It suits sensitive data.

private means only the user's browser may cache the response, not shared caches. public allows shared caches to store it. This includes some cases where they normally wouldn't, such as authenticated requests.

immutable says the content will never change while it is fresh. So the browser should not revalidate it, even when the user reloads. It fits hashed asset files like app.3f9a1c.js.

stale-while-revalidate=N lets a cache serve a stale (expired) response for up to N seconds after it expires. Meanwhile, the cache fetches a fresh copy in the background.

  • max-age sets how long a response stays fresh.
  • no-cache means revalidate before each use, and no-store means never store.
  • private keeps the response out of shared caches, and public allows them.
  • immutable is for hashed assets that never change.
  • stale-while-revalidate serves the stale copy while it fetches an update.