From c808666c1c619abef867f5a47f28254e2f151f44 Mon Sep 17 00:00:00 2001 From: martrapp <94928215+martrapp@users.noreply.github.com> Date: Wed, 16 Aug 2023 19:58:40 +0200 Subject: [PATCH 1/2] override wrong positions in browser history --- .changeset/popular-planes-cover.md | 5 +++++ packages/astro/components/ViewTransitions.astro | 2 ++ 2 files changed, 7 insertions(+) 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..966ce8c2e678 100644 --- a/packages/astro/components/ViewTransitions.astro +++ b/packages/astro/components/ViewTransitions.astro @@ -163,6 +163,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'); From c80424b3c05385632ee9916ce4620132d7aaed11 Mon Sep 17 00:00:00 2001 From: martrapp <94928215+martrapp@users.noreply.github.com> Date: Wed, 16 Aug 2023 20:26:33 +0200 Subject: [PATCH 2/2] Lost events are taken into account during throttling --- packages/astro/components/ViewTransitions.astro | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/astro/components/ViewTransitions.astro b/packages/astro/components/ViewTransitions.astro index 966ce8c2e678..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); };