Course contentsShow
← Web Development
Lesson 1795 of 2,98233. Static Site Generation and JamstackFree lesson

Stale-While-Revalidate Pattern

How ISR serves cached pages instantly while regenerating updated versions in the background for future requests.

Stale-While-Revalidate Pattern

You'll learn how ISR delivers instant page loads by serving cached content while quietly updating it in the background.

The Core Concept

The Stale-While-Revalidate (SWR) pattern is the magic behind how Incremental Static Regeneration provides both speed and freshness. Here's how it works:

When a visitor requests a page, ISR immediately serves the existing cached (static) version—even if it's slightly outdated ("stale"). The user sees content instantly with no waiting. Meanwhile, in the background, the server triggers a regeneration of that page with fresh data. Once the new version is ready, it replaces the old cached copy. The next visitor gets the updated version.

The Restaurant Menu Analogy

Imagine a restaurant with printed menus. When a customer asks for a menu, you hand them the current printed version instantly—you don't make them wait while you reprint it with today's specials. But after they sit down, you send the menu to the printer with updates. Tomorrow's customers get the refreshed version. Everyone gets instant service, and the menu stays reasonably current.

How It Benefits Your Application

This pattern solves a fundamental tension between static and dynamic content:

  • First request: Serves cached page instantly (milliseconds)
  • Background: Regenerates page with fresh data (seconds)
  • Subsequent requests: Serve the newly updated cached page

You get SSG's blazing speed with data that stays relatively fresh. Unlike pure SSG where you rebuild everything, or SSR where every request is slow, SWR gives you the best of both worlds.

Key Takeaway: Stale-While-Revalidate serves cached pages instantly for immediate performance, then regenerates updated versions in the background so future visitors see fresh content—combining static speed with dynamic freshness.