ISR gives you the speed of static HTML with the freshness of dynamic rendering, by regenerating pages in the background when they grow stale.
Request → CDN → cached HTML (instant)
│
├─ if "fresh" → done
└─ if "stale" → serve cached + kick off background regen
↓
updated HTML stored for next visitor
Set revalidate on the page (in seconds):
1// app/news/page.tsx
2export const revalidate = 60; // revalidate at most every 60sOr on a fetch:
1const data = await fetch("https://api.news.com/headlines", {
2 next: { revalidate: 60 },
3});You don't have to wait for the timer:
1// app/api/revalidate/route.ts
2import { revalidatePath, revalidateTag } from "next/cache";
3
4export async function POST(req: Request) {
5 const { path, tag, secret } = await req.json();
6 if (secret !== process.env.REVALIDATE_SECRET) {
7 return new Response("Unauthorized", { status: 401 });
8 }
9
10 if (path) revalidatePath(path);
11 if (tag) revalidateTag(tag);
12
13 return Response.json({ revalidated: true });
14}The CMS posts to this endpoint when a writer publishes. Within milliseconds, the new article is live worldwide.
ISR's promise: no visitor ever waits for a regeneration. The stale page is served instantly; regen happens after the response is flushed.
Exception: the very first time a page is requested (cache miss), the user waits for the initial render. Use generateStaticParams to prebuild your hot pages.
1// lib/data/posts.ts
2"use cache";
3import { cacheTag, cacheLife } from "next/cache";
4
5export async function getPost(slug: string) {
6 cacheTag("post", `post:${slug}`);
7 cacheLife("hours");
8 return db.post.findUnique({ where: { slug } });
9}When the writer edits a post:
1revalidateTag(`post:${slug}`); // wipes the cache for that post onlySubsequent visitors see the updated content. Visitors to other posts continue to hit cache.
1// next.config.ts
2const config: NextConfig = {
3 cacheHandler: require.resolve("./cache-handler.js"),
4 cacheMaxMemorySize: 0, // disable in-memory; rely on shared layer
5};Popular shared backends: Redis, S3, Cloudflare KV.