From 0e0fa605d109cc91e08a1ae1cc560ea240fe631b Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Wed, 16 Aug 2023 22:38:18 +0200 Subject: [PATCH] Fix 8083 (#8105) * override wrong positions in browser history * Lost events are taken into account during throttling --- .changeset/popular-planes-cover.md | 5 +++++ packages/astro/components/ViewTransitions.astro | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 .changeset/popular-planes-cover.md 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');