fix(nav): Restore Back/Forward after same-page hash navigation#18681
Open
jaffrepaul wants to merge 2 commits into
Open
fix(nav): Restore Back/Forward after same-page hash navigation#18681jaffrepaul wants to merge 2 commits into
jaffrepaul wants to merge 2 commits into
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
6 tasks
KyleTryon
approved these changes
Jul 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thehashchangeevent that<Expandable>relies on). A native fragment navigation is performed by the browser, which records the new history entry withhistory.state = null. Next.js's App Router ignorespopstatefor 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.src/components/hashNavigation.tsx; mounted inapp/layout.tsx=== nullguard 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)hashchangebehavior are left untouched; no changes toSmartLink, the TOCs, orExpandableVerified 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/tscclean.IS YOUR CHANGE URGENT?
Help us prioritize incoming PRs by letting us know when the change needs to go live.
SLA
Thanks in advance for your help!
PRE-MERGE CHECKLIST
Make sure you've checked the following before merging your changes: