Stale-while-revalidate (SWR) caching strategies provide faster feedback to the user of web applications, while still allowing eventual consistency. Faster feedback reduces the necessity to show spinners and may result in better-perceived user experience.

Jeff Posnick explained in a blog entry the rationale behind the stale-while-revalidate caching strategy:

stale-while-revalidate helps developers balance between immediacy— loading cached content right away —and freshness—ensuring updates to the cached content are used in the future.

stale-while-revalidate and the max-age parameters can be set in the Cache-Control of the HTTP response header. A cached response to an HTTP request that is older than max-age (expressed in seconds) is considered stale. In case of a stale response, if the age of the cached response is within the window of time covered by the stale-while-revalidate setting, the stale response is returned in parallel to a revalidation request being performed. The response to the revalidation request replaces the stale response in the cache. If the cached response is older than the window of time covered by the stale-while-revalidate setting, the browser will instead retrieve a response directly from the network and populate the cache with that response.

#javascript #http

UX Patterns: Stale-While-Revalidate
2.20 GEEK