Skip to main content
sitewarm
HTTP caching headers

What is stale-while-revalidate?

stale-while-revalidate lets a cache serve a stale response for a set number of seconds while it fetches a fresh copy in the background.

stale-while-revalidate is a `Cache-Control` extension, defined in RFC 5861, that lets a cache serve a stale response for up to the given number of seconds after it goes stale, while it revalidates that response in the background. The visitor gets an immediate answer, and the next one gets the refreshed copy.

Cache-Control: max-age=60, stale-while-revalidate=300

Here the response is fresh for 60 seconds. For the next 300 seconds a cache may still serve it, as long as it starts a background revalidation. After that window it must revalidate before reuse, as it would without the directive.

  • It hides origin latency only when there is a stale copy to serve. After a purge there is none, so the first request is still a miss. That gap is what cache warming fills.
  • `must-revalidate` forbids reusing a stale response without validation, so it cancels the effect. Cloudflare documents the same for no-cache with Origin Cache Control enabled.
  • Cloudflare reports a background refresh as UPDATING, and the cache status of a stale hit varies by CDN.

Its sibling `stale-if-error` serves stale content when the origin fails rather than while it is slow.

References

  1. RFC 5861: HTTP Cache-Control Extensions for Stale Contenthttps://www.rfc-editor.org/rfc/rfc5861
  2. Cloudflare: Revalidationhttps://developers.cloudflare.com/cache/concepts/revalidation/
  3. Fastly: Serving stale contenthttps://www.fastly.com/documentation/guides/concepts/cache/stale/

Warm your cache after every deploy

sitewarm requests the pages in your sitemap after a deploy and reports the cache state the CDN returns for each one, with the vantage point it was measured from.