1
1
import {
2
2
handle_target ,
3
- history ,
4
3
initial_data ,
5
- scroll_history ,
6
- scroll_state ,
7
4
select_target ,
8
5
handle_error ,
9
6
set_target
10
7
} from '../app' ;
11
8
import {
9
+ ScrollPosition ,
12
10
Target
13
11
} from '../types' ;
14
12
import prefetch from '../prefetch/index' ;
@@ -23,24 +21,33 @@ export function set_cid(n: number) {
23
21
cid = n ;
24
22
}
25
23
24
+ const _history = typeof history !== 'undefined' ? history : {
25
+ pushState : ( state : any , title : string , href : string ) => { } ,
26
+ replaceState : ( state : any , title : string , href : string ) => { } ,
27
+ scrollRestoration : ''
28
+ } ;
29
+ export { _history as history } ;
30
+
31
+ export const scroll_history : Record < string , ScrollPosition > = { } ;
32
+
26
33
export default function start ( opts : {
27
34
target : Node
28
35
} ) : Promise < void > {
29
- if ( 'scrollRestoration' in history ) {
30
- history . scrollRestoration = 'manual' ;
36
+ if ( 'scrollRestoration' in _history ) {
37
+ _history . scrollRestoration = 'manual' ;
31
38
}
32
39
33
40
// Adopted from Nuxt.js
34
41
// Reset scrollRestoration to auto when leaving page, allowing page reload
35
42
// and back-navigation from other pages to use the browser to restore the
36
43
// scrolling position.
37
44
addEventListener ( 'beforeunload' , ( ) => {
38
- history . scrollRestoration = 'auto' ;
45
+ _history . scrollRestoration = 'auto' ;
39
46
} ) ;
40
47
41
48
// Setting scrollRestoration to manual again when returning to this page.
42
49
addEventListener ( 'load' , ( ) => {
43
- history . scrollRestoration = 'manual' ;
50
+ _history . scrollRestoration = 'manual' ;
44
51
} ) ;
45
52
46
53
set_target ( opts . target ) ;
@@ -55,7 +62,7 @@ export default function start(opts: {
55
62
return Promise . resolve ( ) . then ( ( ) => {
56
63
const { hash, href } = location ;
57
64
58
- history . replaceState ( { id : uid } , '' , href ) ;
65
+ _history . replaceState ( { id : uid } , '' , href ) ;
59
66
60
67
const url = new URL ( location . href ) ;
61
68
@@ -122,7 +129,7 @@ function handle_click(event: MouseEvent) {
122
129
const noscroll = a . hasAttribute ( 'sapper:noscroll' ) ;
123
130
navigate ( target , null , noscroll , url . hash ) ;
124
131
event . preventDefault ( ) ;
125
- history . pushState ( { id : cid } , '' , url . href ) ;
132
+ _history . pushState ( { id : cid } , '' , url . href ) ;
126
133
}
127
134
}
128
135
@@ -135,6 +142,13 @@ function find_anchor(node: Node) {
135
142
return node ;
136
143
}
137
144
145
+ function scroll_state ( ) {
146
+ return {
147
+ x : pageXOffset ,
148
+ y : pageYOffset
149
+ } ;
150
+ }
151
+
138
152
function handle_popstate ( event : PopStateEvent ) {
139
153
scroll_history [ cid ] = scroll_state ( ) ;
140
154
@@ -151,7 +165,7 @@ function handle_popstate(event: PopStateEvent) {
151
165
// hashchange
152
166
set_uid ( uid + 1 ) ;
153
167
set_cid ( uid ) ;
154
- history . replaceState ( { id : cid } , '' , location . href ) ;
168
+ _history . replaceState ( { id : cid } , '' , location . href ) ;
155
169
}
156
170
}
157
171
0 commit comments