vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.04k stars 26.71k forks source link

Page router - Rapid shallow routing breaks router events - routeChangeComplete does not fire #51485

Open samcarton opened 1 year ago

samcarton commented 1 year ago

Verify canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.15.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.7-canary.1
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5

Which area(s) of Next.js are affected? (leave empty if unsure)

Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/samcarton/nextjs-shallow-router-stuck

To Reproduce

Describe the Bug

If you happen to perform a shallow routing operation while another shallow route operation is still in progress, any further shallow routes fail to trigger the routeChangeComplete event. This can sometimes be reset by a non-shallow routing operation, though it seems to behave differently based on whether the page is on a dynamic route or not.

Expected Behavior

Rapid consecutive shallow routes should not cause the routeChangeComplete event to stop firing entirely. This seemed to work in NextJS 12.X.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Daniel-Parton commented 1 year ago

I'm also getting this issue

samcarton commented 1 year ago

Still having this issue on latest canary.

    Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.15.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.8-canary.7
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5
    Next.js Config:
      output: N/A
Marcosld commented 1 year ago

We are also having this issue. In our case we solved it by awaiting the previous navigation. We have a singleton promise and we add new navigations to it. Something like:

let navigationPromise = Promise.resolve();

const navigate = (newUrl) => {
  navigationPromise = navigationPromise.then(() => {
    // Perform navigation here with next/router
  });
}

On the other hand, I think the new app router doesn't return a promise when navigating :( I would like to know if this happens also :(

Enngage commented 1 year ago

Same here. Happens when quickly switching routes with soft navigation & dynamic routes.