Any dynamic route renders twice in browser
See original GitHub issueBug report
Describe the bug
Any dynamic route is rendered twice in the browser. The entire <App /> seems to be rendered twice.
To Reproduce
- Create route [pid].js in
pages - Add code
const Page = () => {
console.log('rendered')
return <div />
}
export default Page
npm run dev- Open browser to
localhost:3000/anypage - 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:
- Created 3 years ago
- Comments:24 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.memoto prevent the second re-render, if the props hasn’t changed.See: https://reactjs.org/docs/react-api.html#reactmemo
Hope this helps someone!
Hi, this is the expected behavior for automatically statically optimized dynamic pages.
When a dynamic page does not leverage
getServerSideProps,getStaticProps, orgetInitialPropsit 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.