2
2
3
3
import { inBrowser } from './dom'
4
4
import { 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
+ }
5
20
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
+ } ) ( )
39
23
40
24
export function pushState ( url ?: string , replace ?: boolean ) {
41
25
saveScrollPosition ( )
@@ -44,10 +28,9 @@ export function pushState (url?: string, replace?: boolean) {
44
28
const history = window . history
45
29
try {
46
30
if ( replace ) {
47
- history . replaceState ( { key : _key } , '' , url )
31
+ history . replaceState ( { key : getStateKey ( ) } , '' , url )
48
32
} else {
49
- _key = genKey ( )
50
- history . pushState ( { key : _key } , '' , url )
33
+ history. pushState ( { key : setStateKey ( genStateKey ( ) ) } , '' , url )
51
34
}
52
35
} catch ( e ) {
53
36
window . location [ replace ? 'replace' : 'assign' ] ( url )
0 commit comments