diff --git a/.changeset/popular-planes-cover.md b/.changeset/popular-planes-cover.md new file mode 100644 index 000000000000..c379a7edc030 --- /dev/null +++ b/.changeset/popular-planes-cover.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +ViewTransition: bug fix for lost scroll position in browser history diff --git a/packages/astro/components/ViewTransitions.astro b/packages/astro/components/ViewTransitions.astro index 28a17a09d743..47eb3c112765 100644 --- a/packages/astro/components/ViewTransitions.astro +++ b/packages/astro/components/ViewTransitions.astro @@ -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');