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
- A page is generated at build time or on its first request.
- Requests within the revalidation window get the cached page instantly.
- The first request after the window still gets the cached (stale) page, and triggers a background regeneration.
- 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,updateTagorrevalidatePath(App Router) orres.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 }fromgetStaticProps. - App Router with Cache Components:
"use cache"pluscacheLifesets the timing (revalidateandexpirein the profile), andcacheTagenables on-demand updates. Params not ingenerateStaticParamsserve the App Shell first, and the full page is generated and cached in the background. - App Router without Cache Components:
export const revalidate = 60orfetch(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 Components2import { cacheLife, cacheTag } from "next/cache";34async function getPost(slug: string) {5 "use cache";6 cacheLife("hours"); // background refresh at most hourly7 cacheTag(`post:${slug}`); // allow on-demand refresh from a webhook8 const res = await fetch(`https://cms.example.com/posts/${slug}`);9 return res.json() as Promise<{ title: string; body: string }>;10}1112export async function generateStaticParams() {13 return [{ slug: "hello-world" }]; // others are generated on first visit14}1516export 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}2122// app/api/revalidate/route.ts - CMS webhook23// 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.