SWR Core Concepts: Stale-While-Revalidate
Understand SWR's caching strategy: serve stale data immediately, revalidate in the background, update when fresh.
SWR Core Concepts: Stale-While-Revalidate is a free React Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Does SWR Stand For?
SWR stands for Stale-While-Revalidate, an HTTP cache strategy defined in RFC 5861. The strategy is: serve stale (cached) data immediately to the user, then revalidate in the background, and update the UI when fresh data arrives.
This approach prioritizes perceived performance: users see data instantly rather than waiting for a network round-trip.
The User Experience Benefit
With traditional loading patterns, users see a spinner on every navigation. With SWR, they see the last known data immediately and the UI updates silently in the background if the data has changed.
This makes applications feel significantly faster and reduces the cognitive disruption of loading states.
When SWR Triggers Revalidation
SWR automatically revalidates data when: the browser window regains focus (user switches tabs and returns), the network connection is restored after going offline, explicit mutate() is called, or a configurable refreshInterval timer fires.
These automatic triggers keep data fresh without the developer manually managing refetch logic.
The SWR State Machine
A SWR key can be in one of several states: loading (no cached data, first fetch in progress), revalidating (has cached data, background refresh in progress), error (request failed), or success (fresh data available).
Understanding these states helps you build accurate loading skeletons and error UIs.
The Global SWR Cache
SWR maintains a global in-memory cache keyed by the request key (typically a URL string). Any component that requests the same key gets the same cached value and participates in the same revalidation cycle.
This automatic deduplication means 10 components can each call useSWR with the same key and only one network request is made.
The Fetcher Function
SWR separates the cache management logic from the actual data fetching. You provide a fetcher function: an async function that accepts the key and returns the data.
The fetcher is swappable: use fetch, axios, graphql-request, or any async data source. SWR handles caching and revalidation regardless of how the fetcher works.
Configuring a Global Fetcher
Wrap your app in SWRConfig with a value prop containing a fetcher function: value={{ fetcher: (url) => fetch(url).then(r => r.json()) }}. Every useSWR call that omits a fetcher argument uses this global default.
This centralizes API configuration (base URL, auth headers) in one place without repeating it in every hook call.
SWR vs Traditional Data Fetching
The classic pattern uses useEffect + useState to manage loading, data, and error states manually. SWR replaces all of that with a single hook call that handles caching, deduplication, background refresh, and error retry automatically.
The code reduction is significant: a typical data-fetching component shrinks from 20+ lines to 3 lines.
Cache Key Design
The key uniquely identifies a request. Use the URL string for simple cases. For parameterized requests, use an array key: [url, params] where params is a stable object or string. Null disables the request.
A good key design ensures that related requests share cache entries and that unrelated requests do not collide.
Revalidation and Network Overhead
SWR is smart about revalidation: the dedupingInterval (default 2 seconds) prevents multiple revalidations from firing simultaneously if many components mount at once. Only one request goes out per key within the dedup window.
This prevents thundering-herd problems common in large component trees where many components fetch the same data.
SWR in the Context of Next.js
SWR is built by Vercel, the company behind Next.js, and the integration is seamless. getServerSideProps or getStaticProps can prefetch data, and SWR hydrates from that prefetched value on the client.
This gives you server-rendered initial HTML with automatic client-side revalidation on mount, combining the best of SSR and SWR.
When SWR Revalidates
Which of the following does NOT trigger SWR revalidation by default?
Lesson Recap
SWR implements the Stale-While-Revalidate strategy: serve cached data immediately, revalidate in background, update UI when fresh. It triggers revalidation on focus, reconnect, mutate, and polling. The global cache deduplicates requests across components, and the fetcher function is fully swappable.
This model dramatically simplifies data fetching code while delivering faster perceived performance.
Frequently asked questions
Is the “SWR Core Concepts: Stale-While-Revalidate” lesson free?
Yes — the full text of “SWR Core Concepts: Stale-While-Revalidate” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “SWR Core Concepts: Stale-While-Revalidate”?
Understand SWR's caching strategy: serve stale data immediately, revalidate in the background, update when fresh. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “SWR Core Concepts: Stale-While-Revalidate” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- SWR Core Concepts: Stale-While-Revalidate
- useSWR Hook: Fetching, Loading, and Error States
- Mutation and Optimistic Updates with SWR
- SWR vs React Query: Choosing the Right Tool