Page Router

Pre-rendering

  • Traditionally, we use useEffect to fetch the data to update the ui. However, useEffect is triggered after the initial functional component is rendered

  • Therefore, we cannot see the data from the source, which is not good for search engine optimization, so that we need pre-rendering

NextJS can do pre-rendering , which have 2 types - static generation and server-rendering

Static Site Generation

GetStaticProps

  • The html file with initial data will be generated during build time, so that in every user request, the initial content of web page will be the same

The building process
  • If we want to generate the page in every couple of time in order to keep the data of pre-rendering updated

GetStaticPath

  • For the dynamic page, such as [id].js, nextjs need to knows which id should pre-render

  • If the id doesn't included, 404 default error page will be returned if setting fallback is false

  • fallback can also set to be blocking

  • Apart from 1 and 2, other id will be rendered on the server before showing the page, after rendering, the new page will be cache

  • fallback can also to be true

  • Initially, the loading page will be shown, meanwhile, the relevant html and json will be generated on the background and then render the page. After finished, the page will be changed into full page

Server Side Rendering

  • The page will be rendered on the server for each user request, and request and response of the page can obtained through context

  • Request obtained can be used to determined whether the user can be authenticated to enter the page or not

SWR

  • used for client-side rendering

  • Here is the simple example with nextjs pre-rendering

Last updated

Was this helpful?