Static Site Generation (SSG) in Next.js with `getStaticProps`

InstructorXiaoru Li

Share this video with your friends

Send Tweet

Since version 9.3, Next.js added a new life-cycle method getStaticProps for improved Static Site Generation (SSG) support, allowing us to fetch data and create static pages at build time. Together with the new data fetching method getServerSideProps used for Server-Side Rendering (SSR), Next.js allows us to clearly separate code intended for SSG or SSR. This is, in many cases, much better than the classic hybrid approach of getInitialProps.

In this lesson, we will import a JSON file from the file system in the getStaticProps life-cycle method of a Table of Contents page, so that we can render links based on the content we have, and statically generate the page at build time. At the end of this lesson, we will export our app using the next export command to a fully static website.

After this lesson, you will be able to optimize pages that don't need to be updated often on every request by only pre-generating them at build-time, utilizing the new SSG feature.