From b645a851ac75e8db1c4ee834ec69e7d7a818804d Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 14 Dec 2023 09:55:47 -0500 Subject: [PATCH] Only redefine pushstate/replacestate in the browser --- packages/astro/src/transitions/router.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts index 7ab332addd12..b2cfbca6ae0e 100644 --- a/packages/astro/src/transitions/router.ts +++ b/packages/astro/src/transitions/router.ts @@ -11,8 +11,9 @@ type Events = 'astro:page-load' | 'astro:after-swap'; // Create bound versions of pushState/replaceState so that Partytown doesn't hijack them, // which breaks Firefox. -const pushState = history.pushState.bind(history); -const replaceState = history.replaceState.bind(history); +const inBrowser = import.meta.env.SSR === false; +const pushState = (inBrowser && history.pushState.bind(history)) as typeof history.pushState; +const replaceState = (inBrowser && history.replaceState.bind(history)) as typeof history.replaceState; // only update history entries that are managed by us // leave other entries alone and do not accidently add state. @@ -22,7 +23,7 @@ export const updateScrollPosition = (positions: { scrollX: number; scrollY: numb replaceState({ ...history.state, ...positions }, ''); } }; -const inBrowser = import.meta.env.SSR === false; + export const supportsViewTransitions = inBrowser && !!document.startViewTransition;