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.

GetInitialProps get called twice when using Dynamic route causing Query params to be undefined

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
Sletherencommented, May 26, 2020

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 the redux store and on refresh/mount, the store is not initialized, and the image ends up loading undefined as the src url, which causes the page to trigger twice (Weird behaviour)

2reactions
hiro1107commented, Oct 15, 2020

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.

Read more comments on GitHub >

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

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