@sveltejs/kit 1.0.0-next.275 → 1.0.0-next.276

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,21 @@ function normalize_path(path, trailing_slash) {
22
22
  return path;
23
23
  }
24
24
 
25
+ // We track the scroll position associated with each history entry in sessionStorage,
26
+ // rather than on history.state itself, because when navigation is driven by
27
+ // popstate it's too late to update the scroll position associated with the
28
+ // state we're navigating from
29
+ const SCROLL_KEY = 'sveltekit:scroll';
30
+
31
+ /** @typedef {{ x: number, y: number }} ScrollPosition */
32
+ /** @type {Record<number, ScrollPosition>} */
33
+ let scroll_positions = {};
34
+ try {
35
+ scroll_positions = JSON.parse(sessionStorage[SCROLL_KEY]);
36
+ } catch {
37
+ // do nothing
38
+ }
39
+
25
40
  function scroll_state() {
26
41
  return {
27
42
  x: pageXOffset,
@@ -83,6 +98,11 @@ class Router {
83
98
  history.replaceState({ ...history.state, 'sveltekit:index': 0 }, '', location.href);
84
99
  }
85
100
 
101
+ // if we reload the page, or Cmd-Shift-T back to it,
102
+ // recover scroll position
103
+ const scroll = scroll_positions[this.current_history_index];
104
+ if (scroll) scrollTo(scroll.x, scroll.y);
105
+
86
106
  this.hash_navigating = false;
87
107
 
88
108
  this.callbacks = {
@@ -95,9 +115,7 @@ class Router {
95
115
  }
96
116
 
97
117
  init_listeners() {
98
- if ('scrollRestoration' in history) {
99
- history.scrollRestoration = 'manual';
100
- }
118
+ history.scrollRestoration = 'manual';
101
119
 
102
120
  // Adopted from Nuxt.js
103
121
  // Reset scrollRestoration to auto when leaving page, allowing page reload
@@ -122,28 +140,16 @@ class Router {
122
140
  }
123
141
  });
124
142
 
125
- // Setting scrollRestoration to manual again when returning to this page.
126
- addEventListener('load', () => {
127
- history.scrollRestoration = 'manual';
128
- });
129
-
130
- // There's no API to capture the scroll location right before the user
131
- // hits the back/forward button, so we listen for scroll events
143
+ addEventListener('visibilitychange', () => {
144
+ if (document.visibilityState === 'hidden') {
145
+ this.#update_scroll_positions();
132
146
 
133
- /** @type {NodeJS.Timeout} */
134
- let scroll_timer;
135
- addEventListener('scroll', () => {
136
- clearTimeout(scroll_timer);
137
- scroll_timer = setTimeout(() => {
138
- // Store the scroll location in the history
139
- // This will persist even if we navigate away from the site and come back
140
- const new_state = {
141
- ...(history.state || {}),
142
- 'sveltekit:scroll': scroll_state()
143
- };
144
- history.replaceState(new_state, document.title, window.location.href);
145
- // iOS scroll event intervals happen between 30-150ms, sometimes around 200ms
146
- }, 200);
147
+ try {
148
+ sessionStorage[SCROLL_KEY] = JSON.stringify(scroll_positions);
149
+ } catch {
150
+ // do nothing
151
+ }
152
+ }
147
153
  });
148
154
 
149
155
  /** @param {Event} event */
@@ -216,6 +222,8 @@ class Router {
216
222
  // clicking a hash link and those triggered by popstate
217
223
  this.hash_navigating = true;
218
224
 
225
+ this.#update_scroll_positions();
226
+
219
227
  const info = this.parse(url);
220
228
  if (info) {
221
229
  return this.renderer.update(info, [], false);
@@ -245,7 +253,7 @@ class Router {
245
253
 
246
254
  this._navigate({
247
255
  url: new URL(location.href),
248
- scroll: event.state['sveltekit:scroll'],
256
+ scroll: scroll_positions[event.state['sveltekit:index']],
249
257
  keepfocus: false,
250
258
  chain: [],
251
259
  details: null,
@@ -274,6 +282,10 @@ class Router {
274
282
  });
275
283
  }
276
284
 
285
+ #update_scroll_positions() {
286
+ scroll_positions[this.current_history_index] = scroll_state();
287
+ }
288
+
277
289
  /**
278
290
  * Returns true if `url` has the same origin and basepath as the app
279
291
  * @param {URL} url
@@ -421,6 +433,8 @@ class Router {
421
433
  });
422
434
  }
423
435
 
436
+ this.#update_scroll_positions();
437
+
424
438
  accepted();
425
439
 
426
440
  if (!this.navigating) {
package/dist/cli.js CHANGED
@@ -998,7 +998,7 @@ async function launch(port, https) {
998
998
  exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}`);
999
999
  }
1000
1000
 
1001
- const prog = sade('svelte-kit').version('1.0.0-next.275');
1001
+ const prog = sade('svelte-kit').version('1.0.0-next.276');
1002
1002
 
1003
1003
  prog
1004
1004
  .command('dev')
@@ -1156,7 +1156,7 @@ async function check_port(port) {
1156
1156
  function welcome({ port, host, https, open, loose, allow, cwd }) {
1157
1157
  if (open) launch(port, https);
1158
1158
 
1159
- console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.275'}\n`));
1159
+ console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.276'}\n`));
1160
1160
 
1161
1161
  const protocol = https ? 'https:' : 'http:';
1162
1162
  const exposed = typeof host !== 'undefined' && host !== 'localhost' && host !== '127.0.0.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.275",
3
+ "version": "1.0.0-next.276",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",