Stale While Revalidate
Warning
Over a year old. May be outdated
stale-while-revalidate is a Cache-Control directive. Serve the stale copy now, fetch a fresh one in the background. The page feels faster because the user gets something instantly, even if it's a little old, and the update happens asynchronously.
How it works
- First request. Nothing cached or copy still fresh, so fetch from the server and serve it.
- Stale but inside the window. Past its freshness lifetime but still within
stale-while-revalidate, so serve the stale copy right away. - Revalidate. The cache asks the origin for a fresh copy in the background and swaps it in.
- Past the window. Stale and the window has also passed, so the cache usually waits for fresh content. (This can depend on other cache directives and on the cache implementation.)
Use it when slightly old now beats newest later, especially when freshness isn't critical.
Backlinks2
240126231026