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 is called multiple times using Express/Fastify

See original GitHub issue
  • [ x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

A page’s getInitialProps should only be called once per page load.

Current Behavior

Sometimes getInitialProps is called multiple times in one page load.

Steps to Reproduce (for bugs)

Minimal example app (almost identical to the next example app “custom-server-express”): https://github.com/approots/next-express-issue

To reproduce:

  1. Run npm run dev and check the terminal console for multiple outputs of the console.log statement in the getInitialProps function of pages/index.js (“getInitialProps index”).
  2. Or click the link to “post 2”, refresh the page, then load the index page again (localhost:3000/), and check the terminal and see multiple console.logs of “getInitialProps index”. This also happens in production.

Context

This means that API calls are being duplicated.

Your Environment

Tech Version
next ^5.1.0
node 8.9.4
OS Windows
browser Chrome
express ^4.16.3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
timneutkenscommented, Apr 21, 2018

getInitialProps is only ran once per render. The only thing that I could see is that sometimes HMR kicks in and reloads the page really fast while booting up. But that’s not unexpected.

Here’s the code: https://github.com/zeit/next.js/blob/canary/server/render.js#L79

Note that the correct way to test per-render is to do curl http://localhost:3000, since then there are no other factors.

3reactions
approotscommented, Apr 19, 2018

next

Read more comments on GitHub >

github_iconTop Results From Across the Web

getInitialProps is called multiple times using Express/Fastify
A page's getInitialProps should only be called once per page load. Current Behavior. Sometimes getInitialProps is called multiple times in one ...
Read more >
react redux - Nextjs 7.0.2 getInitialProps execute multiple times
getInitialProps is only run once per render. Sometimes HMR(hot module replacement) kicks in and reloads the page really fast while booting up.
Read more >
getInitialProps - Data Fetching - Next.js
getInitialProps receives a single argument called context , it's an object with the following properties: pathname - Current route. That is the path...
Read more >
Express Middleware Calls Multiple Times - ADocLib
Once this is done you start the router object via: const router useRouter getInitialProps is called multiple times using Express/Fastify hot 2 Module...
Read more >
What is Next.js getInitialProps and getServerSideProps? | refine
Next.js has two functions for server-side rendering ... Any data you are fetching in getInitialProps is returned as an object and is used...
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