Skip to content

Commit

Permalink
fix chrome ios navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Jan 15, 2025
1 parent a305357 commit 24f40c8
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions packages/core/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { SessionStorage } from './sessionStorage'
import { Page, ScrollRegion } from './types'

const isServer = typeof window === 'undefined'

const queue = new Queue<Promise<void>>()
const isChromeIOS = !isServer && /CriOS/.test(window.navigator.userAgent)

class History {
public rememberedState = 'rememberedState' as const
Expand Down Expand Up @@ -37,25 +37,28 @@ class History {
return
}

if (this.preserveUrl) {
cb && cb()
cb = cb ?? (() => {})

return
if (this.preserveUrl) {
return cb()
}

this.current = page

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

cb && cb()
// Defer history.pushState to the next event loop tick to prevent timing conflicts.
// Ensure any previous history.replaceState completes before pushState is executed.
const doPush = () => {
this.doPushState({ page: data }, page.url)
cb()
}

if (isChromeIOS) {
setTimeout(doPush)
} else {
doPush()
}
})
})
}
Expand Down Expand Up @@ -139,24 +142,28 @@ class History {
return
}

if (this.preserveUrl) {
cb && cb()
cb = cb ?? (() => {})

return
if (this.preserveUrl) {
return cb()
}

this.current = page

queue.add(() => {
return this.getPageData(page).then((data) => {
this.doReplaceState(
{
page: data,
},
page.url,
)

cb && cb()
// Defer history.replaceState to the next event loop tick to prevent timing conflicts.
// Ensure any previous history.pushState completes before replaceState is executed.
const doReplace = () => {
this.doReplaceState({ page: data }, page.url)
cb()
}

if (isChromeIOS) {
setTimeout(doReplace)
} else {
doReplace()
}
})
})
}
Expand All @@ -180,6 +187,17 @@ class History {
)
}

protected doPushState(
data: {
page: Page | ArrayBuffer
scrollRegions?: ScrollRegion[]
documentScrollPosition?: ScrollRegion
},
url: string,
): void {
window.history.pushState(data, '', url)
}

public getState<T>(key: keyof Page, defaultValue?: T): any {
return this.current?.[key] ?? defaultValue
}
Expand Down

0 comments on commit 24f40c8

Please sign in to comment.