Skip to content

Commit

Permalink
Fix history navigation to hash fragment on Firefox when using view tr…
Browse files Browse the repository at this point in the history
…ansitions (#9563)

* Fix history navigation to hash fragment on Firefox when using view transitions

* Edit changeset

* Improve changeset, add co-authors
  • Loading branch information
martrapp authored Jan 2, 2024
1 parent d854330 commit d48ab90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changeset/breezy-eyes-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"astro": patch
---

Fixes back navigation to fragment links (e.g. `#about`) in Firefox when using view transitions

Co-authored-by: Florian Lefebvre <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
4 changes: 3 additions & 1 deletion packages/astro/src/transitions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ const moveToLocation = (to: URL, from: URL, options: Options, historyState?: Sta
// ... what comes next is a intra-page navigation
// that won't reload the page but instead scroll to the fragment
history.scrollRestoration = 'auto';
location.href = to.href;
const savedState = history.state;
location.href = to.href; // this kills the history state on Firefox
history.state || history.replaceState(savedState, ''); // this restores the history state
} else {
if (!scrolledToTop) {
scrollTo({ left: 0, top: 0, behavior: 'instant' });
Expand Down

0 comments on commit d48ab90

Please sign in to comment.