Skip to content

Commit

Permalink
Fix history navigation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroborges committed Sep 27, 2024
1 parent 6913cd4 commit e6d737f
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from './types'
import { hrefToUrl, mergeDataIntoQueryString, urlWithoutHash } from './url'

const isChromeIOS = /CriOS/.test(navigator.userAgent)
const isServer = typeof window === 'undefined'
const cloneSerializable = <T>(obj: T): T => JSON.parse(JSON.stringify(obj))
const nextFrame = (callback: () => void) => {
Expand Down Expand Up @@ -494,14 +495,24 @@ export class Router {

protected pushState(page: Page): void {
this.page = page
// Defer history.pushState to the next event loop tick to prevent timing conflicts.
// Ensure any previous history.replaceState completes before pushState is executed.
setTimeout(() => window.history.pushState(cloneSerializable(page), '', 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(() => window.history.pushState(cloneSerializable(page), '', page.url))
} else {
window.history.pushState(cloneSerializable(page), '', page.url)
}
}

protected replaceState(page: Page): void {
this.page = page
window.history.replaceState(cloneSerializable(page), '', page.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(() => window.history.replaceState(cloneSerializable(page), '', page.url))
} else {
window.history.replaceState(cloneSerializable(page), '', page.url)
}
}

protected handlePopstateEvent(event: PopStateEvent): void {
Expand Down

0 comments on commit e6d737f

Please sign in to comment.