Stale-While-Revalidate
A caching strategy that immediately returns cached (potentially stale) data to the caller while asynchronously revalidating the cache in the background.
Description
Stale-While-Revalidate (SWR) is a cache invalidation strategy defined in HTTP's Cache-Control header (RFC 5861) and popularized as a data fetching paradigm by the Vercel SWR library and React Query. The strategy works in three steps: first, return the cached data immediately (even if stale) so the user sees content instantly; second, send a revalidation request to the origin in the background; third, when the fresh response arrives, update the cache and notify the UI. This gives users the best of both worlds: instant perceived performance with eventual data freshness.
At the HTTP level, the Cache-Control directive 'stale-while-revalidate=60' tells browsers and CDNs to serve stale responses for up to 60 seconds after the max-age expires, while revalidating in the background. At the application level, React Query's default behavior is SWR: queries return cached data immediately, then refetch in the background when the data is stale (determined by staleTime). SWR triggers include window refocus (refetchOnWindowFocus), network reconnection (refetchOnReconnect), component mount, and configurable polling intervals.
SWR is particularly effective for data that changes infrequently but where freshness still matters: dashboards, social feeds, product catalogs, and configuration data. It reduces perceived loading times to near zero after the initial fetch, because subsequent navigations to the same data are served from cache. The pattern works poorly for data that must be absolutely fresh (financial balances, real-time auctions) or where showing stale data would cause user actions to fail. For those cases, you need either real-time synchronization via WebSockets or a blocking revalidation strategy.
Prompt Snippet
Configure React Query's QueryClient with global defaults: staleTime of 30 seconds, gcTime (formerly cacheTime) of 10 minutes, refetchOnWindowFocus set to true, and retry configured with exponential backoff (3 attempts, 1s/2s/4s). For the product listing page, set staleTime to 60 seconds with a keepPreviousData placeholder to prevent layout shift during pagination. Complement client-side SWR with HTTP-level SWR by setting response headers to 'Cache-Control: public, max-age=10, stale-while-revalidate=59' so that CDN edge nodes also serve stale content while revalidating against the origin, reducing origin load by ~80% for popular endpoints.
Tags
Related Terms
Cache Invalidation Strategies
Techniques for determining when cached data is stale and must be refreshed, including time-based expiry, event-based invalidation, and tag-based dependency tracking.
Optimistic Updates
A UI pattern where the interface immediately reflects a user's action as if it succeeded, then reconciles with the server response asynchronously—rolling back on failure.
Component-Level vs Global State
The architectural decision of whether state should live within a single component, be lifted to a shared ancestor, or be placed in a global store based on its scope and consumer count.
WebSocket State Synchronization
A pattern for keeping client-side state synchronized with server-side state in real time using persistent WebSocket connections that push updates as they occur.