Next.jsMedium

What is Incremental Static Regeneration (ISR)?

ISR lets statically generated pages update after deployment without a full rebuild. Pages are served from cache, and after a time window or an on-demand trigger, Next.js regenerates them in the background using stale-while-revalidate, so visitors never wait for a rebuild.

How time-based ISR works

  1. A page is generated at build time or on its first request.
  2. Requests within the revalidation window get the cached page instantly.
  3. The first request after the window still gets the cached (stale) page, and triggers a background regeneration.
  4. Once regeneration succeeds, the new page replaces the cached one. If it fails, the old page keeps being served.

Flavors

  • Time-based: regenerate at most every N seconds.
  • On-demand: regenerate when content changes, via revalidateTag, updateTag or revalidatePath (App Router) or res.revalidate() (Pages Router), usually from a CMS webhook.
  • New paths: pages that were never built are generated on first request and then cached.

In each router

  • Pages Router: return { props, revalidate: 60 } from getStaticProps.
  • App Router with Cache Components: "use cache" plus cacheLife sets the timing (revalidate and expire in the profile), and cacheTag enables on-demand updates. Params not in generateStaticParams serve the App Shell first, and the full page is generated and cached in the background.
  • App Router without Cache Components: export const revalidate = 60 or fetch(url, { next: { revalidate: 60 } }).

Hosting

ISR needs a server (next start, a container or Vercel). It does not work with output: 'export'. When self-hosting several instances, configure a shared cache handler so instances do not serve different versions.

Code example

tsx
1// app/blog/[slug]/page.tsx - ISR with Cache Components
2import { cacheLife, cacheTag } from "next/cache";
3
4async function getPost(slug: string) {
5 "use cache";
6 cacheLife("hours"); // background refresh at most hourly
7 cacheTag(`post:${slug}`); // allow on-demand refresh from a webhook
8 const res = await fetch(`https://cms.example.com/posts/${slug}`);
9 return res.json() as Promise<{ title: string; body: string }>;
10}
11
12export async function generateStaticParams() {
13 return [{ slug: "hello-world" }]; // others are generated on first visit
14}
15
16export default async function Post({ params }: PageProps<"/blog/[slug]">) {
17 const { slug } = await params;
18 const post = await getPost(slug);
19 return <article><h1>{post.title}</h1><p>{post.body}</p></article>;
20}
21
22// app/api/revalidate/route.ts - CMS webhook
23// revalidateTag(`post:${slug}`, "max");

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app