Half Baked
+ hash target +diff --git a/.changeset/chilled-shoes-fail.md b/.changeset/chilled-shoes-fail.md
new file mode 100644
index 000000000000..1567ecca3563
--- /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 be312b1bf25d..12dfe0f4f70a 100644
--- a/packages/astro/components/ViewTransitions.astro
+++ b/packages/astro/components/ViewTransitions.astro
@@ -20,22 +20,21 @@ const { fallback = 'animate' } = Astro.props as Props;
type Events = 'astro:load' | 'astro:beforeload';
const persistState = (state: State) => history.replaceState(state, '');
+ const supportsViewTransitions = !!document.startViewTransition;
+ const transitionEnabledOnThisPage = () =>
+ !!document.querySelector('[name="astro-view-transitions-enabled"]');
+ const triggerEvent = (name: Events) => document.dispatchEvent(new Event(name));
+ 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) {
+ if (!history.state && transitionEnabledOnThisPage()) {
persistState({ index: currentHistoryIndex, scrollY: 0 });
}
- const supportsViewTransitions = !!document.startViewTransition;
- const transitionEnabledOnThisPage = () =>
- !!document.querySelector('[name="astro-view-transitions-enabled"]');
- const triggerEvent = (name: Events) => document.dispatchEvent(new Event(name));
- const onload = () => triggerEvent('astro:load');
- const PERSIST_ATTR = 'data-astro-transition-persist';
-
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 000000000000..40298d125f5c
--- /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