-
Notifications
You must be signed in to change notification settings - Fork 26.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unnecessary top-level render from the router #37139
Comments
Originally, we added that behavior in #24189 to support query parameters in {
source: '/rewriting-to-auto-export',
destination: '/auto-export/hello?rewrite=1',
}, ...which is a valid use case. I’m wondering that if it makes sense to only trigger the router's subscription if the state doesn't change (with deep comparison): next.js/packages/next/shared/lib/router/router.ts Lines 1670 to 1682 in f9ed795
|
This may not be directly related to rewrites, causing unnecessary re-renders at the top level, but for re-renders in general. The linked issue describes the problem of changing the hash of a route, which per se should not result in a re-render of the entire app starting in _app. Indeed, it is a change made to the router context; it seems only a single property "asPath" changes. Re-render from the top is too expensive for inner page navigation, which should be as lightweight as possible. Maybe the linked issue can also be addressed to solve the current problem. |
Since the hash is part of the router context I don’t think it could be updated independently. But the suggestion to do a comparison first makes sense to me. |
Reopening since the change was reverted. |
Lands #37431 again, but it only solves the re-render issue completely for the middleware case (closes #38267), not the `rewrites` case (#37139). For `rewrites`, the blocker is `isReady` always being `false` initially and to match the markup on hydration we can't simply work around it without a re-render. Need to come up with another fix. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
hi, is there any update or ongoing/planned work on this? |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
If you have
rewrites
in next config:Then Next.js will cause a top-level re-render right after the initial navigation:
next.js/packages/next/client/index.tsx
Lines 118 to 121 in e57e275
This has negative performance implications (if you don't have a bunch of memos, it may render the entire app twice 🙃 ), and can lead to cascading updates in userland effects which in turn may cause Suspense boundaries to get dehydrated (#33861 (comment)).
Expected Behavior
I would not expect Next.js to call
replaceState
on every single initial navigation for every page if I have definedrewrites
in my config. (I only usedrewrites
for the RSS feed.) It is not obvious that just havingrewrites
in the config degrades the perf of every page in the application. In either case, even ifreplaceState
is called, I would not expect the router component to propagate a context update if nothing in the route context actually changed (the path is the same, etc).To Reproduce
Here's the branch:
https://github.com/gaearon/sc-bug-repro/tree/render-twice
yarn
,yarn build
,yarn start
.Notice that in production the effect runs twice. However, it should run once because there is no reason for the second render in production. If you remove
rewrites
from the config, it will run once as expected.The text was updated successfully, but these errors were encountered: