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
App router causes unexpected page reload on popstate event #56636
Comments
Looks like this might be getting fixed by |
Yeah |
Thanks @timneutkens. The documentation at https://nextjs.org/docs/app/building-your-application/upgrading/from-vite#step-5-create-the-entrypoint-page led me to believe it should be possible to migrate a fully client-rendered SPA to Next.js. |
It is, but not when you were using react-router history integration, using react-router with memory integration has always worked though. With the new experimental flag both work. |
@timneutkens I love this 'feature.' I need to migrate 40-50 web apps using |
Confirmed fixed in 14.0.4 with |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/next-app-router-spa-reload-issue-dnzlt9
To Reproduce
npm run dev
To see my current workaround
layout.tsx
, uncomment the//if (type === "popstate"...
line.Current vs. Expected behavior
Current behavior
I've migrated an SPA from create-react-app to Next.js and referenced these instructions:
https://nextjs.org/docs/app/building-your-application/upgrading/from-vite
It's working fine in general, but I've noticed unwanted/unnecessary page reloads after the browser back button is used. These impact our app performance and analytics.
The SPA currently makes use of react-router-dom for client-side routing. I may look to incrementally migrate this to the Next.js app router in the future.
I think I've identified the culprit code here:
next.js/packages/next/src/client/components/app-router.tsx
Line 443 in 9b2f29e
Where the
popstate
event hasstate
and a falsy__NA
value, it assumesthe history entry was pushed by the 'pages' router
, but this is not necessarily the case.Expected behavior
app-router
more correctly identifies history entry pushed by pages router and ignores entries pushed 'outside' Next.js, or;pages
router, so thatfalse
can be returned to avoid this bug.Verify canary release
Provide environment information
Operating System: Platform: linux Arch: x64 Version: #1 SMP PREEMPT_DYNAMIC Sun Aug 6 20:05:33 UTC 2023 Binaries: Node: 16.17.0 npm: 8.15.0 Yarn: 1.22.19 pnpm: 7.1.0 Relevant Packages: next: 13.5.5-canary.4 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.2.2 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router, Routing (next/router, next/navigation, next/link)
Additional context
No response
The text was updated successfully, but these errors were encountered: