Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix chromeIOS and edgeIOS browser back button problems #1999

Open
wants to merge 2 commits into
base: v1
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { hrefToUrl, mergeDataIntoQueryString, urlWithoutHash } from './url'

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

protected pushState(page: Page): void {
this.page = page
if (isChromeIOS) {
if (isChromeIOS || isEdgeIOS) {
// 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))
setTimeout(() => window.history.pushState(cloneSerializable(page), '', page.url), 1)
} else {
window.history.pushState(cloneSerializable(page), '', page.url)
}
}

protected replaceState(page: Page): void {
this.page = page
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)
}
Comment on lines -509 to -515
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why did you delete this block of code? Wouldn't it just need to be updated to isChromeIOS || isEdgeIOS as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During my testing, I observed that when navigating from one page to another, the replaceState function is called first, followed by pushState, and then replaceState is triggered again. For example, when transitioning from the home page to the dashboard, the browser would first process the home page with replaceState, then execute pushState for the dashboard page, and afterward call replaceState again. With this sequence, the back navigation worked, but I had to click the back button twice to return to the previous page. After adjusting the replaceState function, the issue of adding the same page twice was resolved, and now a single back button click is enough to return to the previous page.

window.history.replaceState(cloneSerializable(page), '', page.url)
}

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