Course contentsShow
← React
Lesson 1804 of 2,39642. Data Fetching LibrariesFree lesson

SWR's Core Philosophy: Stale-While-Revalidate

Understanding SWR's namesake pattern and how it prioritizes instant data display while fetching fresh content in the background.

SWR's Core Philosophy: Stale-While-Revalidate

What you'll learn: How SWR's namesake pattern delivers instant UX by showing cached data first while fetching fresh updates behind the scenes.

The Core Idea

SWR (Stale-While-Revalidate) is named after an HTTP cache invalidation strategy, but applies it to React data fetching. The philosophy is simple yet powerful: show users stale (cached) data immediately, then revalidate (fetch fresh data) in the background, and update when ready.

This creates an instant, optimistic user experience — users never wait for loading spinners after the first fetch.

How It Works

Think of SWR like a news app that shows you yesterday's headlines instantly while checking for breaking news in the background. You're never staring at a blank screen, and when new content arrives, it seamlessly updates.

The three-step dance:

  1. Return cached data immediately (if available)
  2. Send fetch request in background (revalidation)
  3. Update UI with fresh data when it arrives

This differs from traditional approaches where you either show loading states on every navigation or risk showing stale data indefinitely.

Why This Philosophy Matters

Users perceive apps as faster because they see content instantly. Most of the time, slightly stale data (a few seconds old) is perfectly acceptable while fresh data loads. SWR bakes this pattern into its DNA — every query follows this flow by default.

The library assumes your data is temporarily stale and worth showing, but fresh data is always better. This mirrors how humans actually browse: we'd rather see yesterday's weather forecast instantly than wait 2 seconds for today's, then get the update.

Key Takeaway: SWR prioritizes perceived performance by instantly displaying cached data while transparently fetching updates, creating fluid UX without loading states after initial fetch.