Skip to content

Conversation

@destruc7i0n
Copy link

@destruc7i0n destruc7i0n commented Nov 11, 2025

Changes

  • With View Transitions, hashchange events are now emitted when navigating between hash anchors on the same page (e.g., clicking anchor links)
  • The event includes oldURL and newURL properties matching standard browser behavior
  • This behaviour is useful for apps like documentation sites, where we want to sync the highlighted element on the sidebar to the hash.

Testing

  • Added three new e2e tests in view-transitions.test.js:
    • Test that hashchange fires for same-page hash navigation
    • Test that hashchange fires for back/forward navigation with hashes
    • Test that hashchange does NOT fire for cross-page navigation

Docs

I don't think any are required. This implements standard browser behavior that developers already expect.

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2025

🦋 Changeset detected

Latest commit: 2d29da4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Nov 11, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 11, 2025

CodSpeed Performance Report

Merging #14746 will not alter performance

Comparing destruc7i0n:main (2d29da4) with main (00c579a)1

Summary

✅ 6 untouched

Footnotes

  1. No successful run was found on main (ac4f3ac) during the generation of this report, so 00c579a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@destruc7i0n destruc7i0n changed the title fix: emit hashchange events with view transitions feat: emit hashchange events with view transitions Nov 12, 2025
@destruc7i0n destruc7i0n changed the title feat: emit hashchange events with view transitions fix: emit hashchange events with view transitions Nov 12, 2025
@martrapp
Copy link
Member

Hi @destruc7i0n 👋🏼

Thank you for opening this PR!
I wasn't aware that history.pushState() or history.replaceState() won't dispatch the hashchange event.

scrollTo({ left: 0, top: 0, behavior: 'instant' });
}
}
if (intraPage && from.hash !== to.hash) {
Copy link
Member

@martrapp martrapp Nov 16, 2025

Choose a reason for hiding this comment

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

Without the client router, a navigation from /long-page#click-one-again to /long-page won't dispatch a hashchange event.

Also doing a history backward traversal from /long-page to /long-page#click-one-again won't dispatch the event.

To mimic standard browser behavior that developers already expect, we should exclude the event for a navigation to an URL without fragment identifier and for backward traversal from an URL without fragment identifier? 🤓

Copy link
Author

@destruc7i0n destruc7i0n Nov 17, 2025

Choose a reason for hiding this comment

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

Hmm true. Seems like the browser itself is emitting a hashchange event with the backwards traversal from /long-page to /long-page#click-one-again so I'll need to take a look at how to stop that 🤔

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

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants