Open cyrilcabo opened 1 year ago
I ran into a similar issue and I just discovered that React 18 is testing conditional mounting and reusable state.
If strict mode is enabled, which is the default setting in Next.js, React automatically performs a double rendering of components. This double rendering is a deliberate approach to help developers adapt to and prepare for the integration of this new features.
In short we need to build our components and pages to be re-rendered multiple times and can no longer depend on things like an empty dependency array.
React 18 docs on the topic This video explains it in detail and how to properly clean up / detect mounts - https://youtu.be/MXSuOR2yRvQ?t=559
I had ran into the same issue in Production Mode (next start).
And found a discussion #43158.
When i try to delete generateStaticParams
, the page.tsx
render once!But the Cache-Control
changes to private, no-cache, no-store, max-age=0, must-revalidate
See also #52934
once for html, once for rsc
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue
https://github.com/cyrilcabo/nextjs-app-duplicate-rendering
To Reproduce
Run
yarn build
on the projectDuring build, you'll notice that for each page that's being built, two console.logs always run:
After building, you can try running
yarn start
.Visit any of the pages:
http://localhost:3000/test-path/slug-3
http://localhost:3000/test-path/slug-4
On the terminal, you'll see duplicate console.logs:
Describe the Bug
As you can see, there are multiple renders from the
body
orpage
, and this causes the page to call a third party API multiple times. We've had instances where we exceeded API limits because of this. This https://beta.nextjs.org/docs/data-fetching/caching#preload-pattern-with-cache didn't help either as the renders happen a few seconds after the other.Expected Behavior
There should only be one render per each page:
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
next start