Incremental static regeneration

Intermediate · 12 min

ISR updates static pages without a full rebuild. See the classic revalidate model and how Cache Components replaces it with cacheLife and tags.

Before thisGenerating static paramsRevalidation and cache tags

ByteGen · 12 min

Incremental static regeneration

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by ByteGenReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • ISR serves prerendered static pages and regenerates them in the background, so a large site stays fast without rebuilding everything for each content change.
  • In the classic model, export const revalidate = 60 plus generateStaticParams means the first request after 60 seconds still gets the stale page while a fresh one builds.
  • With Cache Components, exporting revalidate is a build error; time-based refresh moves to cacheLife and on-demand refresh to cacheTag with updateTag or revalidateTag.
  • For a URL not listed in generateStaticParams, Cache Components serves the App Shell at once, renders the URL-specific content, and caches the completed result for later visitors.
  • Whether an unlisted path can show a fallback first depends on Partial Prefetching: await params inside Suspense with it on, and outside Suspense with it off.

The problem ISR solves

A static site is fast and cheap. It is also frozen until the next build. A blog with 20,000 posts cannot rebuild everything every time someone fixes a typo, and an editor does not want to wait for a deploy.

Incremental static regeneration is the compromise. Pages are built ahead and served as static files. When one gets old, Next.js rebuilds just that page in the background and swaps it in. The docs describe it as updating static content without rebuilding the site, reducing server load, and handling large numbers of pages without long builds.

The classic model

Before Cache Components, ISR was a route-level setting:

// app/blog/[id]/page.tsx
export const revalidate = 60

export async function generateStaticParams() {
  const posts = await fetch('https://api.vercel.app/blog').then((r) => r.json())
  return posts.map((post: { id: number }) => ({ id: String(post.id) }))
}

export default async function Page({ params }: PageProps<'/blog/[id]'>) {
  const { id } = await params
  const post = await fetch(`https://api.vercel.app/blog/${id}`).then((r) => r.json())
  return <main><h1>{post.title}</h1></main>
}

The docs’ step-by-step:

  1. generateStaticParams lists the pages to prerender.
  2. next build prerenders each.
  3. Requests to those pages are instant.
  4. After 60 seconds, the next request still returns the cached, now-stale page.
  5. The cache is invalidated and a new version starts generating in the background.
  6. Once it succeeds, the next request gets the new page.
  7. An unlisted id that exists is generated on demand; one that does not exist returns a 404.

I built this exact shape with cacheComponents off on 16.4.6 and the route table marked /legacy/1 with ● (SSG, uses generateStaticParams). Two details: the lowest revalidate across a route’s layouts and pages decides the route’s frequency, and the value must be statically analyzable, so 600 works and 60 * 10 does not.

You could refresh on demand too, with revalidatePath or revalidateTag in a Server Action or Route Handler.

ISR with Cache Components

With cacheComponents: true, the route config options are not allowed. I tried export const revalidate = 60 and next build failed: “Route segment config “revalidate” is not compatible with nextConfig.cacheComponents. Please remove it.” The replacement is in the migration guide: wrap the data or UI in use cache and set cacheLife ('hours' instead of revalidate = 3600).

export default async function Page({ params }: PageProps<'/blog/[id]'>) {
  const { id } = await params
  const post = await getPost(id)
  return <main><h1>{post.title}</h1></main>
}

async function getPost(id: string) {
  'use cache'
  cacheLife('hours')
  cacheTag(`post-${id}`)
  return db.post.findFirst({ where: { id } })
}

Revalidate on demand with the tag, as in revalidation and cache tags. The generateStaticParams rules from the previous lesson still apply: at least one param, and no dynamicParams export.

What happens to a URL you did not list

This is where the new model is more interesting than the old. Each route is split into an App Shell, the generic reusable part that does not depend on URL data, and the param-specific prerenders for the URLs you listed.

  • A visit to a listed URL is served as a fully prerendered page.
  • A visit to an unlisted URL gets the App Shell at once, with the param-specific parts behind their fallbacks. Next.js then renders the real page in the background and stores the completed result. Later visitors to that URL get the finished version.

The docs call this the Cache Components equivalent of ISR and of fallback: true from the Pages Router. It depends on Partial Prefetching being enabled. With it, you can await params inside <Suspense> and the fallback appears instantly. Without it, the first request for an unlisted path has to wait, and you should await params outside any Suspense boundary, including a loading.tsx, to get an ISR entry for that URL. Awaiting inside Suspense without Partial Prefetching leaves visitors on the shared fallback.

// the layout passes the promise down and awaits inside the boundary
<Suspense fallback={<div>Loading…</div>}>
  <CategoryHeader params={props.params} />
</Suspense>

Caveats

Cache entries persist differently by host. The docs note that serverless platforms typically do not keep in-memory entries across requests, while build-time prerenders are reused. If your host does not store use cache output at runtime, regeneration will not behave like a long-lived server. Check the deployment lessons and your provider.

What the video covers

ByteGen’s December 2024 video covers, per its chapters: an introduction to ISR, time-based revalidation, on-demand revalidation, and tag-based revalidation. That is the classic model. It is from a small channel with a low view count, and it predates Next.js 16, so expect fetch-level options like next: { revalidate } that this lesson moves to cacheLife. The Vercel runner-up, from January 2024, has a section on revalidating static data with ISR and uses Next.js 14 defaults.

Where this fits

Rendering decides when HTML is made. One piece of that HTML matters to search engines and social cards in particular. Next: metadata and SEO.

Check yourself

  1. 01In the classic ISR model, what does the visitor see on the first request after the revalidate time passes?

    The stale cached page, because Next.js serves it immediately and regenerates a new version in the background, which later visitors receive.

  2. 02Why does export const revalidate = 60 fail with Cache Components on?

    Route segment config such as revalidate is not compatible with cacheComponents, so time-based freshness is expressed with use cache and cacheLife on the data or UI instead.

  3. 03What is the App Shell, and when is it served?

    It is the reusable, URL-independent prerender of a route, served instantly for a URL whose params were not listed in generateStaticParams, then upgraded with the real content for later visits.

  4. 04How do you trigger ISR on demand when an editor publishes?

    Tag the cached data with cacheTag and call updateTag in a Server Action, or revalidateTag with a profile in a Route Handler, or use revalidatePath.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark