Skip to content

Ignore hash-only popstate navigation#1216

Merged
justinvdm merged 1 commit into
mainfrom
fix/hash-popstate-navigation
Jun 2, 2026
Merged

Ignore hash-only popstate navigation#1216
justinvdm merged 1 commit into
mainfrom
fix/hash-popstate-navigation

Conversation

@peterp

@peterp peterp commented Jun 1, 2026

Copy link
Copy Markdown
Member

Problem

Same-page anchor links could jump to the right heading, then immediately jump back to the top. This happened because the browser fired popstate for a hash-only URL change, and Redwood treated it like a full client navigation. The bug showed up on the RedwoodSDK docs site when using table-of-contents links.

Fixes #1215.

Solution

Client navigation now remembers the current pathname + search. When popstate fires but only the hash changed, it lets the browser handle the anchor scroll on its own and skips the RSC navigation. The docs site uses rwsdk from the workspace, so it gets this fix the next time it deploys from this code.

Technical Summary

  • Track a path key for client navigation that ignores location.hash.
  • Update the path key after navigate() pushes or replaces history, so normal route changes still work.
  • Bail out of the popstate handler only when the path key did not change.
  • Add a regression test that proves hash-only popstate does not call onNavigate, does not fetch a navigation payload, and does not apply pending scroll.

Credit

Thanks to @arsdehnel for reporting this, explaining the root cause, and proposing the path-key fix in #1215.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 1, 2026

Copy link
Copy Markdown

Deploying redwood-sdk-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 6171687
Status: ✅  Deploy successful!
Preview URL: https://30b911c5.redwood-sdk-docs.pages.dev
Branch Preview URL: https://fix-hash-popstate-navigation.redwood-sdk-docs.pages.dev

View logs

@peterp peterp force-pushed the fix/hash-popstate-navigation branch from eb184b9 to ab9241e Compare June 1, 2026 09:32
Reported-by: Adam Dehnel <1697162+arsdehnel@users.noreply.github.com>

Co-authored-by: Adam Dehnel <1697162+arsdehnel@users.noreply.github.com>
@peterp peterp force-pushed the fix/hash-popstate-navigation branch from ab9241e to 6171687 Compare June 1, 2026 09:39

@justinvdm justinvdm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @arsdehnel @peterp!

@justinvdm justinvdm merged commit a5ac6e3 into main Jun 2, 2026
8 checks passed
justinvdm added a commit that referenced this pull request Jun 4, 2026
Cherry-picked from a5ac6e3, manually resolved to fit current
scroll-restoration code (pendingScroll instead of ScrollRestorationController).

Client navigation now remembers the current pathname + search. When
popstate fires but only the hash changed, it lets the browser handle
the anchor scroll on its own and skips the RSC navigation.

Fixes #1215.

Co-authored-by: Adam Dehnel <1697162+arsdehnel@users.noreply.github.com>
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.

initClientNavigation: popstate handler triggers RSC navigation on same-page hash links, causing scroll reset

2 participants