On-demand Revalidation in Next.js
📖 Scenario: You are building a blog with Next.js. You want to update the blog posts page when new posts are added without rebuilding the whole site manually.
🎯 Goal: Create a Next.js page that shows a list of blog posts. Add an API route that triggers on-demand revalidation to update the page when new posts are added.
📋 What You'll Learn
Create a Next.js page at
app/posts/page.tsx that fetches and displays a list of posts.Create a mock data array called
posts with exact post titles.Add a configuration variable
revalidatePath with the path to revalidate.Create an API route at
app/api/revalidate/route.ts that calls Next.js revalidatePath to refresh the posts page.Use
export const revalidate = 0 in the posts page to enable on-demand revalidation.💡 Why This Matters
🌍 Real World
On-demand revalidation lets websites update static pages instantly when content changes, without rebuilding the whole site. This is useful for blogs, news sites, and e-commerce.
💼 Career
Understanding on-demand revalidation is important for Next.js developers to build fast, dynamic sites that stay up-to-date with minimal delay.
Progress0 / 4 steps