Skip to content

Commit e005855

Browse files
authored
revert history function caching (#14294)
1 parent 71562f2 commit e005855

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

.changeset/angry-eels-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Restores the ability to use Google Analytics `History change trigger` with the `<ClientRouter />`.

packages/astro/src/transitions/router.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@ type Transition = {
1919
viewTransitionFinished?: () => void;
2020
};
2121

22-
// Create bound versions of pushState/replaceState so that Partytown doesn't hijack them,
23-
// which breaks Firefox.
2422
const inBrowser = import.meta.env.SSR === false;
25-
const pushState = (inBrowser && history.pushState.bind(history)) as typeof history.pushState;
26-
const replaceState = (inBrowser &&
27-
history.replaceState.bind(history)) as typeof history.replaceState;
2823

2924
// only update history entries that are managed by us
3025
// leave other entries alone and do not accidently add state.
3126
export const updateScrollPosition = (positions: { scrollX: number; scrollY: number }) => {
3227
if (history.state) {
3328
history.scrollRestoration = 'manual';
34-
replaceState({ ...history.state, ...positions }, '');
29+
history.replaceState({ ...history.state, ...positions }, '');
3530
}
3631
};
3732

@@ -93,7 +88,7 @@ if (inBrowser) {
9388
} else if (transitionEnabledOnThisPage()) {
9489
// This page is loaded from the browser address bar or via a link from extern,
9590
// it needs a state in the history
96-
replaceState({ index: currentHistoryIndex, scrollX, scrollY }, '');
91+
history.replaceState({ index: currentHistoryIndex, scrollX, scrollY }, '');
9792
history.scrollRestoration = 'manual';
9893
}
9994
}
@@ -191,7 +186,7 @@ const moveToLocation = (
191186
if (to.href !== location.href && !historyState) {
192187
if (options.history === 'replace') {
193188
const current = history.state;
194-
replaceState(
189+
history.replaceState(
195190
{
196191
...options.state,
197192
index: current.index,
@@ -202,7 +197,7 @@ const moveToLocation = (
202197
to.href,
203198
);
204199
} else {
205-
pushState(
200+
history.pushState(
206201
{ ...options.state, index: ++currentHistoryIndex, scrollX: 0, scrollY: 0 },
207202
'',
208203
to.href,
@@ -231,7 +226,7 @@ const moveToLocation = (
231226
const savedState = history.state;
232227
location.href = to.href; // this kills the history state on Firefox
233228
if (!history.state) {
234-
replaceState(savedState, ''); // this restores the history state
229+
history.replaceState(savedState, ''); // this restores the history state
235230
if (intraPage) {
236231
window.dispatchEvent(new PopStateEvent('popstate'));
237232
}

0 commit comments

Comments
 (0)