GetInitialProps get called twice when using Dynamic route causing Query params to be undefined
See original GitHub issueBug report
When trying to access a page like: posts/[id]
which is a page that uses a dynamic query param:
const Posts = ({ value }) => <h1>{value}</h1>
Posts.getInitialProps = async function (appContext) {
console.log('Calling Test getinitialProps with query', appContext.query.id)
const value = await fetch(`/api/posts/${id}`)
return {
value,
namespacesRequired: ['common']
}
}
export default Posts
I get the console log inside the getInitialProps
called twice with the second time the query param is undefined
which causes errors (especially for the API call which fails since the ID is undefined the second time)
I don’t know why this happens
To Reproduce
I have a custom _app.js
which looks like this:
class MyApp extends App {
static async getInitialProps (appContext) {
const { Component, ctx } = appContext
let pageProps = {}
if (Component.getInitialProps) {
console.log('calling getInitialProps')
pageProps = await Component.getInitialProps(ctx)
}
pageProps.configs = get(ctx, 'req.EnvConfigs', {})
return { pageProps }
}
...
System information
- OS: macOS
- Browser Chrome
- Version of Next.js: 9.4.2
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
GetInitialProps get called twice when using Dynamic route ...
I get the console log inside the getInitialProps called twice with the second time the query param is undefined which causes errors ...
Read more >Nextjs dynamic route is rendered twice - Stack Overflow
query.id); if I go to url/user/5 When I check console once I get undefined and then the correct id (5 in this case)....
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 >how to map routes to data and wait for the router to be ready.
In this video we'll create a dynamic route in NextJs in 4 different ways. The first method we'll use useEffect and useState and...
Read more >next - npm
Notice that to load data when the page loads, we use getInitialProps which is an async static method. It can asynchronously fetch anything...
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 FreeTop 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
Top GitHub Comments
Surprisingly, after spending hours trying to debug this! Found out that the page rerender twice, because of an Image component that had a
src
that gets retrieved from theredux store
and on refresh/mount, the store is not initialized, and the image ends up loadingundefined
as the src url, which causes the page to trigger twice (Weird behaviour)I had the same issue and as Sletheren said, this happens when the app tried to get an image, but it only happened when the image URL was undefined.