# Time-based Revalidation (ISR)

> Automatically update static pages in the background on a timer.

Subject: Next.js | Lesson 26 of 65 | 6 min read
Interactive lesson: https://codewithanimation.in/nextjs/time-based-revalidation
Previous: [The Router Cache](https://codewithanimation.in/nextjs/router-cache.md) | Next: [On-Demand Revalidation (revalidatePath)](https://codewithanimation.in/nextjs/revalidate-path.md)

## Incremental Static Regeneration

Time-based Revalidation (historically known as ISR) allows you to update static pages after you've already built your site, without needing a full redeploy.

It caches the data just like a normal static fetch, but attaches an expiration timer to that specific cache entry.

## The best of both worlds

Traditionally, you had to choose: build the page statically for maximum speed (but it never updates), or render it dynamically on the server (it's always fresh, but slower).

Time-based revalidation gives you the speed of static rendering with the freshness of dynamic rendering. The server simply serves the cached file to the user instantly.

## Setting the timer

You can configure this per-request by adding an option to `fetch()`: `fetch('https://...', { next: { revalidate: 60 } })`.

Alternatively, you can apply it to an entire route by exporting a constant from your `page.tsx` file: `export const revalidate = 60;`.

## Common mistake: The 'Stale-While-Revalidate' trap

A massive point of confusion for beginners: when the timer hits zero, the cache does not immediately update itself.

Instead, the cache becomes 'stale'. The FIRST user to visit the page after the timer expires will STILL receive the old, stale data. However, their visit triggers Next.js to quietly rebuild the cache in the background. Only the NEXT user to visit the page will see the new data.

## Tip: When to use it

Time-based revalidation is perfect for data that doesn't need to be perfectly real-time: blog posts, product catalogs, or marketing pages.

Never use it for data that requires immediate consistency after a user action, like an e-commerce checkout or editing a user profile. For those, you must use On-Demand Revalidation.

## Try it: Stale-While-Revalidate

Watch the lifecycle of ISR. A request made BEFORE the timer expires simply returns the cached data. A request made AFTER the timer expires STILL returns the stale data, but triggers a background rebuild.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/time-based-revalidation

## Check yourself

### Question 1

A page is set to `{ revalidate: 60 }`. 65 seconds have passed. What happens when User A requests the page?

- A. They wait while the server builds the new page
- B. They see the old, stale data
- C. They see the new, fresh data instantly
- D. The page throws a 404 error

Correct answer: B

Next.js uses the 'stale-while-revalidate' pattern. User A's request at 65 seconds is instantly served the stale data, but triggers a background rebuild so User B will see the fresh data.

- Why not A: That would block the request, defeating the purpose of static generation.
- Why not C: The cache hasn't been rebuilt yet; it is only rebuilt in the background *after* this request.
- Why not D: The old data is still served until the background rebuild finishes.

### Question 2

How can you apply time-based revalidation to an entire route segment, affecting all components inside it?

- A. Export const revalidate = 60; from the page file
- B. Add { cache: 'no-store' } to every fetch
- C. Call router.refresh(60) on the client
- D. Add a staleTime config to next.config.js

Correct answer: A

Exporting `const revalidate = 60;` is the Route Segment Config option to set a default revalidation timer for all data fetching within that route.

- Why not B: That forces dynamic rendering, completely opting out of the cache.
- Why not C: router.refresh() does not accept a timer parameter and clears the client cache, not the server cache.
- Why not D: That configures the client-side Router Cache, not the server-side Data Cache.

## Remember this

- Time-based revalidation is also known as ISR.
- It allows static pages to update automatically in the background.
- The first request after the timer expires serves STALE data, but triggers a rebuild.
- Use `{ next: { revalidate: 60 } }` on fetch, or `export const revalidate = 60;` on routes.
- Do not use it for data requiring instant updates upon user action.
