We now have a youtube channel. Subscribe!

Next.js | Pre-Rendering

Next.js Pre-Rendering


Hello folks! welcome back to a new section of our tutorial on Next.js. In this tutorial, we are going to be studying about Next.js Pre-rendering.

Pre-rendering allows a user to see the page before any of the JavaScript code is loaded. Next.js supports two types of pre-rendering -

  • Static Generation - This type of pre-rendering generates the HTML page at build time. This pre-rendered HTML is sent on each request. This method is useful for marketing websites, help documentation websites, ecommerce product listing websites, blogs.
  • Server Side Generation - This type of pre-rendering can generate the HTML page on each request. This method is ideal when a HTML page content can vary with each request.

Per Page Pre-Rendering

Next.js allow to set pre-rendered method for each page where most of the pages follows static generation and other pages will utilize server side rendering.

Static Generation without Data

Static generation can be performed without data in which case, the HTML pages will be ready without need to prefetch the data and start rendering. Data can be fetched later or on request. This technique helps in showing users an User Interface without any data in case data takes long to come.


Static Generation with Data

Static generation can be done using data in which case, Html pages won't be ready until data is fetched, as HTML may be dependent on data. Each component has a very unique method called getStaticProps which can be used to fetch and pass data as props of the page so that the page can render according to passed props.

getStaticProps() method runs at buid time in production and runs for every request in dev mode.

Let us create a simple example to illustrate the same.

In this tutorial guide, we'll create an update index.js and first.js page to cause a server hit to get data.

Let us update the nextjs project which was used in Global CSS Support tutorial.

Update index.js file in pages directory to use getStaticProps(). This method will be called per request.

import Link from 'next/link'
import Head from 'next/head'

function HomePage(props) {
   return (
      <>
         <Head>
            <title>Welcome to Next.js!</title>
         </Head>
         <div>Welcome to Next.js!</div>
         <Link href="/posts/first"><a>First Post</a></Link>
         <br/>
         <div>Next stars: {props.stars}</div>
         <img src="/logo.png" alt="WebDesignTutorialz Logo" />
      </>	    
   )
}

export async function getServerSideProps(context) {
   const res = await fetch('https://api.github.com/repos/vercel/next.js')
   const json = await res.json()
   return {
      props: { stars: json.stargazers_count }
   }
}

export default HomePage


Update first.js file in pages directory to use getStaticProps() method. This method will be called once.

import Link from 'next/link'
import Head from 'next/head'
import Container from '../../components/container'

export default function FirstPost(props) {
   return (
      <>
         <Container>
            <Head>
               <title>My First Post</title>
            </Head>
            <h1>My First Post</h1>
            <h2>
               <Link href="/">
                  <a>Home</a>
               </Link>
               <div>Next stars: {props.stars}</div>
            </h2>
         </Container>
      </>	  
   )
}

export async function getStaticProps() {
   const res = await fetch('https://api.github.com/repos/vercel/next.js')
   const json = await res.json()
   return {
      props: { stars: json.stargazers_count }
   }
}

Start Next.js Server

Run the following command below to start the server -

npm run dev
> nextjs@1.0.0 dev \Node\nextjs
> next

ready - started server on http://localhost:3000
event - compiled successfully
event - build page: /
wait  - compiling...
event - compiled successfully
event - build page: /next/dist/pages/_error
wait  - compiling...
event - compiled successfully

Output

Open localhost:3000 in any browser of your choice to see the following result -


Click on First Post link.



Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about Next.js Routing.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain