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:
- Run
npm run dev
and check the terminal console for multiple outputs of the console.log statement in thegetInitialProps
function ofpages/index.js
(“getInitialProps index”). - 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:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top 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 >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
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.