0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

Fix history navigation to hash fragment on Firefox when using view transitions (#9563)

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

* Edit changeset

* Improve changeset, add co-authors
This commit is contained in:
Martin Trapp 2024-01-02 22:18:52 +01:00 committed by GitHub
parent d854330571
commit d48ab90fb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View file

@ -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 <sarah@rainsberger.ca>

View file

@ -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' });