Open
Description
Link to the code that reproduces this issue
https://github.com/sgarrity/nextjs-anchor-link-testcase
To Reproduce
- Run the simple create-next-app example at https://github.com/sgarrity/nextjs-anchor-link-testcase
- Click on the "Anchor Example" link (or manually add a
#example
fragment at the end of the home page URL - Click the "Go to Foobar" link (to /foobar)
- Click the browser Back button
Current vs. Expected behavior
Note how the URL shows the home URL with the fragment (http://localhost:3000/#anchor
) but the Foobar (/foobar
) page is still rendered in the browser, not the home page as intended.
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.2.0: Fri Dec 6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000
Available memory (MB): 32768
Available CPU cores: 10
Binaries:
Node: 20.11.1
npm: 10.2.4
Yarn: 1.22.22
pnpm: 9.15.4
Relevant Packages:
next: 15.1.5 // Latest available version is detected (15.1.5).
eslint-config-next: N/A
react: 19.0.0
react-dom: 19.0.0
typescript: N/A
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Not sure, create-next-app
Which stage(s) are affected? (Select all that apply)
next dev (local), next start (local)
Additional context
If I replace the example anchor fragment link with a next/link Link
component instead of a stock tag, the browser back button works as intended. However, someone could always end up on a page with a URL fragment already there from an external link beyond your controle.