question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any dynamic route renders twice in browser

See original GitHub issue

Bug report

Describe the bug

Any dynamic route is rendered twice in the browser. The entire <App /> seems to be rendered twice.

To Reproduce

  1. Create route [pid].js in pages
  2. Add code
const Page = () => {
  console.log('rendered')
  return <div />
}

export default Page
  1. npm run dev
  2. Open browser to localhost:3000/anypage
  3. See ‘rendered’ printed twice

Expected behavior

Dynamic routes should only render once in the browser like any other page.

System information

macOS 10.15.4 Next.js 9.3.4 Chrome 80.0.3987.163 + Firefox 74.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
rickchristiecommented, Mar 25, 2021

I just want to use query string and found out that the page is rendered twice whenever page is accessed with query string. Even if the page is not using useRouter, etc.

For static websites, we can use React.memo to prevent the second re-render, if the props hasn’t changed.

See: https://reactjs.org/docs/react-api.html#reactmemo

Hope this helps someone!

5reactions
ijjkcommented, Apr 19, 2020

Hi, this is the expected behavior for automatically statically optimized dynamic pages.

When a dynamic page does not leverage getServerSideProps, getStaticProps, or getInitialProps it is auto prerendered at build time and since the dynamic page’s values can’t be known during this stage the initial HTML doesn’t isn’t rendered with them so during the first render on the client we can’t provide these values either to make sure to match the initial HTML and not break hydration.

After hydration has occurred we parse the values from the URL on the client and trigger another render to allow the parsed values to be used now that hydration has occurred.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nextjs dynamic route is rendered twice - Stack Overflow
What is going on here, is there a way to fix it? This is an expect behaviour for dynamic routes, see github.com/vercel/next.js/issues/12010 for ......
Read more >
Dynamic routes rendered twice : r/nextjs - Reddit
Dynamic routes rendered twice. I have a dynamic route in pages/user/[id].jsx. my [id].jsx. contains console.log(useRouter().query.id);.
Read more >
Routing in Next.js – How to Set Up Dynamic Routing with Pre ...
In this tutorial, you'll learn how to set up dynamic routing in Next.js. You'll also learn about pre-rendering and why it's important.
Read more >
Shallow Routing - Next.js
Shallow routing allows you to change the URL without running data fetching methods again, that includes getServerSideProps , getStaticProps , and ...
Read more >
Programmatically Navigate Using React Router - Stack Abuse
Programmatically Navigate Using React Router · About = () => { return ( · { render } from "react-dom"; import { BrowserRouter, Routes,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found