22
33import { inBrowser } from './dom'
44import { saveScrollPosition } from './scroll'
5+ import { genStateKey , setStateKey , getStateKey } from './state-key'
6+
7+ export const supportsPushState =
8+ inBrowser &&
9+ ( function ( ) {
10+ const ua = window . navigator . userAgent
11+
12+ if (
13+ ( ua . indexOf ( 'Android 2.' ) !== - 1 || ua . indexOf ( 'Android 4.0' ) !== - 1 ) &&
14+ ua . indexOf ( 'Mobile Safari' ) !== - 1 &&
15+ ua . indexOf ( 'Chrome' ) === - 1 &&
16+ ua . indexOf ( 'Windows Phone' ) === - 1
17+ ) {
18+ return false
19+ }
520
6- export const supportsPushState = inBrowser && ( function ( ) {
7- const ua = window . navigator . userAgent
8-
9- if (
10- ( ua . indexOf ( 'Android 2.' ) !== - 1 || ua . indexOf ( 'Android 4.0' ) !== - 1 ) &&
11- ua . indexOf ( 'Mobile Safari' ) !== - 1 &&
12- ua . indexOf ( 'Chrome' ) === - 1 &&
13- ua . indexOf ( 'Windows Phone' ) === - 1
14- ) {
15- return false
16- }
17-
18- return window . history && 'pushState' in window . history
19- } ) ( )
20-
21- // use User Timing api (if present) for more accurate key precision
22- const Time = inBrowser && window . performance && window . performance . now
23- ? window . performance
24- : Date
25-
26- let _key : string = genKey ( )
27-
28- function genKey ( ) : string {
29- return Time . now ( ) . toFixed ( 3 )
30- }
31-
32- export function getStateKey ( ) {
33- return _key
34- }
35-
36- export function setStateKey ( key : string ) {
37- _key = key
38- }
21+ return window . history && 'pushState' in window . history
22+ } ) ( )
3923
4024export function pushState ( url ?: string , replace ?: boolean ) {
4125 saveScrollPosition ( )
@@ -44,10 +28,9 @@ export function pushState (url?: string, replace?: boolean) {
4428 const history = window . history
4529 try {
4630 if ( replace ) {
47- history . replaceState ( { key : _key } , '' , url )
31+ history . replaceState ( { key : getStateKey ( ) } , '' , url )
4832 } else {
49- _key = genKey ( )
50- history . pushState ( { key : _key } , '' , url )
33+ history. pushState ( { key : setStateKey ( genStateKey ( ) ) } , '' , url )
5134 }
5235 } catch ( e ) {
5336 window . location [ replace ? 'replace' : 'assign' ] ( url )
0 commit comments