Course contentsShow
← Web Development
Lesson 1803 of 2,98233. Static Site Generation and JamstackFree lesson

ISR in Next.js Implementation

Practical implementation using Next.js getStaticProps with revalidate property and revalidatePath API routes.

ISR in Next.js Implementation

You'll learn: How to implement Incremental Static Regeneration in Next.js using getStaticProps with the revalidate property and API routes for on-demand updates.

Implementing ISR with getStaticProps

Next.js makes ISR straightforward using getStaticProps with a revalidate property. This property defines the revalidation interval in seconds—how long a cached page stays "fresh" before Next.js regenerates it in the background.

export async function getStaticProps() {
  const data = await fetchDataFromCMS();
  
  return {
    props: {
      content: data
    },
    revalidate: 60 // Regenerate page at most once per 60 seconds
  };
}

Think of revalidate as a "best before" timer. After 60 seconds, the next visitor triggers a background rebuild while still seeing the cached version (stale-while-revalidate).

On-Demand Revalidation with revalidatePath

For content that needs immediate updates without waiting for the interval, use revalidatePath in API routes. This is perfect for webhook integrations when your CMS publishes new content.

// pages/api/revalidate.js
export default async function handler(req, res) {
  // Verify request authenticity (check secret token)
  if (req.query.secret !== process.env.REVALIDATE_TOKEN) {
    return res.status(401).json({ message: 'Invalid token' });
  }

  try {
    await res.revalidate('/blog/my-post');
    return res.json({ revalidated: true });
  } catch (err) {
    return res.status(500).send('Error revalidating');
  }
}

Your CMS webhook calls this endpoint when editors publish changes, instantly invalidating the cache and triggering regeneration.

Combining Both Approaches

Use time-based revalidation as your safety net for periodic updates, and on-demand revalidation for immediate content changes—giving you both automatic freshness and editorial control.

Key Takeaway: Next.js ISR combines getStaticProps with revalidate for time-based regeneration and revalidatePath API routes for on-demand cache invalidation, delivering both performance and content freshness.