diff --git a/.changeset/breezy-eyes-teach.md b/.changeset/breezy-eyes-teach.md new file mode 100644 index 000000000000..6ab45e666e26 --- /dev/null +++ b/.changeset/breezy-eyes-teach.md @@ -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 <69633530+florian-lefebvre@users.noreply.github.com> +Co-authored-by: Sarah Rainsberger diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts index d77842f4da3c..848fa38bb4db 100644 --- a/packages/astro/src/transitions/router.ts +++ b/packages/astro/src/transitions/router.ts @@ -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' });