Static site generation (SSG) is one of the core features that makes Next.js powerful. However, traditional static sites often face the challenge of keeping content up-to-date without redeploying the entire application.
In this post, you'll learn:
ISR is a feature that allows you to update static content at runtime, combining the speed of static pages with the flexibility of dynamic content.
With ISR your application will have the ability to revalidate static pages after a specified time interval. In simpler words, this means you can serve pre-rendered content while keeping it updated without rebuilding your application.
With ISR, you can:
Incremental Static Regeneration (ISR) combines the benefits of static generation with the freshness of dynamic content.
When a user makes the first request to a page, Next.js generates it statically and caches the result. This initial generation happens on-demand rather than at build time, meaning pages are created only when they're actually needed. Here's how it works step by step:
After a specified time period, the next user request triggers a background regeneration, seamlessly updating the cached content.
Let’s build a simple blog that updates dynamically using ISR. This example uses the App Router.
// app/articles/page.js
export const revalidate = 10 // Set the revalidation interval to 10 seconds
async function getPosts() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts?_limit=5")
return res.json()
}
export default async function Articles() {
const posts = await getPosts()
return (
<div>
<h1>Latest Articles</h1>
<ul>
{posts.map((post) => (
<li key={post.id}>
<h2>{post.title}</h2>
<p>{post.body}</p>
</li>
))}
</ul>
</div>
)
}
In the above example the page will be regenerated every 10 seconds.
ISR is perfect for:
ISR in Next.js bridges the gap between static and dynamic content, offering a flexible and powerful way to build more sophisticated web applications. Whether you’re creating a blog, an online store, or a dashboard, ISR is a very cool feature that is worth exploring.
Interested in collaborating or have questions about my work? Feel free to reach out using the options below. I'm always excited about new opportunities and ready to discuss how we can bring your next project to life together. I look forward to hearing from you!