vercel / next.js

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

Unexpected full page reloads when transitioning between pages router views (in conjunction with app router and middleware) #64880

Open raphaelsaunier opened 7 months ago

raphaelsaunier commented 7 months ago

Link to the code that reproduces this issue

https://github.com/raphaelsaunier/nextjs-pages-router-app-router-middleware-repro

To Reproduce

  1. Set up a multi-tenant/domain Next.js pages router app, borrowing the architecture from the Vercel Platforms Starter Kit example
  2. Introduce new views using the app router
  3. Try to navigate between views that use the pages router app

Current vs. Expected behavior

Navigating between two pages that share the same layout (whether using the app router or pages router) should not cause a full re-render of the page (exemplified in the repro with an artificial delay to render the navigation component).

Navigating between app router views (i.e. /a and /b) always works as expected, however navigating within pages router views (i.e. /x and /y) consistently causes a full re-render in one of the following circumstances:

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.12.0
  npm: 10.5.0
  Yarn: 1.22.21
  pnpm: 8.5.1
Relevant Packages:
  next: 14.2.2 // Latest available version is detected (14.2.2).
  eslint-config-next: 14.2.2
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware, Pages Router

Which stage(s) are affected? (Select all that apply)

next dev (local), Vercel (Deployed)

Additional context

This has been a blocking issue for as long as I can remember and prevents us from gradually migrating a multi-tenant Next.js app and start using app router.

raphaelsaunier commented 7 months ago

Possibly related to #54231 since it could also be because caused by a path overlap (c.f. shared path prefix /domains/[domain] in repro)

raphaelsaunier commented 6 months ago

Possible duplicate of #53067 as well.