next.js: `Link` component is buggy when used in app router
Link to the code that reproduces this issue
https://github.com/mehulmpt/nextjs-app-router-link-bug
To Reproduce
- Clone the GitHub repository
- Run
npm run bugcheckcommand in your terminal - Visit
http://localhost:3000
Current vs. Expected behavior
Current behavior:
The links rightly point to /link?productid=1 / /link?productid=2 … and so on. However, when you click on it, they redirect you to the wrong page!
Expected behavior:
Redirect works properly
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000
Available memory (MB): 16384
Available CPU cores: 8
Binaries:
Node: 18.20.4
npm: 8.19.2
Yarn: 1.22.19
pnpm: 8.7.6
Relevant Packages:
next: 15.1.3 // There is a newer version (15.1.6) available, upgrade recommended!
eslint-config-next: N/A
react: 19.0.0
react-dom: 19.0.0
typescript: 5.7.3
Next.js Config:
output: export
Which area(s) are affected? (Select all that apply)
Navigation
Which stage(s) are affected? (Select all that apply)
Other (Deployed), Vercel (Deployed), next start (local)
Additional context
I spent 20 minutes debugging this for one of our clients today and finally figured out this is a bug with Next.js itself. The reproduction code above is extremely minimal and does not include anything else other than the required code.
The behavior is very weird, the link shown is correct in the browser preview on the bottom left but the moment it is clicked, it takes you to the wrong page. I am also attaching the video that showcases it:
- In the first half of the video I show you wrong behavior
- In the second half of the video I disable javascript and show you how it should actually work
https://github.com/user-attachments/assets/d64a0fe2-57bf-4d5d-b081-faa5220b7cee
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 15 (10 by maintainers)
I’m having a similar issue with the link component when linking to a new search param value, but my issue is that there is no navigation at all. I can see fetches happening in my network tab but no navigation is happening on the front-end. Refreshing fixes the issue, but only for that pages pagination/search param navigation.
I filed https://github.com/vercel/next.js/issues/76911 which @ztanner marked as a duplicate. (Thank you!)
Here’s a minimal repro with videos: https://github.com/statico/repro-nextjs-link-get-routes-prefetch
This appears to be related to prefetching and only happens in production (i.e. with
npm build && npm start, but not withnpm dev) which adds to the confusion. Addingprefetch={false}to your<Link>tag should fix it. (Not that this is obvious or expected.)