@@ -5,8 +5,8 @@ import { SessionStorage } from './sessionStorage'
5
5
import { Page , ScrollRegion } from './types'
6
6
7
7
const isServer = typeof window === 'undefined'
8
-
9
8
const queue = new Queue < Promise < void > > ( )
9
+ const isChromeIOS = ! isServer && / C r i O S / . test ( window . navigator . userAgent )
10
10
11
11
class History {
12
12
public rememberedState = 'rememberedState' as const
@@ -39,23 +39,25 @@ class History {
39
39
40
40
if ( this . preserveUrl ) {
41
41
cb && cb ( )
42
-
43
42
return
44
43
}
45
44
46
45
this . current = page
47
46
48
47
queue . add ( ( ) => {
49
48
return this . getPageData ( page ) . then ( ( data ) => {
50
- window . history . pushState (
51
- {
52
- page : data ,
53
- } ,
54
- '' ,
55
- page . url ,
56
- )
57
-
58
- cb && cb ( )
49
+ // Defer history.pushState to the next event loop tick to prevent timing conflicts.
50
+ // Ensure any previous history.replaceState completes before pushState is executed.
51
+ const doPush = ( ) => {
52
+ this . doPushState ( { page : data } , page . url )
53
+ cb && cb ( )
54
+ }
55
+
56
+ if ( isChromeIOS ) {
57
+ setTimeout ( doPush )
58
+ } else {
59
+ doPush ( )
60
+ }
59
61
} )
60
62
} )
61
63
}
@@ -141,22 +143,25 @@ class History {
141
143
142
144
if ( this . preserveUrl ) {
143
145
cb && cb ( )
144
-
145
146
return
146
147
}
147
148
148
149
this . current = page
149
150
150
151
queue . add ( ( ) => {
151
152
return this . getPageData ( page ) . then ( ( data ) => {
152
- this . doReplaceState (
153
- {
154
- page : data ,
155
- } ,
156
- page . url ,
157
- )
158
-
159
- cb && cb ( )
153
+ // Defer history.replaceState to the next event loop tick to prevent timing conflicts.
154
+ // Ensure any previous history.pushState completes before replaceState is executed.
155
+ const doReplace = ( ) => {
156
+ this . doReplaceState ( { page : data } , page . url )
157
+ cb && cb ( )
158
+ }
159
+
160
+ if ( isChromeIOS ) {
161
+ setTimeout ( doReplace )
162
+ } else {
163
+ doReplace ( )
164
+ }
160
165
} )
161
166
} )
162
167
}
@@ -180,6 +185,17 @@ class History {
180
185
)
181
186
}
182
187
188
+ protected doPushState (
189
+ data : {
190
+ page : Page | ArrayBuffer
191
+ scrollRegions ?: ScrollRegion [ ]
192
+ documentScrollPosition ?: ScrollRegion
193
+ } ,
194
+ url : string ,
195
+ ) : void {
196
+ window . history . pushState ( data , '' , url )
197
+ }
198
+
183
199
public getState < T > ( key : keyof Page , defaultValue ?: T ) : any {
184
200
return this . current ?. [ key ] ?? defaultValue
185
201
}
0 commit comments