Half Baked
+ hash target +diff --git a/.changeset/chilled-shoes-fail.md b/.changeset/chilled-shoes-fail.md
new file mode 100644
index 000000000..1567ecca3
--- /dev/null
+++ b/.changeset/chilled-shoes-fail.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+ViewTransitions: Fixes in the client-side router
diff --git a/packages/astro/components/ViewTransitions.astro b/packages/astro/components/ViewTransitions.astro
index be312b1bf..12dfe0f4f 100644
--- a/packages/astro/components/ViewTransitions.astro
+++ b/packages/astro/components/ViewTransitions.astro
@@ -20,15 +20,6 @@ const { fallback = 'animate' } = Astro.props as Props;
type Events = 'astro:load' | 'astro:beforeload';
const persistState = (state: State) => history.replaceState(state, '');
-
- // The History API does not tell you if navigation is forward or back, so
- // you can figure it using an index. On pushState the index is incremented so you
- // can use that to determine popstate if going forward or back.
- let currentHistoryIndex = history.state?.index || 0;
- if (!history.state) {
- persistState({ index: currentHistoryIndex, scrollY: 0 });
- }
-
const supportsViewTransitions = !!document.startViewTransition;
const transitionEnabledOnThisPage = () =>
!!document.querySelector('[name="astro-view-transitions-enabled"]');
@@ -36,6 +27,14 @@ const { fallback = 'animate' } = Astro.props as Props;
const onload = () => triggerEvent('astro:load');
const PERSIST_ATTR = 'data-astro-transition-persist';
+ // The History API does not tell you if navigation is forward or back, so
+ // you can figure it using an index. On pushState the index is incremented so you
+ // can use that to determine popstate if going forward or back.
+ let currentHistoryIndex = history.state?.index || 0;
+ if (!history.state && transitionEnabledOnThisPage()) {
+ persistState({ index: currentHistoryIndex, scrollY: 0 });
+ }
+
const throttle = (cb: (...args: any[]) => any, delay: number) => {
let wait = false;
// During the waiting time additional events are lost.
@@ -323,9 +322,10 @@ const { fallback = 'animate' } = Astro.props as Props;
});
addEventListener('popstate', (ev) => {
- if (!transitionEnabledOnThisPage()) {
+ if (!transitionEnabledOnThisPage() && ev.state) {
// The current page doesn't haven't View Transitions,
// respect that with a full page reload
+ // -- but only for transition managed by us (ev.state is set)
location.reload();
return;
}
diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/half-baked.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/half-baked.astro
new file mode 100644
index 000000000..40298d125
--- /dev/null
+++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/half-baked.astro
@@ -0,0 +1,24 @@
+---
+import { ViewTransitions } from 'astro:transitions';
+
+// For the test fixture, we import the script but we don't use the
Half Baked
+ hash target +Page 3
go to 2 +Long paragraph