Blog>
Snippets

Incremental Static Regeneration with revalidate Option

Provide an example of how to use the revalidate option in getStaticProps to enable Incremental Static Regeneration, allowing pages to be regenerated after deployment.
export async function getStaticProps(context) {
  // Fetch data from an API or any data source
  const data = await fetchData();

  // Return the fetched data as props and revalidate every 10 seconds
  return {
    props: { data },
    revalidate: 10, // In seconds
  };
}
This code defines a 'getStaticProps' function for a page in a Next.js application. The function fetches data asynchronously and returns it as props for the page. The 'revalidate' option is set to 10 seconds, which means that the page will be regenerated at most every 10 seconds if there are requests coming in. This enables Incremental Static Regeneration, allowing the page to update dynamically while still benefiting from static generation.