Stale-While-Revalidate Pattern
What you'll learn: How to deliver instant responses by serving cached content first, then quietly updating the cache in the background for next time.
The Core Idea
Imagine a newspaper stand where the vendor hands you yesterday's paper immediately (because they have it ready), but simultaneously runs to fetch today's edition. You get instant access to content, and the next customer will get the fresh version. That's stale-while-revalidate.
This pattern combines the speed of cache-first with the freshness of network-first. When a request comes in, your service worker:
- Immediately returns the cached version (if available) — no waiting
- Simultaneously fetches from the network in the background
- Updates the cache with the fresh response for the next request
The user gets blazing-fast responses because they're not waiting for the network, but your cache stays relatively fresh because every request triggers a background update.
When to Use This
This pattern shines for content that changes occasionally but where instant loading matters more than absolute freshness:
- Social media feeds
- News articles
- Product listings
- User profiles
- API responses that tolerate being slightly outdated
It's a middle-ground strategy: users see something instantly (even if a few minutes old), and the cache gradually stays current without forcing anyone to wait.
The Tradeoff
The current request always sees potentially stale data. The network update only benefits the next request. If you need guaranteed fresh data on every load, use network-first instead.
Key Takeaway: Stale-while-revalidate delivers cached content instantly for speed while fetching fresh content in the background, keeping your cache gradually updated without sacrificing response time.