Skip to content

Commit

Permalink
Fix chrome ios history
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasKion committed Jan 11, 2025
1 parent 7377505 commit f8a6c10
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions packages/core/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SessionStorage } from './sessionStorage'
import { Page, ScrollRegion } from './types'

const isServer = typeof window === 'undefined'
const isChromeIOS = !isServer && /CriOS/.test(window.navigator.userAgent)

const queue = new Queue<Promise<void>>()

Expand Down Expand Up @@ -47,14 +48,22 @@ class History {

queue.add(() => {
return this.getPageData(page).then((data) => {
window.history.pushState(
{
page: data,
},
'',
page.url,
const pushAction = () => window.history.pushState(
{
page: data,
},
'',
page.url,
)

if (isChromeIOS) {
// Defer history.pushState to the next event loop tick to prevent timing conflicts.
// Ensure any previous history.replaceState completes before pushState is executed.
setTimeout(pushAction)
} else {
pushAction()
}

cb && cb()
})
})
Expand Down Expand Up @@ -169,7 +178,7 @@ class History {
},
url: string,
): void {
window.history.replaceState(
const replaceAction = () => window.history.replaceState(
{
...data,
scrollRegions: data.scrollRegions ?? window.history.state?.scrollRegions,
Expand All @@ -178,6 +187,14 @@ class History {
'',
url,
)

if (isChromeIOS) {
// Defer history.replaceState to the next event loop tick to prevent timing conflicts.
// Ensure any previous history.pushState completes before replaceState is executed.
setTimeout(replaceAction)
} else {
replaceAction()
}
}

public getState<T>(key: keyof Page, defaultValue?: T): any {
Expand Down

0 comments on commit f8a6c10

Please sign in to comment.