Skip to content

fix(nav): Restore Back/Forward after same-page hash navigation#18681

Open
jaffrepaul wants to merge 2 commits into
masterfrom
fix-link-behavior
Open

fix(nav): Restore Back/Forward after same-page hash navigation#18681
jaffrepaul wants to merge 2 commits into
masterfrom
fix-link-behavior

Conversation

@jaffrepaul

Copy link
Copy Markdown
Contributor

DESCRIBE YOUR PR

Fixes a browser Back/Forward bug after clicking a same-page anchor (an "On this page" TOC entry, a heading link, etc.).

Same-page # links are rendered as native <a> anchors (on purpose — for native scroll and the hashchange event that <Expandable> relies on). A native fragment navigation is performed by the browser, which records the new history entry with history.state = null. Next.js's App Router ignores popstate for such state-less entries (if (!event.state) return;), so pressing Back to a hash URL left the previous page rendered while only the URL changed — and a second Back was needed to move at all.

This adds one small global handler (HashNavigation, mounted once in the root layout) that, on a same-page hash-link click, copies the current page's router state onto the freshly-created entry. Next then treats it like any other entry and Back/Forward work as expected.

  • New src/components/hashNavigation.tsx; mounted in app/layout.tsx
  • The === null guard fires only for a genuine fragment navigation, so it never touches modified clicks, new tabs, or router navigations (all of which leave a non-null state)
  • Native scroll and hashchange behavior are left untouched; no changes to SmartLink, the TOCs, or Expandable

Verified in a real browser: the reported repro, heading-autolink clicks, deep-links into collapsed Expandables, and multi-hop Back and Forward across hashes + pages. eslint/prettier/tsc clean.

IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs to go live.

  • Urgent deadline (GA date, etc.):
  • Other deadline:
  • None: Not urgent, can wait up to 1 week+

SLA

  • Teamwork makes the dream work, so please add a reviewer to your PRs.
  • Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it.
    Thanks in advance for your help!

PRE-MERGE CHECKLIST

Make sure you've checked the following before merging your changes:

  • Checked Vercel preview for correctness, including links
  • PR was reviewed and approved by any necessary SMEs (subject matter experts)
  • PR was reviewed and approved by a member of the Sentry docs team

A native `<a href="#section">` click performs a browser fragment navigation,
which records the new history entry with `history.state = null`. Next's App
Router ignores `popstate` for such state-less entries, so pressing Back to a
hash URL left the previous page rendered while only the URL changed (and a
second Back was needed to move at all).

Add a small global handler that, on a same-page hash-link click, copies the
current page's router state onto the freshly-created entry. The `=== null`
check fires only for a genuine fragment navigation, so it never interferes with
modified clicks, new tabs, or router navigations. Native scroll and
`hashchange` behavior are left untouched.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
develop-docs Ready Ready Preview, Comment Jul 9, 2026 4:29pm
sentry-docs Ready Ready Preview, Comment Jul 9, 2026 4:29pm

Request Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants