We now have a youtube channel. Subscribe!

Next.js | Routing

Next.js Routing


Hello folks! welcome back to a new section of our tutorial on Next.js. In this tutorial, we will be studying about Next.js Routing.

Next.js uses file system based router. Every time we add any page to pages directory, it's automatically available via URL.

Rules of Next.js Router

Following below are the rules of this router -

  • Index Routes - An index.js file present in a folder maps to root of a directory. For example -
    • pages/index.js maps to '/'.
    • pages/posts/index.js maps to '/posts'.
  • Nested Routes - Any nested folder structure in pages directory becomes router url automatically. For example -
    • pages/settings/dashboard/about.js maps to '/settings/dashboard/about.
    • pages/posts/first.js maps to '/posts/first'.
  • Dynamic Routes - We can also use named parameter to match URL. Use brackets for the same. For example -
    • pages/posts/[id].js maps to '/posts/:id' where we can use URL like '/posts/1'.
    • pages/[user]/settings.js maps to '/posts/:user/settings' where we can use URL like '/abc/settings'.
    • pages/posts/[...all].J's maps to '/posts/*' where we can use any URL like '/posts/2022/apr/'.

Page Linking

Next.js allow linking of pages on client side using Link react component.

Properties of Link react component

Following are the properties of Link react component -

  • href - name of the page in pages directory. For example /posts/first which refers to first.js available in pages/posts directory.

Let's create an example to demonstrate the same.

In this example, we will update index.js and first.js page to make 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 using the following content -

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


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 Dynamic 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