Using Next.js's Static Generation method
1 year ago
•
•
4 min read
In Next.js, there are two forms of pre-rendering, Static Generation and Server-side Rendering. We can apply these methods for each page individually.
The Static Generation method fetches the page's data on build-time, and renders static HTML files. To apply the Static Generation method, we need to export a getStaticProps
method from our page:
1// pages/index.tsx23import type { GetStaticProps } from 'next'45import type { Lesson } from 'types/lesson'6import { fetchLessons } from 'utils/lessons'78type Props = {9 lessons: Lesson[]10}1112const Home = ({ lessons }: Props) => {13 return (14 <ul>15 {lessons.map((lesson) => (...))}16 </ul>17 )18}1920export const getStaticProps: GetStaticProps<Props> = async (context) => {21 const lessons = await fetchLessons()2223 const props: Props = {24 lessons25 }2627 return {28 props29 }30}3132export default Home
For our Home page, Next.js will execute the fetchLessons
method asynchroniously, and pass the lessons array as a prop in our Home
component. If there is no data for the given query, we need to return notFound: true
instead of the props. If the lessons data changes and we want to update the page, we can either rebuild our website, or use the Incremental Static Regeneration method.
In our getStaticProps
method we can also obtain the context
, which holds data like:
params
: the route parameters for pages that use dynamic routespreview
: a boolean which istrue
if the page is in the Preview Mode, otherwiseundefined
previewData
: an object that holds the preview data set bysetPreviewData
locale
: the active locale, if you've enabled Internationalized Routinglocales
: an array that contains all of the localesdefaultLocale
: the default configured locale
Incremental Static Regeneration
#The ISR method is an extension of the Static Generation method. We can enable ISR if we provide a revalidate
property in our getStaticProps
result:
1export const getStaticProps: GetStaticProps<Props> = async (context) => {2 return {3 props: {...},4 revalidate: 60 * 60,5 }6}
The revalidate
property will tell Next.js to "revalidate" our data maximum one time in the given timeframe (in our case is 1 hour, 60 seconds times 60).
Building pages with dynamic routes
#Since the Static Generation happens on build-time, if we have a page that uses Dynamic Routes we also need to export the getStaticPaths
method. The getStaticPaths
method returns a list of paths that have to be rendered to HTML at build-time.
1import type { GetStaticProps, GetStaticPaths } from 'next'23...45export const getStaticPaths: GetStaticPaths = async () => {6 const lessons = await fetchLessons()78 return {9 paths: lessons.map({ slug } => ({ params: { slug } })),10 fallback: false11 }12}
The paths
key determines which paths will be pre-rendered. If for example we had 3 lessons, Next.js will pre-render the following URLs:
/lessons/getting-started
/lessons/create-pages
/lessons/create-dynamic-routes
For each lesson, Next.js will execute the getStaticProps
method. That pre-renders every page and generates static HTML files for them.
The fallback
key is a boolean key that we must include in our getStaticPaths
result. If it's set to false
, then any paths that are not returned by the getStaticPaths
method will result in a 404 page. If it's set to true
, Next.js will render a "fallback" page while it statically generates the HTML and JSON (this includes running the getStaticProps
method). When it's done, the page will receive the brand new data in its props and it will render its contents. At the end of the process, Next.js will add the new path to the list of pre-rendered pages.
If our page supports a fallback, we can use Next.js's router to check if Next.js wants us to render a fallback page:
1import { useRouter } from 'next/router'23const Home = () => {4 const router = useRouter()56 if (router.isFallback) {7 return <div>Loading...</div>8 }910 ...11}
If we don't want to display a Loading page, we can set the fallback
property in getStaticPaths
to 'blocking'
. This will make the browser wait for Next.js to pre-render the HTML.
Subscribe to my newsletter ✉️
Get emails from me about web development, content creation, and whenever I publish new content.
Subscribe on Substack