Skip to content

Browser back button fails after navigation with URL anchor fragment #75072

Open
@sgarrity

Description

@sgarrity

Link to the code that reproduces this issue

https://github.com/sgarrity/nextjs-anchor-link-testcase

To Reproduce

  1. Run the simple create-next-app example at https://github.com/sgarrity/nextjs-anchor-link-testcase
  2. Click on the "Anchor Example" link (or manually add a #example fragment at the end of the home page URL
  3. Click the "Go to Foobar" link (to /foobar)
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    create-next-appRelated to our CLI tool for quickly starting a new Next.js application.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions