the Blog's Logo
the Blog's Logo
Pre-render Pages Using Static Generation with Next.jsimage
Photo by Pawel Czerwinski on Unsplash

Static Generation is one of the core rendering patterns in Next.js. With this approach, pages are built at build time rather than on every request. This means your content is pre-rendered into HTML before users ever visit your site, leading to faster load times, better performance, and improved SEO.

In practice, Static Generation works especially well for content that does not change frequently, such as blog posts, documentation, marketing pages, or product listings. Instead of generating the page dynamically for each visitor, Next.js prepares everything in advance and serves the same optimized version to everyone.

How Static Generation Works

When you use Static Generation in Next.js, the framework runs your data-fetching logic during the build process. The result is a fully rendered HTML page that can be cached and delivered instantly via a CDN. This reduces server workload and ensures consistent performance even under heavy traffic.

In Next.js, this is typically achieved using a special function called getStaticProps. This function runs only on the server at build time, fetches any necessary data, and passes it to your page as props.

export async function getStaticProps() {
  const data = await fetch('https://api.example.com/posts').then(res =>
    res.json()
  );

  return {
    props: {
      posts: data,
    },
  };
}

Because this code runs before deployment, your page is generated once and then reused for all visitors.


Static Generation with Dynamic Routes

Static Generation is not limited to single pages. You can also pre-render pages that depend on dynamic parameters, such as blog posts with different IDs or product pages.

For this, Next.js provides getStaticPaths, which tells the framework which dynamic routes should be generated at build time.

export async function getStaticPaths() {
  const posts = await fetch('https://api.example.com/posts').then(res =>
    res.json()
  );

  return {
    paths: posts.map(post => ({
      params: { id: post.id.toString() },
    })),
    fallback: false,
  };
}

This allows Next.js to create a separate static page for each blog post, ensuring that all content is ready before users request it.

Incremental Static Regeneration

One limitation of pure Static Generation is that content cannot change unless you rebuild your site. To solve this, Next.js introduced Incremental Static Regeneration (ISR).

With ISR, you can tell Next.js to regenerate a page in the background after a certain amount of time, keeping your content up to date without requiring a full redeploy.

export async function getStaticProps() {
  const data = await fetch('https://api.example.com/posts').then(res =>
    res.json());

  return {
    props: { posts: data },
    revalidate: 60,
  };
}

In this example, the page will be regenerated at most once per minute when a user visits it, ensuring fresh content while maintaining the performance benefits of Static Generation.


When to Use Static Generation

Static Generation is best suited for pages that are mostly the same for all users and do not rely on real-time data. Examples include:

  • Blog posts

  • Documentation pages

  • Marketing content

  • Landing pages

  • Product listings that rarely change

If your page depends heavily on user-specific data or frequent updates, Server-Side Rendering or Client-Side Rendering might be a better choice.


Final Thoughts

Static Generation is one of the most powerful features of Next.js. It allows you to build fast, scalable, and SEO-friendly applications with minimal effort. By understanding when and how to use it, you can significantly improve both the performance and reliability of your website.