Incremental Static Regeneration
- Static pages (SSG) can be auto updated after site is built, without rebuilding the entire site.
ISR Workflow
- In project, use
getStaticProps with the revalidate option.
// This function runs at build time and regenerates pages
export async function getStaticProps() {
// Fetch data from an API
const res = await fetch('<https://jsonplaceholder.typicode.com/posts?_limit=5>');
const posts = await res.json();
return {
props: { posts }, // Passed to the page component
revalidate: 30, // Regenerate page every 30 seconds
};
}
// pages/posts/[id].js
export default function Post({ post }) {
return (
<div>
<h1>{post.title}</h1>
<p>{post.body}</p>
</div>
);
}
- At build time, the framework generates static HTML pages just like SSG.
- Example:
/blog/post1.html and /blog/post2.html.
- Pages are uploaded to the CDN (Content Delivery Network).
- Users visiting the page get the pre-rendered HTML instantly.
- Pages can be regenerated in the background based on:
- A time interval (e.g., revalidate every 60 seconds).
- On-demand triggers (like a CMS webhook).
- While regeneration happens, users still get the old static page, so there’s no downtime.
- Once the page is regenerated, the new HTML replaces the old page on the CDN.
- Future visitors see the latest version without rebuilding the whole site.
Advantages of ISR
- Fast initial load ✅
- Like SSG, HTML is served instantly from the CDN.
- SEO-friendly ✅
- Pages are static and fully rendered for search engines.
- Dynamic content support ✅
- Pages can update automatically without rebuilding the entire site.
- Minimal server load ✅
- Only updated pages are regenerated, not the whole site.