Frontend System Design · Lesson 36 of 86 · 4 min
Incremental Static Regeneration (ISR)
This is how you explain getting the speed of a static site with the freshness of a server-rendered one. It shows you can make nuanced architectural trade-offs.
How to Explain the Stale-While-Revalidate Flow
ISR lets you update static pages after they've been built. It works by serving a cached (stale) page instantly, while regenerating a fresh one in the background.
This strategy is called "stale-while-revalidate". When a request comes in for a page:
- If a fresh, cached version exists, it's served immediately from the CDN.
- If the cached version is stale (older than the
revalidatetime), the stale version is still served immediately. The user gets a fast response. - Simultaneously, a background process kicks off to re-fetch data and render a fresh page.
- Once complete, the cache is updated. The next visitor gets the new version.
This avoids a full site rebuild for content changes and keeps user-facing performance high by never making a user wait for a server-side render on a stale page.
When to Use ISR (and When Not To)
Use ISR for content that needs to be fresh, but not real-time. The goal is "eventual consistency."
Good candidates:
- E-commerce product pages: Prices or stock levels change, but not every second. A 5-10 minute
revalidatewindow is often acceptable. - Blog posts or news articles: Allows for fixing typos or adding updates without a full redeploy.
- Marketing pages with CMS-driven content: A/B test copy can be updated without developer intervention.
Bad candidates:
- Stock tickers or live sports scores: The data is useless if it's even a few seconds old. Use client-side fetching with WebSockets or streaming.
- User-specific dashboards: This content is unique per user and cannot be statically cached. Use SSR or CSR.
In an interview, frame this as a business decision. A
revalidateof 60 seconds for a blog post is fine. For e-commerce stock levels, 300 seconds might be too long and lead to overselling. The "right" number depends on the business impact of stale data.
How to Configure Revalidation and Fallbacks
In Next.js, you enable ISR by returning a revalidate key in getStaticProps.
// pages/posts/[id].tsx
import { GetStaticProps, GetStaticPaths } from 'next';
interface Post {
id: string;
title: string;
content: string;
}
export const getStaticPaths: GetStaticPaths = async () => {
// Pre-build the first 10 posts for speed.
// const posts = await fetchTop10Posts();
// const paths = posts.map(post => ({ params: { id: post.id } }));
return {
paths: [ { params: { id: '1' } }, { params: { id: '2' } } ],
// Other post IDs will be generated on-demand.
fallback: 'blocking',
};
};
export const getStaticProps: GetStaticProps = async (context) => {
const { id } = context.params;
const res = await fetch(`https://api.example.com/posts/${id}`);
const post: Post = await res.json();
if (!post) {
return {
notFound: true,
};
}
return {
props: {
post,
},
// Revalidate this page at most once every 60 seconds.
revalidate: 60,
};
};
export default function PostPage({ post }: { post: Post }) {
// Render post...
return <h1>{post.title}</h1>;
}
The other critical piece is the fallback property in getStaticPaths. It determines how to handle pages that weren't pre-built at build time.
fallback: false: Any path not pre-generated returns a 404. This is pure SSG; no new pages can be added without a rebuild.fallback: true: Serves a fallback version of the page (e.g., a loading spinner) on the first request. The browser then fetches the JSON from a client-side call and renders the page. Good for UX if you have a nice loading state, but requires extra client-side work.fallback: 'blocking': The user's browser "waits" for the initial page to be generated on the server (like SSR). Subsequent requests serve the cached static page. This is simpler to manage thanfallback: trueand better for SEO, as the crawler sees the full content on the first visit.
The trap: Candidates often forget about fallback. Without fallback: 'blocking' or fallback: true, your site can't generate new pages (like a newly published blog post) on the fly. You're just revalidating the pages that existed at build time.
The Trade-offs vs. SSG and SSR
Choosing ISR means you've considered the alternatives. Here's how to articulate the comparison.
- vs. SSG: ISR offers better data freshness. The cost is slightly more infrastructure complexity (a running Node.js server environment is needed for revalidation) and the potential for serving stale data for a short period. Pure SSG is simpler and faster if content rarely changes.
- vs. SSR: ISR is much more scalable. Pages are served from a CDN edge, which is cheaper and faster than hitting a server-side render function for every single request. The trade-off is that content is not real-time; there's always a revalidation delay. SSR is for when every request must have the absolute latest data.
You're trading real-time freshness for a massive performance and scalability gain.
Try it
Create a new Next.js page that uses getStaticProps to fetch and display the current time from an API (like worldtimeapi.org). Set revalidate: 10. Deploy it to Vercel. Refresh the page every couple of seconds and watch how the displayed time only updates after the 10-second window has passed and a new request comes in.
Next: In Micro-Frontends: Core Principles, we'll look at how to break up the frontend itself into independently deployable pieces.
Lock it in — apply this lesson under interview conditions.