Blog>
Snippets

Incremental Static Regeneration (ISR) with Dynamic Routes

Provide an example of ISR with dynamic routes, demonstrating how to use fallback: 'blocking' for efficient static regeneration of pages.
import { useRouter } from 'next/router';
import { GetStaticPaths, GetStaticProps } from 'next';

export default function Product({ product }) {
  // Body of your product component
}

export const getStaticPaths: GetStaticPaths = async () => {
  // Here you would usually fetch an array of products or paths
  const paths = [{ params: { id: '1' } }, { params: { id: '2' } }];

  return {
    paths,
    // Use fallback: 'blocking' to serve a server-side rendered page
    // if the static page is not yet generated
    fallback: 'blocking',
  };
};

export const getStaticProps: GetStaticProps = async (context) => {
  const { params } = context;
  const product = await fetchProduct(params.id); // Replace with actual data fetching

  return {
    props: {
      product,
    },
    // Specify the revalidation time (in seconds)
    revalidate: 10, // Regenerate the page after 10 seconds
  };
};
This is a Next.js page component with Incremental Static Regeneration (ISR) implemented for products with dynamic routes. 'getStaticPaths' is used to define which paths to pre-render at build time, with fallback: 'blocking' to SSR any paths not pre-rendered. 'getStaticProps' fetches data for an individual product and specifies 'revalidate' to update the static page periodically. 'fetchProduct' should be replaced with the actual fetching logic for each product page.