We now have a youtube channel. Subscribe!

Next.js | Shallow Routing

Next.js Shallow Routing

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

In Next.js, shallow routing mean navigation to same page but no calls to getStaticProps, getServerSideProps, and getInitialProps.

To perform shallow routing, we make use of Router with shallow flag set to true.


Following below is a simple example

Update index.js file in pages directory using the following content -

import Router from 'next/router'
import Head from 'next/head'

function HomePage(props) {
   return (
            <title>Welcome to Next.js!</title>
         <div>Welcome to Next.js!</div>
         <span onClick={() => Router.push('/?counter=1', undefined, { shallow: true })}>Reload</span>
         <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


Open localhost:3000 in any browser of your choice and then click on the Reload link and you will see the following result -

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 Api Routes.

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