-
Notifications
You must be signed in to change notification settings - Fork 27.1k
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
fix: Don't reset shallow URL updates on prefetch #58297
fix: Don't reset shallow URL updates on prefetch #58297
Conversation
Hey @franky47 -- thanks for working on this. We'd like to still keep the existing devtools sync behavior as we rely on this for debugging. Let me know if you'd like to take a stab at that otherwise I can take a look next week! |
@ztanner thanks for the quick feedback, I'll have a look throughout the weekend and I'll let you know. Ultimately, this is only a quick fix, a better fix for this behaviour would be to have access to shallow routing in the app router, to mirror the pages router API. It would allow keeping the internal I don't know if that's a thing you guys at Vercel have in the pipeline, but I'd love to take a closer look at how to do this (it would shave a good chunk of hacks off my library). |
@ztanner I have restored the Redux devtools sync function, with the caveat that it now runs when a state update resolves to a new reducer state, rather than when an action is dispatched. It seemed more inline with the previous synchronous behaviour. There were a few places in the app router render body where a Promise reducer state would be unwrapped to parts of the app router state. I'm kind of new to the use of |
Note: it's currently conflicting with #58335, which may make this PR irrelevant, will investigate further on the impacts on next-usequerystate. |
Stats from current PRDefault BuildGeneral
Client Bundles (main, webpack)
Legacy Client Bundles (polyfills)
Client Pages
Client Build Manifests
Rendered Page Sizes
Edge SSR bundle Size
Middleware size
Next Runtimes
Diff detailsDiff for page.jsDiff too large to display Diff for edge-ssr.jsDiff too large to display Diff for 199-HASH.jsDiff too large to display |
Failing test suitesCommit: 1b6d315
Expand output● app dir › HMR › should not cause error when removing loading.js
Read more about building and testing Next.js in contributing.md. |
@anonkey there's actually a lot that changed behaviourally in 12.0.3-canary.6, I'm still looking into how to properly integrate it into nuqs. See 47ng/nuqs#394 Thanks for the conflict res, I'll take a look at your changes. |
The WHS flag does fix the issue described here, but it's still present when the flag isn't used, so this PR still makes sense for baseline behaviour. @anonkey |
Conflicts resolved. @ztanner could you take a look please? |
Between 14.0.2-canary.6 and 14.0.2-canary.7, a change was introduced in #56497 that turned the Redux store state into Promises, rather than a synchronous state update. This caused the `sync` function used to send state update to the Redux Devtools to be recreated on every dispatch, which in turn, by referential instability, caused the `HistoryUpdater` component to re-render and trigger a `history.replaceState` with no particular change, but with the internal `canonicalUrl`. When an app does a soft/shallow navigation by calling history methods directly (currently the only way to do shallow search params updates in the app router), these changes would have been overwritten by any prefetch (eg: hovering or mounting a Link), which is usually a no-op for the navigation state. This commit removes the `sync` function for the Redux devtools, as it cannot function as-is: actual state updates need to be awaited and forwarded somewhere else in the router if this behaviour is to be maintained, and should be decoupled from history calls to prevent side-effects.
- Add missing types for sync function & HistoryUpdater props - Make the `sync` function referentially stable - Unwrap reducer state only once in app router
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes looks good, thanks for working on this!
Nice thank you |
Description
Between 14.0.2-canary.6 and 14.0.2-canary.7, a change was introduced in #56497 that turned the Redux store state into a Promise, rather than a synchronous state update.
This caused the
sync
function -- used to send state updates to the Redux Devtools -- to be recreated on every dispatch, which in turn, by referential instability, caused theHistoryUpdater
component to re-render and trigger ahistory.replaceState
with no particular change, but with the internalcanonicalUrl
.When an app does a soft/shallow navigation by calling history methods directly (currently the only way to do shallow search params updates in the app router), these changes would have been overwritten by any prefetch (eg: hovering or mounting a Link), which is usually a no-op for the navigation state.
This PR changes the
sync
function to take the state as an argument rather than as a closure. The whole app router state is also unwrapped only once, and fed to the HistoryUpdater. Changes to its contents made by reducers will cause the HistoryUpdater effect to re-run, triggering history updates and a call to the sync function.Context
I maintain
next-usequerystate
, which is used in the Vercel dashboard, and which is impacted by this change (see#388).
History
@timneutkens introduced the
sync
function and the whole Redux devtools reducer in #39866, with the note:If a different direction is needed to keep sending
RENDER_SYNC
actions to Redux devtools, I'll be happy to rework this PR to move thesync
function into the action queue.Changes
start
mode as prefetch links are disabled in development. Test was verified to fail from next@>=12.0.2-canary.7 without the fix.