DEV Community

Cover image for Getting Started with Next.js: Part 5 - Static Site Generation and Incremental Static Regeneration
Dipak Ahirav
Dipak Ahirav

Posted on • Edited on

Getting Started with Next.js: Part 5 - Static Site Generation and Incremental Static Regeneration

๐Ÿš€ Check Out My YouTube Channel! ๐Ÿš€

Hi everyone! If you enjoy my content here on Dev.to, please consider subscribing to my YouTube channel devDive with Dipak. I post practical full-stack development videos that complement my blog posts. Your support means a lot!

Introduction

Welcome to Part 5 of our Next.js series! Today, we're delving into Static Site Generation (SSG) and Incremental Static Regeneration (ISR), two powerful features that leverage the best of static and dynamic rendering techniques. These methods provide optimized performance and flexible data fetching strategies, making your web applications faster and more efficient.

What is Static Site Generation (SSG)?

Static Site Generation is a method where HTML pages are generated at build time. This means that all pages are compiled into static files when you build your application. These static pages can then be served from a CDN, which reduces the load on your server and significantly improves the site's load time.

Implementing SSG with getStaticProps

  1. Create or Update a Page:
    • Use the getStaticProps function to fetch data at build time. For example, create a new file in the pages directory named blog.js.
  2. Add the following code:
   export async function getStaticProps() {
     const res = await fetch('https://api.example.com/posts');
     const posts = await res.json();

     return {
       props: { posts }, // will be passed to the page component as props
     };
   }

   function Blog({ posts }) {
     return (
       <div>
         <h1>Blog</h1>
         <ul>
           {posts.map(post => (
             <li key={post.id}>{post.title}</li>
           ))}
         </ul>
       </div>
     );
   }

   export default Blog;
Enter fullscreen mode Exit fullscreen mode
  • This function fetches blog posts from an API at build time and passes them as props to the Blog component.

What is Incremental Static Regeneration (ISR)?

Incremental Static Regeneration allows you to update static content after you've built your site. It enables you to use static-generation on a per-page basis, without needing to rebuild the entire site. This is useful for content that changes frequently but not instantaneously.

Implementing ISR with getStaticProps

  1. Modify getStaticProps to Enable ISR:

    • You can add the revalidate option to getStaticProps to enable ISR. This tells Next.js how often it needs to regenerate the page in seconds:
     export async function getStaticProps() {
       const res = await fetch('https://api.example.com/posts');
       const posts = await res.json();
    
       return {
         props: { posts },
         revalidate: 10, // In seconds
       };
     }
    
  • In this example, Next.js will attempt to re-generate the page every 10 seconds if there are requests for the page.

Benefits of SSG and ISR

  • Enhanced Performance: SSG serves pre-built HTML and static files which can be quickly delivered from a CDN.
  • SEO Friendly: Static files are fully rendered at build time, making them readily indexable by search engines.
  • Scalability: SSG with ISR scales effortlessly under heavy load, as it reduces the number of server-side computations required per request.

Conclusion

Static Site Generation and Incremental Static Regeneration are powerful strategies in Next.js that combine the benefits of static and dynamic rendering. By utilizing these methods, you can enhance the performance, SEO, and scalability of your Next.js applications.

Series Index

Part Title Link
1 Getting Started with Next.js: Setting Up Your Project Read Part 1
2 Next.js: Creating Pages and Routing Read Part 2
3 Next.js: API Routes Read Part 3
4 Next.js: Server-Side Rendering (SSR) Read Part 4
5 Next.js: Static Site Generation and ISR Read Part 5
6 Next.js: Advanced Configuration and Optimization Read Part 6
7 Next.js: Internationalization and Localization Read Part 7
8 Next.js: State Management and API Integration Read Part 8

Stay tuned for the next part of our series, where we will explore more advanced Next.js features and optimization techniques.

Top comments (1)

Collapse
 
dipakahirav profile image
Dipak Ahirav

Next part -> Part - 6