vercel / next.js

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

Query parameters incorrect when navigating from App Router to Pages Router #72884

Open ledbetterljoshua opened 3 days ago

ledbetterljoshua commented 3 days ago

Link to the code that reproduces this issue

https://github.com/ledbetterljoshua/link-bug-reproduction

To Reproduce

Live Demo

https://link-bug-reproduction.vercel.app/

Repository

https://github.com/ledbetterljoshua/link-bug-reproduction

Steps to reproduce

  1. Start on the home page (App Router)
  2. Click any of the "pages route" links
  3. Observe that regardless of which link you click, you always get the query parameter from the first link (link=1)

Current vs. Expected behavior

Current behavior

Each Link component should navigate to the Pages Router page with its specified query parameter intact:

Actual behavior

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.0.0: Thu Jun 20 20:38:33 PDT 2024; root:xnu-11215.0.115.501.3~1/RELEASE_ARM64_T8112
  Available memory (MB): 24576
  Available CPU cores: 8
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: 1.22.17
  pnpm: N/A
Relevant Packages:
  next: 15.0.3 // Latest available version is detected (15.0.3).
  eslint-config-next: 15.0.3
  react: 19.0.0-rc-66855b96-20241106
  react-dom: 19.0.0-rc-66855b96-20241106
  typescript: 5.6.3
Next.js Config:
  output: N/A

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

Navigation, Pages Router

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

Vercel (Deployed)

Additional context

Description

When navigating from a page in the App Router to a page in the Pages Router using Next.js Link components, query parameters are not being properly maintained. The first query parameter encountered becomes "sticky" and is used for all subsequent navigations to the Pages Router page, regardless of the actual query parameters specified in the Links and in the DOM.

Additional observations

Environment

Notes

This appears to be a client-side navigation issue, as:

  1. Server-side navigation (new tabs) works correctly
  2. The DOM shows the correct href attributes on the links, but the navigation results are incorrect

Would appreciate guidance on whether this is a known issue or if there's a recommended workaround while we gradually migrate from Pages Router to App Router.

laugharn commented 2 days ago

@ledbetterljoshua if you know ahead of time that a URL is in pages router and not app, you can pass prefetch={false} to the link and it will behave as expected. Tested it here: https://link-bug-reproduction-isskh3bjx-laugharns-projects.vercel.app

ledbetterljoshua commented 1 day ago

@laugharn wow awesome, that works. Interesting to note that if any of the links to the pages router do not have prefetch={false} then the ones with it set still do not work. But this is super helpful. Thanks!

laugharn commented 1 day ago

A maybe unpopular opinion but I often find myself creating a link component with prefetch={false} by default and opt into prefetching as opposed to vice versa.