Incremental Static Regeneration
Exploring incremental static regeneration and common implementation patterns
1. The Problem
SSG has a fundamental limitation: the HTML is generated at build time and stays frozen until the next build. For a product catalog where prices update daily, a news site where articles are published hourly, or a documentation site where content changes weekly, this means users see stale data between builds.
The naive solution is to rebuild the entire site whenever data changes. For a site with 10,000 pages, a full rebuild takes minutes. You cannot rebuild every time a single product price changes. You need something more targeted.
The other naive solution is to switch everything to SSR. Every request gets fresh data. But now you lose all the performance benefits of static files. 10,000 product pages that could have been served from a CDN in 15ms now each require a server render and database query on every request.
ISR is Next.js’s answer to this tension. It keeps the static file performance of SSG while adding a background regeneration mechanism so pages can become fresh again without a full rebuild and without switching to SSR.
2. The Naive Attempt
A developer builds a news site with SSG. Articles are published several times per day. They add revalidate to their getStaticProps:
export async function getStaticProps({ params }) {
const article = await fetchArticle(params.slug);
return {
props: { article },
revalidate: 60, // regenerate every 60 seconds
};
}
They expect: every 60 seconds, the page checks for new content and updates itself. They are surprised when:
A new article is published. Users who visit the page within the next 60 seconds still see the old version. Users who visit at 61 seconds also see the old version. The page does not regenerate on a timer. It regenerates on request, and the first request after the 60-second window triggers regeneration in the background, while that first user still receives the stale page.
The mental model they had (timer-based regeneration) is wrong. The actual model (request-triggered background regeneration) has different implications for how fresh the data actually is.
3. Why That Breaks
The failure mode of misunderstanding ISR’s model: expecting ISR to proactively regenerate pages on a schedule, when it actually regenerates pages reactively on request after a staleness window expires.
If a page has revalidate: 60 and no user requests it for two hours, it is not regenerated. It sits as a stale static file. The staleness window expiring does not trigger regeneration. Only the combination of “staleness window expired” AND “a request arrives” triggers regeneration.
This is the stale-while-revalidate model, borrowed from HTTP caching. It has a specific behavior that must be understood to use ISR correctly:
The first request after the window expires does NOT get fresh data. It gets the stale page (served instantly from cache) and triggers background regeneration for the next user.
4. Next.js’s Mental Model
ISR combines two things: the static file performance of SSG with a staleness window that controls when background regeneration is triggered.
The contract: serve the cached static file to every user, instantly, from the CDN. When a request arrives after the staleness window has expired, serve the current cached file to that user anyway (instant response, no waiting), and simultaneously trigger a background re-render of the page using fresh data. When the background re-render completes, the new HTML replaces the cached file. The next user gets the fresh version.
One sentence: ISR serves stale content instantly while regenerating in the background, so users never wait for a rebuild but the data eventually becomes fresh after the staleness window.
5. Next.js’s Solution
The revalidate option in getStaticProps (Pages Router).
// pages/products/[id].js
export async function getStaticProps({ params }) {
const product = await fetchProduct(params.id);
return {
props: { product },
revalidate: 3600, // page can be stale for up to 1 hour
// After 1 hour + a request arrives → background regeneration
};
}
The revalidate value is in seconds. It is not “regenerate every N seconds.” It is “this page is considered fresh for N seconds after it was last generated. After N seconds, the next request triggers background regeneration.”
The precise ISR request lifecycle.
Build time:
next build runs getStaticProps for all static paths
HTML files generated and stored
Each file stamped with a "generated at" timestamp
Request arrives at T+0s (within revalidation window):
CDN serves cached HTML
User receives instant response
No regeneration triggered
Request arrives at T+3601s (after 1 hour window expired):
CDN serves cached HTML (stale, but instant)
Background regeneration triggered:
Next.js calls getStaticProps with fresh params
New HTML rendered
New file replaces old file in cache
User who triggered it still sees stale data
Request arrives at T+3602s (after regeneration completes):
CDN serves new HTML (fresh)
User receives fresh data instantly
The user who arrives at T+3601 sees stale data. The user who arrives at T+3602 sees fresh data. The regeneration window between T+3601 and T+3602 is typically under a second (the time to fetch data and render).
The revalidate option in the App Router (fetch-level).
In the App Router, ISR is controlled per fetch call rather than per page:
// app/products/[id]/page.js
async function ProductPage({ params }) {
const product = await fetch(`https://api.example.com/products/${params.id}`, {
next: { revalidate: 3600 }, // ISR: cache for 1 hour
}).then((r) => r.json());
return (
<div>
<h1>{product.name}</h1>
<p>${product.price}</p>
</div>
);
}
next: { revalidate: 3600 } is Next.js’s extension to the standard fetch API. It tells Next.js’s Data Cache to store this fetch result and consider it fresh for 3600 seconds. After 3600 seconds, the next request to this route triggers a background re-fetch and re-render.
Multiple fetch calls in the same route can have different revalidation windows:
async function ArticlePage({ params }) {
// Article content: revalidate every 24 hours
const article = await fetch(`https://api.example.com/articles/${params.slug}`, {
next: { revalidate: 86400 },
}).then((r) => r.json());
// Related articles: revalidate every hour
const related = await fetch(`https://api.example.com/articles/related/${params.slug}`, {
next: { revalidate: 3600 },
}).then((r) => r.json());
return <ArticleLayout article={article} related={related} />;
}
The page’s overall revalidation window is determined by the shortest revalidate value among all its fetch calls. If the article content has revalidate: 86400 but the related articles have revalidate: 3600, the page will be eligible for regeneration after 3600 seconds.
Route-level revalidation in the App Router.
You can also set revalidation at the route segment level, which overrides individual fetch configurations:
// app/products/[id]/page.js
// This applies to all fetches in this route segment
export const revalidate = 3600;
async function ProductPage({ params }) {
// Both fetches inherit revalidate: 3600
const product = await fetch(`https://api.example.com/products/${params.id}`).then((r) =>
r.json(),
);
const inventory = await fetch(`https://api.example.com/inventory/${params.id}`).then((r) =>
r.json(),
);
return <ProductLayout product={product} inventory={inventory} />;
}
On-demand revalidation: the ISR escape hatch.
Time-based ISR works for content that changes on a predictable schedule. For content where you know exactly when it changes (a CMS publishes a new article, an admin updates a price), waiting for the staleness window to expire wastes freshness. On-demand revalidation lets you invalidate specific pages immediately when their data changes:
// app/api/revalidate/route.js
import { revalidatePath, revalidateTag } from 'next/cache';
import { NextResponse } from 'next/server';
export async function POST(request) {
const { secret, path, tag } = await request.json();
// Protect the endpoint with a secret
if (secret !== process.env.REVALIDATION_SECRET) {
return NextResponse.json({ error: 'Invalid secret' }, { status: 401 });
}
if (path) {
revalidatePath(path); // invalidate a specific path
}
if (tag) {
revalidateTag(tag); // invalidate all fetches with this tag
}
return NextResponse.json({ revalidated: true });
}
Your CMS webhook calls this API route when content changes:
CMS: Article "Next.js ISR Explained" was updated
→ POST https://mysite.com/api/revalidate
{ "secret": "...", "path": "/blog/nextjs-isr-explained" }
Next.js: revalidatePath('/blog/nextjs-isr-explained')
→ Marks the cached HTML for that path as stale
→ Next request to that path triggers immediate regeneration
→ User who triggers it still sees stale HTML (same stale-while-revalidate model)
→ Second user sees fresh HTML
Tag-based revalidation for data-level invalidation.
revalidateTag is more powerful than revalidatePath: it invalidates all routes that fetched data with a specific tag, regardless of which pages those fetches appeared in.
// Tag fetches with a data identifier
async function ProductPage({ params }) {
const product = await fetch(`https://api.example.com/products/${params.id}`, {
next: {
revalidate: 3600,
tags: [`product-${params.id}`, 'products'], // multiple tags
},
}).then((r) => r.json());
return <ProductLayout product={product} />;
}
// When product 42 is updated in the CMS:
revalidateTag('product-42');
// Invalidates: /products/42, /products/42/reviews, /products/42/related,
// any other route that fetched with the 'product-42' tag
// WITHOUT needing to know which specific paths to invalidate
Tag-based invalidation decouples the CMS or admin system from knowledge of your URL structure. The CMS knows it updated “product 42.” Next.js knows which pages fetched “product 42” data. The mapping is maintained by the fetch tags.
6. Internals
How Next.js tracks revalidation state.
Next.js stores ISR metadata in its cache layer. Each cached route entry has:
{
html: '<html>...', // the pre-rendered HTML
pageData: { ... }, // the serialized props/RSC payload
headers: { ... }, // HTTP headers to send with the response
status: 200, // HTTP status code
revalidate: 3600, // revalidation interval in seconds
lastModified: 1704067200, // Unix timestamp of when this was generated
}
On each request, Next.js computes whether the entry is stale:
function isStale(cacheEntry) {
const age = Date.now() / 1000 - cacheEntry.lastModified;
return age > cacheEntry.revalidate;
}
If isStale returns true, Next.js serves the cached entry to the current request (the stale-while-revalidate behavior) and triggers background regeneration.
The background regeneration process.
Background regeneration is non-blocking with respect to the incoming request. Next.js forks a new execution path:
Incoming request → stale check → STALE
├── Serve cached HTML to user (immediate, non-blocking)
└── Background task:
1. Call getStaticProps or re-execute the Server Component
2. Render the component tree to HTML
3. Write new HTML to cache
4. Update lastModified timestamp
5. (In Vercel deployment: push to CDN edge nodes)
The background task does not block the request that triggered it. The user who triggered regeneration gets the old HTML. The regeneration happens asynchronously. When it completes, the cache entry is updated. Subsequent requests get the new HTML.
Preventing regeneration stampedes.
If 1,000 users request a stale page simultaneously, Next.js should not trigger 1,000 simultaneous background regenerations. It triggers one and queues the rest:
// Simplified deduplicated regeneration
const revalidationInProgress = new Set();
async function triggerRegeneration(path) {
if (revalidationInProgress.has(path)) {
return; // already regenerating, skip
}
revalidationInProgress.add(path);
try {
const newEntry = await regeneratePage(path);
cache.set(path, newEntry);
} finally {
revalidationInProgress.delete(path);
}
}
While regeneration is in progress for a path, all requests to that path receive the stale cached HTML. Only after the regeneration completes do new requests get the fresh HTML. This ensures the regeneration cost is paid exactly once regardless of concurrent traffic.
ISR in the App Router: the Data Cache.
The App Router’s ISR is built on Next.js’s Data Cache rather than the filesystem-based static HTML cache of the Pages Router. The Data Cache stores individual fetch results keyed by URL and cache options. Each entry has its own revalidation metadata.
When a route has multiple fetch calls with ISR, each fetch result is independently cached and independently revalidated. A page that fetches both article content (revalidate: 86400) and view count (revalidate: 60) will have its view count fetch revalidated every minute while the article content is revalidated only once per day, without re-fetching the article content on every view count revalidation.
This is more granular than the Pages Router’s per-page revalidation. The route’s full HTML is only regenerated when at least one of its data dependencies has been revalidated and returned new data.
7. Interview Traps
Trap 1: A page has revalidate: 60. It was last generated at 12:00:00. A user requests it at 12:01:05. What does that user see, and what happens next?
Answer
The user at 12:01:05 sees the stale HTML generated at 12:00:00. The revalidation window (60 seconds) expired at 12:01:00. The request at 12:01:05 arrives after the window expired, so it triggers background regeneration. But the stale-while-revalidate model means that triggering user still receives the old cached HTML immediately rather than waiting for the new render.
Next.js serves the cached response to the 12:01:05 user instantly (no waiting) and simultaneously begins regenerating the page in the background: fetching fresh data and rendering new HTML. When that background process completes (typically under 1 second), the cache is updated.
The next user who requests the page after the background regeneration completes will receive the fresh HTML. The user at 12:01:05 will have seen data that was up to 65 seconds old (the 60-second window plus the 5 seconds they arrived after expiry).
The key takeaway: ISR’s stale-while-revalidate model guarantees instant responses for all users at the cost of the first post-expiry user seeing stale data. It never makes users wait for regeneration.
Trap 2: What is the difference between revalidatePath and revalidateTag? When would you use each?
Answer
revalidatePath('/products/42') invalidates the cached HTML and data for a specific URL path. Every fetch result cached for that route is marked as stale. The next request to /products/42 triggers immediate regeneration. Use this when you know exactly which URL needs to be refreshed: an admin updates a specific page, a CMS webhook fires for a specific article.
revalidateTag('product-42') invalidates all cached fetch results that were tagged with 'product-42', regardless of which routes those fetches appeared in. This can invalidate multiple routes simultaneously: /products/42, /products/42/reviews, /products/featured (if product 42 is featured), and any other route that fetched data tagged 'product-42'. Use this when data is used across multiple routes and you want to invalidate all of them without knowing the complete list of affected URLs.
The practical choice: use revalidatePath for simple cases where the URL-to-data mapping is one-to-one. Use revalidateTag for data that appears in multiple routes (a product, an author, a category) where you want the invalidation to cascade to all affected pages automatically. revalidateTag requires discipline in tagging fetch calls but provides much more powerful invalidation targeting.
Trap 3: A developer sets revalidate: 0 on a page. What does this do?
Answer
revalidate: 0 means the page’s staleness window is zero seconds. Every request arrives after the staleness window has expired. The behavior is equivalent to SSR: every request triggers background regeneration, and the current user always receives stale data while the next user receives fresh data.
However, this is subtly different from SSR. With SSR (getServerSideProps or cache: 'no-store'), the current user’s request waits for fresh data before receiving a response. With revalidate: 0, the current user always receives the previous cached version instantly (stale-while-revalidate) and regeneration happens in the background.
For truly real-time data where the current user must see fresh data (user-specific dashboards, live prices where showing a stale price even briefly is unacceptable), revalidate: 0 is the wrong choice. Use cache: 'no-store' or getServerSideProps for genuine SSR. revalidate: 0 is effectively “ISR with zero staleness tolerance” which produces near-SSR behavior with different freshness guarantees than true SSR.
Self-Test
Q1. Explain the stale-while-revalidate model precisely. Who sees stale data, and who sees fresh data, in relation to when the revalidation window expires?
Answer
The stale-while-revalidate model works as follows. Every user who requests the page before the revalidation window expires receives the current cached HTML instantly. These users always see the most recently generated version of the page, which may be up to revalidate seconds old.
The first user who requests the page after the revalidation window expires also receives the current cached HTML instantly. This user sees stale data. Their request triggers background regeneration: Next.js fetches fresh data and renders a new HTML page asynchronously.
All users who request the page after the background regeneration completes receive the newly generated HTML. They see fresh data.
The staleness guarantee is: any user may see data that is at most revalidate seconds old plus the time required to regenerate the page (typically under a second). No user ever waits for regeneration. The freshness guarantee is: after the first request following window expiry plus regeneration time, all subsequent users see data that was fetched after the window expired.
Q2. A product catalog has 50,000 products. The top 1,000 products by sales volume receive 95% of traffic. Each product’s price updates once a day. Design an ISR strategy for this catalog.
Answer
Pre-render the top 1,000 products at build time using getStaticPaths with those 1,000 product IDs in the paths array and fallback: 'blocking' for the remaining 49,000.
Set revalidate: 86400 (24 hours) for all product pages, matching the price update frequency.
For the top 1,000 products, users get instant static file responses from CDN with data that is at most 24 hours old. This is correct because prices update once a day anyway.
For the remaining 49,000 products, the first user to request any specific product triggers on-demand generation (fallback: 'blocking', they wait once), which produces a static file cached for 24 hours. Subsequent users are served the cached file instantly.
Add on-demand revalidation via revalidateTag: tag each product fetch with product-{id}. When the nightly price update runs, call revalidateTag('product-{id}') for each updated product. This invalidates those pages immediately rather than waiting for the 24-hour window, ensuring prices are fresh within minutes of the nightly update rather than potentially 24 hours after it.
This strategy provides CDN-level performance for all products, correct price freshness after the nightly update, and does not require pre-building all 50,000 pages at build time.
Q3. What is the difference between ISR’s revalidation model and HTTP Cache-Control: stale-while-revalidate? Are they the same thing?
Answer
They are based on the same concept (serve stale while refreshing in the background) but implemented at different levels with different scopes.
HTTP Cache-Control: stale-while-revalidate is a browser and CDN cache directive. It tells intermediate caches (CDN nodes) and the browser to serve a stale cached response while asynchronously fetching a fresh one. The revalidation is driven by the HTTP caching layer, not by application code. It applies to any HTTP resource (HTML, JSON, images) and requires no server-side logic.
Next.js ISR is implemented at the application level, within Next.js’s server. It is not just a cache header: the revalidation actually re-executes your data fetching code and re-renders your React components on the server. The output is a completely regenerated HTML file with fresh data, not just a cached HTTP response served with a longer TTL. ISR knows how to run getStaticProps or re-execute Server Components. HTTP cache revalidation only knows how to repeat the original HTTP request.
Next.js also sets Cache-Control: s-maxage=REVALIDATE_SECONDS, stale-while-revalidate headers on ISR responses so that CDN nodes can implement the HTTP-level caching on top of Next.js’s server-level ISR. The two layers work together: the CDN handles geographically distributed caching, and Next.js handles the actual regeneration when the CDN decides it needs fresh content.