Skip to the solution
MediumSystem Design6 min read

What is ISR (Incremental Static Regeneration) in Next.js and how do you implement it?

418 views

The problem

Question presented to candidate: "Our blog has 20,000 posts. Rebuilding everything on each publish takes too long, and rendering every request is too expensive. How does ISR help, and how do you set it up?"

What a strong answer should cover:

  • ISR keeps a page static (prerendered, served from storage) but lets it be regenerated after deploy, without a rebuild.
  • Time-based: export const revalidate = n on the route, or next: { revalidate: n } on a fetch.
  • It is stale-while-revalidate: the first request after expiry still gets the old page and triggers a regeneration in the background.
  • On-demand: revalidatePath or revalidateTag mark content stale from a Server Action or webhook.
  • Pages not listed in generateStaticParams can be generated on first request and then cached, which is how ISR scales to many pages.

Clarifying questions expected:

  • "How stale may a post be after an edit?" β€” decides between time-based and on-demand.
  • "How many pages are there?" β€” decides how many to prerender at build time.

Code / implementation expected: Yes β€” a route with a revalidate time and a partial generateStaticParams list.

  • nextjs
  • isr
  • revalidate
  • static

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Read the code

Time-based ISR with a partial prerender list
// app/blog/[slug]/page.tsx
export const revalidate = 3600; // regenerate at most once an hour

export async function generateStaticParams() {
  const top = await getTopPosts(100);        // build only the popular posts
  return top.map((p) => ({ slug: p.slug }));  // the rest are generated on first visit
}

export default async function Post({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const post = await getPost(slug);
  return <article><h1>{post.title}</h1><div>{post.body}</div></article>;
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 78 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextMedium

How do you organize a large Next.js App Router project (colocation, private folders, route groups)?

Continue