Next.js: Static Site Generation (SSG) with Incremental Static Regeneration (ISR)

Search for a command to run...

No comments yet. Be the first to comment.
The Problem Wasn’t Triage Most AI workflows in software engineering still keep the human directly in the middle of triage. The AI might help write code. It might explain a stack trace. It might summar

There’s been a steady undercurrent of people switching tools lately. Copilot to Claude. Claude to something else. Codex quietly entering more workflows. A lot of confidence in different directions, of

It’s been said to me that at times my responses can come across as a “no” first-unintentionally, of course, but real all the same. Not long ago, someone on my team shared an idea they were exploring. I began with a caution, meaning to help them avoid...

The opening sentence of any message often carries more weight than everything that follows. I’ve learned this the hard way. Recently, a report shared something he wanted to pursue. I was supportive — I really was — but my first response came as a cau...

One of the things I’ve always been comfortable with is admitting when I don’t know something. To me, it’s one of the real distinctions between the more and less experienced engineers — and honestly, people in general. When you’re earlier in your care...

Next.js: Static Site Generation (SSG) with Incremental Static Regeneration (ISR). This allows you to build static pages that can be updated after deployment without needing a full rebuild.
Let's say you're building a blog where the posts are stored in a CMS (like Sanity or a local JSON file). You can fetch the blog posts at build time, but with ISR, you can update them when new data is available.
Here’s how you can implement it:
In the /app folder (using Next.js App Router), create dynamic pages for your blog posts:
/app
/blog
[slug]
page.tsx
// blog/[slug]/page.tsx
import { GetStaticProps, GetStaticPaths } from 'next';
// Simulated blog data (replace with actual CMS/API call)
const posts = [
{ id: 1, slug: 'first-post', title: 'My First Post', content: 'Hello world!' },
{ id: 2, slug: 'second-post', title: 'Another Post', content: 'More content here!' },
];
type Post = {
id: number;
slug: string;
title: string;
content: string;
};
export async function generateStaticParams() {
return posts.map((post) => ({
slug: post.slug,
}));
}
// Page component
const BlogPost = ({ post }: { post: Post }) => {
return (
<div>
<h1>{post.title}</h1>
<p>{post.content}</p>
</div>
);
};
// ISR configuration
export const generateMetadata = async ({ params }: { params: { slug: string } }) => {
const post = posts.find((p) => p.slug === params.slug);
return { title: post?.title || 'Blog Post' };
};
// Fetch data for each post
export async function getPostData(slug: string): Promise<Post | undefined> {
return posts.find((post) => post.slug === slug);
}
// Generate static params at build time
export default async function Page({ params }: { params: { slug: string } }) {
const post = await getPostData(params.slug);
if (!post) {
// Render a 404 page if post is not found
return <div>Post not found</div>;
}
return <BlogPost post={post} />;
}
In the getPostData function, we set a revalidate time (e.g., 60 seconds). This allows the static page to be updated periodically without requiring a full rebuild.
export async function generateStaticParams() {
return posts.map((post) => ({
slug: post.slug,
}));
}
// Inside Page component
export const revalidate = 60; // Revalidate data every 60 seconds
At build time, the blog posts are statically generated.
If new posts are added or old posts are updated in your CMS, the ISR mechanism will re-fetch and update the page after 60 seconds when a new request is made.
This gives you the benefit of fast static pages with up-to-date content.
Faster Pages: Since pages are statically generated, they load quickly.
Auto-Updates: With ISR, you don't need to manually rebuild your site when content changes; Next.js handles it for you.
This combination of SSG and ISR is extremely powerful for any site where you want a balance of static performance and dynamic updates, such as blogs, e-commerce, or news websites.