* override wrong positions in browser history

* Lost events are taken into account during throttling
This commit is contained in:
Martin Trapp 2023-08-16 22:38:18 +02:00 committed by GitHub
parent 9dd09e2c6a
commit 0e0fa605d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
ViewTransition: bug fix for lost scroll position in browser history

View file

@ -38,12 +38,21 @@ const { fallback = 'animate' } = Astro.props as Props;
const throttle = (cb: (...args: any[]) => any, delay: number) => {
let wait = false;
// During the waiting time additional events are lost.
// So repeat the callback at the end if we have swallowed events.
let onceMore = false;
return (...args: any[]) => {
if (wait) return;
if (wait) {
onceMore = true;
return;
}
cb(...args);
wait = true;
setTimeout(() => {
if (onceMore) {
onceMore = false;
cb(...args);
}
wait = false;
}, delay);
};
@ -163,6 +172,8 @@ const { fallback = 'animate' } = Astro.props as Props;
}
if (state?.scrollY != null) {
scrollTo(0, state.scrollY);
// Overwrite erroneous updates by the scroll handler during transition
persistState(state);
}
triggerEvent('astro:beforeload');