@sveltejs/kit 1.0.0-next.27 → 1.0.0-next.270

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.
Files changed (86) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +20 -0
  3. package/assets/app/navigation.js +79 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/app/stores.js +97 -0
  6. package/assets/chunks/utils.js +13 -0
  7. package/assets/client/singletons.js +21 -0
  8. package/assets/client/start.js +1504 -0
  9. package/assets/components/error.svelte +18 -2
  10. package/assets/env.js +8 -0
  11. package/assets/paths.js +13 -0
  12. package/assets/server/index.js +2725 -0
  13. package/dist/chunks/amp_hook.js +56 -0
  14. package/dist/chunks/build.js +658 -0
  15. package/dist/chunks/cert.js +28154 -0
  16. package/dist/chunks/index.js +467 -0
  17. package/dist/chunks/index2.js +836 -0
  18. package/dist/chunks/index3.js +638 -0
  19. package/dist/chunks/index4.js +115 -0
  20. package/dist/chunks/index5.js +891 -0
  21. package/dist/chunks/index6.js +170 -0
  22. package/dist/chunks/index7.js +15584 -0
  23. package/dist/chunks/index8.js +4207 -0
  24. package/dist/chunks/misc.js +3 -0
  25. package/dist/chunks/multipart-parser.js +449 -0
  26. package/dist/cli.js +1132 -86
  27. package/dist/hooks.js +28 -0
  28. package/dist/install-fetch.js +6518 -0
  29. package/dist/node.js +95 -0
  30. package/package.json +96 -54
  31. package/svelte-kit.js +2 -0
  32. package/types/ambient-modules.d.ts +208 -0
  33. package/types/app.d.ts +35 -0
  34. package/types/config.d.ts +200 -0
  35. package/types/csp.d.ts +115 -0
  36. package/types/endpoint.d.ts +39 -0
  37. package/types/helper.d.ts +23 -0
  38. package/types/hooks.d.ts +34 -0
  39. package/types/index.d.ts +17 -0
  40. package/types/internal.d.ts +260 -0
  41. package/types/page.d.ts +33 -0
  42. package/CHANGELOG.md +0 -319
  43. package/assets/runtime/app/navigation.js +0 -23
  44. package/assets/runtime/app/navigation.js.map +0 -1
  45. package/assets/runtime/app/paths.js +0 -2
  46. package/assets/runtime/app/paths.js.map +0 -1
  47. package/assets/runtime/app/stores.js +0 -78
  48. package/assets/runtime/app/stores.js.map +0 -1
  49. package/assets/runtime/internal/singletons.js +0 -15
  50. package/assets/runtime/internal/singletons.js.map +0 -1
  51. package/assets/runtime/internal/start.js +0 -591
  52. package/assets/runtime/internal/start.js.map +0 -1
  53. package/assets/runtime/utils-85ebcc60.js +0 -18
  54. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  55. package/dist/api.js +0 -44
  56. package/dist/api.js.map +0 -1
  57. package/dist/build.js +0 -246
  58. package/dist/build.js.map +0 -1
  59. package/dist/cli.js.map +0 -1
  60. package/dist/colors.js +0 -37
  61. package/dist/colors.js.map +0 -1
  62. package/dist/create_app.js +0 -580
  63. package/dist/create_app.js.map +0 -1
  64. package/dist/index.js +0 -368
  65. package/dist/index.js.map +0 -1
  66. package/dist/index2.js +0 -12035
  67. package/dist/index2.js.map +0 -1
  68. package/dist/index3.js +0 -547
  69. package/dist/index3.js.map +0 -1
  70. package/dist/index4.js +0 -74
  71. package/dist/index4.js.map +0 -1
  72. package/dist/index5.js +0 -464
  73. package/dist/index5.js.map +0 -1
  74. package/dist/index6.js +0 -734
  75. package/dist/index6.js.map +0 -1
  76. package/dist/logging.js +0 -43
  77. package/dist/logging.js.map +0 -1
  78. package/dist/package.js +0 -432
  79. package/dist/package.js.map +0 -1
  80. package/dist/renderer.js +0 -2403
  81. package/dist/renderer.js.map +0 -1
  82. package/dist/standard.js +0 -101
  83. package/dist/standard.js.map +0 -1
  84. package/dist/utils.js +0 -58
  85. package/dist/utils.js.map +0 -1
  86. package/svelte-kit +0 -3
@@ -0,0 +1,1504 @@
1
+ import Root from '__GENERATED__/root.svelte';
2
+ import { fallback, routes } from '__GENERATED__/manifest.js';
3
+ import { onMount, tick } from 'svelte';
4
+ import { g as get_base_uri } from '../chunks/utils.js';
5
+ import { writable } from 'svelte/store';
6
+ import { base, set_paths } from '../paths.js';
7
+ import { init } from './singletons.js';
8
+
9
+ /**
10
+ * @param {string} path
11
+ * @param {'always' | 'never' | 'ignore'} trailing_slash
12
+ */
13
+ function normalize_path(path, trailing_slash) {
14
+ if (path === '/' || trailing_slash === 'ignore') return path;
15
+
16
+ if (trailing_slash === 'never') {
17
+ return path.endsWith('/') ? path.slice(0, -1) : path;
18
+ } else if (trailing_slash === 'always' && /\/[^./]+$/.test(path)) {
19
+ return path + '/';
20
+ }
21
+
22
+ return path;
23
+ }
24
+
25
+ function scroll_state() {
26
+ return {
27
+ x: pageXOffset,
28
+ y: pageYOffset
29
+ };
30
+ }
31
+
32
+ /**
33
+ * @param {Event} event
34
+ * @returns {HTMLAnchorElement | SVGAElement | undefined}
35
+ */
36
+ function find_anchor(event) {
37
+ const node = event
38
+ .composedPath()
39
+ .find((e) => e instanceof Node && e.nodeName.toUpperCase() === 'A'); // SVG <a> elements have a lowercase name
40
+ return /** @type {HTMLAnchorElement | SVGAElement | undefined} */ (node);
41
+ }
42
+
43
+ /**
44
+ * @param {HTMLAnchorElement | SVGAElement} node
45
+ * @returns {URL}
46
+ */
47
+ function get_href(node) {
48
+ return node instanceof SVGAElement
49
+ ? new URL(node.href.baseVal, document.baseURI)
50
+ : new URL(node.href);
51
+ }
52
+
53
+ class Router {
54
+ /**
55
+ * @param {{
56
+ * base: string;
57
+ * routes: import('types/internal').CSRRoute[];
58
+ * trailing_slash: import('types/internal').TrailingSlash;
59
+ * renderer: import('./renderer').Renderer
60
+ * }} opts
61
+ */
62
+ constructor({ base, routes, trailing_slash, renderer }) {
63
+ this.base = base;
64
+ this.routes = routes;
65
+ this.trailing_slash = trailing_slash;
66
+ /** Keeps tracks of multiple navigations caused by redirects during rendering */
67
+ this.navigating = 0;
68
+
69
+ /** @type {import('./renderer').Renderer} */
70
+ this.renderer = renderer;
71
+ renderer.router = this;
72
+
73
+ this.enabled = true;
74
+
75
+ // make it possible to reset focus
76
+ document.body.setAttribute('tabindex', '-1');
77
+
78
+ // keeping track of the history index in order to prevent popstate navigation events if needed
79
+ this.current_history_index = history.state?.['sveltekit:index'] ?? 0;
80
+
81
+ if (this.current_history_index === 0) {
82
+ // create initial history entry, so we can return here
83
+ history.replaceState({ ...history.state, 'sveltekit:index': 0 }, '', location.href);
84
+ }
85
+
86
+ this.callbacks = {
87
+ /** @type {Array<({ from, to, cancel }: { from: URL, to: URL | null, cancel: () => void }) => void>} */
88
+ before_navigate: [],
89
+
90
+ /** @type {Array<({ from, to }: { from: URL | null, to: URL }) => void>} */
91
+ after_navigate: []
92
+ };
93
+ }
94
+
95
+ init_listeners() {
96
+ if ('scrollRestoration' in history) {
97
+ history.scrollRestoration = 'manual';
98
+ }
99
+
100
+ // Adopted from Nuxt.js
101
+ // Reset scrollRestoration to auto when leaving page, allowing page reload
102
+ // and back-navigation from other pages to use the browser to restore the
103
+ // scrolling position.
104
+ addEventListener('beforeunload', (e) => {
105
+ let should_block = false;
106
+
107
+ const intent = {
108
+ from: this.renderer.current.url,
109
+ to: null,
110
+ cancel: () => (should_block = true)
111
+ };
112
+
113
+ this.callbacks.before_navigate.forEach((fn) => fn(intent));
114
+
115
+ if (should_block) {
116
+ e.preventDefault();
117
+ e.returnValue = '';
118
+ } else {
119
+ history.scrollRestoration = 'auto';
120
+ }
121
+ });
122
+
123
+ // Setting scrollRestoration to manual again when returning to this page.
124
+ addEventListener('load', () => {
125
+ history.scrollRestoration = 'manual';
126
+ });
127
+
128
+ // There's no API to capture the scroll location right before the user
129
+ // hits the back/forward button, so we listen for scroll events
130
+
131
+ /** @type {NodeJS.Timeout} */
132
+ let scroll_timer;
133
+ addEventListener('scroll', () => {
134
+ clearTimeout(scroll_timer);
135
+ scroll_timer = setTimeout(() => {
136
+ // Store the scroll location in the history
137
+ // This will persist even if we navigate away from the site and come back
138
+ const new_state = {
139
+ ...(history.state || {}),
140
+ 'sveltekit:scroll': scroll_state()
141
+ };
142
+ history.replaceState(new_state, document.title, window.location.href);
143
+ // iOS scroll event intervals happen between 30-150ms, sometimes around 200ms
144
+ }, 200);
145
+ });
146
+
147
+ /** @param {Event} event */
148
+ const trigger_prefetch = (event) => {
149
+ const a = find_anchor(event);
150
+ if (a && a.href && a.hasAttribute('sveltekit:prefetch')) {
151
+ this.prefetch(get_href(a));
152
+ }
153
+ };
154
+
155
+ /** @type {NodeJS.Timeout} */
156
+ let mousemove_timeout;
157
+
158
+ /** @param {MouseEvent|TouchEvent} event */
159
+ const handle_mousemove = (event) => {
160
+ clearTimeout(mousemove_timeout);
161
+ mousemove_timeout = setTimeout(() => {
162
+ // event.composedPath(), which is used in find_anchor, will be empty if the event is read in a timeout
163
+ // add a layer of indirection to address that
164
+ event.target?.dispatchEvent(
165
+ new CustomEvent('sveltekit:trigger_prefetch', { bubbles: true })
166
+ );
167
+ }, 20);
168
+ };
169
+
170
+ addEventListener('touchstart', trigger_prefetch);
171
+ addEventListener('mousemove', handle_mousemove);
172
+ addEventListener('sveltekit:trigger_prefetch', trigger_prefetch);
173
+
174
+ /** @param {MouseEvent} event */
175
+ addEventListener('click', (event) => {
176
+ if (!this.enabled) return;
177
+
178
+ // Adapted from https://github.com/visionmedia/page.js
179
+ // MIT license https://github.com/visionmedia/page.js#license
180
+ if (event.button || event.which !== 1) return;
181
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
182
+ if (event.defaultPrevented) return;
183
+
184
+ const a = find_anchor(event);
185
+ if (!a) return;
186
+
187
+ if (!a.href) return;
188
+
189
+ const url = get_href(a);
190
+ const url_string = url.toString();
191
+ if (url_string === location.href) {
192
+ if (!location.hash) event.preventDefault();
193
+ return;
194
+ }
195
+
196
+ // Ignore if tag has
197
+ // 1. 'download' attribute
198
+ // 2. 'rel' attribute includes external
199
+ const rel = (a.getAttribute('rel') || '').split(/\s+/);
200
+
201
+ if (a.hasAttribute('download') || (rel && rel.includes('external'))) {
202
+ return;
203
+ }
204
+
205
+ // Ignore if <a> has a target
206
+ if (a instanceof SVGAElement ? a.target.baseVal : a.target) return;
207
+
208
+ // Check if new url only differs by hash and use the browser default behavior in that case
209
+ // This will ensure the `hashchange` event is fired
210
+ // Removing the hash does a full page navigation in the browser, so make sure a hash is present
211
+ const [base, hash] = url.href.split('#');
212
+ if (hash !== undefined && base === location.href.split('#')[0]) {
213
+ // Call `pushState` to add url to history so going back works.
214
+ // Also make a delay, otherwise the browser default behaviour would not kick in
215
+ setTimeout(() => history.pushState({}, '', url.href));
216
+ const info = this.parse(url);
217
+ if (info) {
218
+ return this.renderer.update(info, [], false);
219
+ }
220
+ return;
221
+ }
222
+
223
+ this._navigate({
224
+ url,
225
+ scroll: a.hasAttribute('sveltekit:noscroll') ? scroll_state() : null,
226
+ keepfocus: false,
227
+ chain: [],
228
+ details: {
229
+ state: {},
230
+ replaceState: false
231
+ },
232
+ accepted: () => event.preventDefault(),
233
+ blocked: () => event.preventDefault()
234
+ });
235
+ });
236
+
237
+ addEventListener('popstate', (event) => {
238
+ if (event.state && this.enabled) {
239
+ // if a popstate-driven navigation is cancelled, we need to counteract it
240
+ // with history.go, which means we end up back here, hence this check
241
+ if (event.state['sveltekit:index'] === this.current_history_index) return;
242
+
243
+ this._navigate({
244
+ url: new URL(location.href),
245
+ scroll: event.state['sveltekit:scroll'],
246
+ keepfocus: false,
247
+ chain: [],
248
+ details: null,
249
+ accepted: () => {
250
+ this.current_history_index = event.state['sveltekit:index'];
251
+ },
252
+ blocked: () => {
253
+ const delta = this.current_history_index - event.state['sveltekit:index'];
254
+ history.go(delta);
255
+ }
256
+ });
257
+ }
258
+ });
259
+ }
260
+
261
+ /**
262
+ * Returns true if `url` has the same origin and basepath as the app
263
+ * @param {URL} url
264
+ */
265
+ owns(url) {
266
+ return url.origin === location.origin && url.pathname.startsWith(this.base);
267
+ }
268
+
269
+ /**
270
+ * @param {URL} url
271
+ * @returns {import('./types').NavigationInfo | undefined}
272
+ */
273
+ parse(url) {
274
+ if (this.owns(url)) {
275
+ const path = decodeURI(url.pathname.slice(this.base.length) || '/');
276
+
277
+ return {
278
+ id: url.pathname + url.search,
279
+ routes: this.routes.filter(([pattern]) => pattern.test(path)),
280
+ url,
281
+ path
282
+ };
283
+ }
284
+ }
285
+
286
+ /**
287
+ * @typedef {Parameters<typeof import('$app/navigation').goto>} GotoParams
288
+ *
289
+ * @param {GotoParams[0]} href
290
+ * @param {GotoParams[1]} opts
291
+ * @param {string[]} chain
292
+ */
293
+ async goto(
294
+ href,
295
+ { noscroll = false, replaceState = false, keepfocus = false, state = {} } = {},
296
+ chain
297
+ ) {
298
+ const url = new URL(href, get_base_uri(document));
299
+
300
+ if (this.enabled) {
301
+ return this._navigate({
302
+ url,
303
+ scroll: noscroll ? scroll_state() : null,
304
+ keepfocus,
305
+ chain,
306
+ details: {
307
+ state,
308
+ replaceState
309
+ },
310
+ accepted: () => {},
311
+ blocked: () => {}
312
+ });
313
+ }
314
+
315
+ location.href = url.href;
316
+ return new Promise(() => {
317
+ /* never resolves */
318
+ });
319
+ }
320
+
321
+ enable() {
322
+ this.enabled = true;
323
+ }
324
+
325
+ disable() {
326
+ this.enabled = false;
327
+ }
328
+
329
+ /**
330
+ * @param {URL} url
331
+ * @returns {Promise<import('./types').NavigationResult>}
332
+ */
333
+ async prefetch(url) {
334
+ const info = this.parse(url);
335
+
336
+ if (!info) {
337
+ throw new Error('Attempted to prefetch a URL that does not belong to this app');
338
+ }
339
+
340
+ return this.renderer.load(info);
341
+ }
342
+
343
+ /** @param {({ from, to }: { from: URL | null, to: URL }) => void} fn */
344
+ after_navigate(fn) {
345
+ onMount(() => {
346
+ this.callbacks.after_navigate.push(fn);
347
+
348
+ return () => {
349
+ const i = this.callbacks.after_navigate.indexOf(fn);
350
+ this.callbacks.after_navigate.splice(i, 1);
351
+ };
352
+ });
353
+ }
354
+
355
+ /**
356
+ * @param {({ from, to, cancel }: { from: URL, to: URL | null, cancel: () => void }) => void} fn
357
+ */
358
+ before_navigate(fn) {
359
+ onMount(() => {
360
+ this.callbacks.before_navigate.push(fn);
361
+
362
+ return () => {
363
+ const i = this.callbacks.before_navigate.indexOf(fn);
364
+ this.callbacks.before_navigate.splice(i, 1);
365
+ };
366
+ });
367
+ }
368
+
369
+ /**
370
+ * @param {{
371
+ * url: URL;
372
+ * scroll: { x: number, y: number } | null;
373
+ * keepfocus: boolean;
374
+ * chain: string[];
375
+ * details: {
376
+ * replaceState: boolean;
377
+ * state: any;
378
+ * } | null;
379
+ * accepted: () => void;
380
+ * blocked: () => void;
381
+ * }} opts
382
+ */
383
+ async _navigate({ url, scroll, keepfocus, chain, details, accepted, blocked }) {
384
+ const from = this.renderer.current.url;
385
+ let should_block = false;
386
+
387
+ const intent = {
388
+ from,
389
+ to: url,
390
+ cancel: () => (should_block = true)
391
+ };
392
+
393
+ this.callbacks.before_navigate.forEach((fn) => fn(intent));
394
+
395
+ if (should_block) {
396
+ blocked();
397
+ return;
398
+ }
399
+
400
+ const info = this.parse(url);
401
+ if (!info) {
402
+ location.href = url.href;
403
+ return new Promise(() => {
404
+ // never resolves
405
+ });
406
+ }
407
+
408
+ accepted();
409
+
410
+ if (!this.navigating) {
411
+ dispatchEvent(new CustomEvent('sveltekit:navigation-start'));
412
+ }
413
+ this.navigating++;
414
+
415
+ const pathname = normalize_path(url.pathname, this.trailing_slash);
416
+
417
+ info.url = new URL(url.origin + pathname + url.search + url.hash);
418
+
419
+ if (details) {
420
+ const change = details.replaceState ? 0 : 1;
421
+ details.state['sveltekit:index'] = this.current_history_index += change;
422
+ history[details.replaceState ? 'replaceState' : 'pushState'](details.state, '', info.url);
423
+ }
424
+
425
+ await this.renderer.handle_navigation(info, chain, false, {
426
+ scroll,
427
+ keepfocus
428
+ });
429
+
430
+ this.navigating--;
431
+ if (!this.navigating) {
432
+ dispatchEvent(new CustomEvent('sveltekit:navigation-end'));
433
+
434
+ const navigation = { from, to: url };
435
+ this.callbacks.after_navigate.forEach((fn) => fn(navigation));
436
+ }
437
+ }
438
+ }
439
+
440
+ /**
441
+ * @param {unknown} err
442
+ * @return {Error}
443
+ */
444
+ function coalesce_to_error(err) {
445
+ return err instanceof Error ||
446
+ (err && /** @type {any} */ (err).name && /** @type {any} */ (err).message)
447
+ ? /** @type {Error} */ (err)
448
+ : new Error(JSON.stringify(err));
449
+ }
450
+
451
+ /**
452
+ * Hash using djb2
453
+ * @param {import('types/hooks').StrictBody} value
454
+ */
455
+ function hash(value) {
456
+ let hash = 5381;
457
+ let i = value.length;
458
+
459
+ if (typeof value === 'string') {
460
+ while (i) hash = (hash * 33) ^ value.charCodeAt(--i);
461
+ } else {
462
+ while (i) hash = (hash * 33) ^ value[--i];
463
+ }
464
+
465
+ return (hash >>> 0).toString(36);
466
+ }
467
+
468
+ /**
469
+ * @param {import('types/page').LoadOutput} loaded
470
+ * @returns {import('types/internal').NormalizedLoadOutput}
471
+ */
472
+ function normalize(loaded) {
473
+ const has_error_status =
474
+ loaded.status && loaded.status >= 400 && loaded.status <= 599 && !loaded.redirect;
475
+ if (loaded.error || has_error_status) {
476
+ const status = loaded.status;
477
+
478
+ if (!loaded.error && has_error_status) {
479
+ return {
480
+ status: status || 500,
481
+ error: new Error()
482
+ };
483
+ }
484
+
485
+ const error = typeof loaded.error === 'string' ? new Error(loaded.error) : loaded.error;
486
+
487
+ if (!(error instanceof Error)) {
488
+ return {
489
+ status: 500,
490
+ error: new Error(
491
+ `"error" property returned from load() must be a string or instance of Error, received type "${typeof error}"`
492
+ )
493
+ };
494
+ }
495
+
496
+ if (!status || status < 400 || status > 599) {
497
+ console.warn('"error" returned from load() without a valid status code — defaulting to 500');
498
+ return { status: 500, error };
499
+ }
500
+
501
+ return { status, error };
502
+ }
503
+
504
+ if (loaded.redirect) {
505
+ if (!loaded.status || Math.floor(loaded.status / 100) !== 3) {
506
+ return {
507
+ status: 500,
508
+ error: new Error(
509
+ '"redirect" property returned from load() must be accompanied by a 3xx status code'
510
+ )
511
+ };
512
+ }
513
+
514
+ if (typeof loaded.redirect !== 'string') {
515
+ return {
516
+ status: 500,
517
+ error: new Error('"redirect" property returned from load() must be a string')
518
+ };
519
+ }
520
+ }
521
+
522
+ // TODO remove before 1.0
523
+ if (/** @type {any} */ (loaded).context) {
524
+ throw new Error(
525
+ 'You are returning "context" from a load function. ' +
526
+ '"context" was renamed to "stuff", please adjust your code accordingly.'
527
+ );
528
+ }
529
+
530
+ return /** @type {import('types/internal').NormalizedLoadOutput} */ (loaded);
531
+ }
532
+
533
+ /**
534
+ * @typedef {import('types/internal').CSRComponent} CSRComponent
535
+ * @typedef {{ from: URL; to: URL }} Navigating
536
+ */
537
+
538
+ /** @param {any} value */
539
+ function notifiable_store(value) {
540
+ const store = writable(value);
541
+ let ready = true;
542
+
543
+ function notify() {
544
+ ready = true;
545
+ store.update((val) => val);
546
+ }
547
+
548
+ /** @param {any} new_value */
549
+ function set(new_value) {
550
+ ready = false;
551
+ store.set(new_value);
552
+ }
553
+
554
+ /** @param {(value: any) => void} run */
555
+ function subscribe(run) {
556
+ /** @type {any} */
557
+ let old_value;
558
+ return store.subscribe((new_value) => {
559
+ if (old_value === undefined || (ready && new_value !== old_value)) {
560
+ run((old_value = new_value));
561
+ }
562
+ });
563
+ }
564
+
565
+ return { notify, set, subscribe };
566
+ }
567
+
568
+ function create_updated_store() {
569
+ const { set, subscribe } = writable(false);
570
+
571
+ const interval = +(
572
+ /** @type {string} */ (import.meta.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL)
573
+ );
574
+ const initial = import.meta.env.VITE_SVELTEKIT_APP_VERSION;
575
+
576
+ /** @type {NodeJS.Timeout} */
577
+ let timeout;
578
+
579
+ async function check() {
580
+ if (import.meta.env.DEV || import.meta.env.SSR) return false;
581
+
582
+ clearTimeout(timeout);
583
+
584
+ if (interval) timeout = setTimeout(check, interval);
585
+
586
+ const file = import.meta.env.VITE_SVELTEKIT_APP_VERSION_FILE;
587
+
588
+ const res = await fetch(`${base}/${file}`, {
589
+ headers: {
590
+ pragma: 'no-cache',
591
+ 'cache-control': 'no-cache'
592
+ }
593
+ });
594
+
595
+ if (res.ok) {
596
+ const { version } = await res.json();
597
+ const updated = version !== initial;
598
+
599
+ if (updated) {
600
+ set(true);
601
+ clearTimeout(timeout);
602
+ }
603
+
604
+ return updated;
605
+ } else {
606
+ throw new Error(`Version check failed: ${res.status}`);
607
+ }
608
+ }
609
+
610
+ if (interval) timeout = setTimeout(check, interval);
611
+
612
+ return {
613
+ subscribe,
614
+ check
615
+ };
616
+ }
617
+
618
+ /**
619
+ * @param {RequestInfo} resource
620
+ * @param {RequestInit} [opts]
621
+ */
622
+ function initial_fetch(resource, opts) {
623
+ const url = typeof resource === 'string' ? resource : resource.url;
624
+
625
+ let selector = `script[data-type="svelte-data"][data-url=${JSON.stringify(url)}]`;
626
+
627
+ if (opts && typeof opts.body === 'string') {
628
+ selector += `[data-body="${hash(opts.body)}"]`;
629
+ }
630
+
631
+ const script = document.querySelector(selector);
632
+ if (script && script.textContent) {
633
+ const { body, ...init } = JSON.parse(script.textContent);
634
+ return Promise.resolve(new Response(body, init));
635
+ }
636
+
637
+ return fetch(resource, opts);
638
+ }
639
+
640
+ class Renderer {
641
+ /**
642
+ * @param {{
643
+ * Root: CSRComponent;
644
+ * fallback: [CSRComponent, CSRComponent];
645
+ * target: Node;
646
+ * session: any;
647
+ * }} opts
648
+ */
649
+ constructor({ Root, fallback, target, session }) {
650
+ this.Root = Root;
651
+ this.fallback = fallback;
652
+
653
+ /** @type {import('./router').Router | undefined} */
654
+ this.router;
655
+
656
+ this.target = target;
657
+
658
+ this.started = false;
659
+
660
+ this.session_id = 1;
661
+ this.invalid = new Set();
662
+ this.invalidating = null;
663
+ this.autoscroll = true;
664
+ this.updating = false;
665
+
666
+ /** @type {import('./types').NavigationState} */
667
+ this.current = {
668
+ // @ts-ignore - we need the initial value to be null
669
+ url: null,
670
+ session_id: 0,
671
+ branch: []
672
+ };
673
+
674
+ /** @type {Map<string, import('./types').NavigationResult>} */
675
+ this.cache = new Map();
676
+
677
+ /** @type {{id: string | null, promise: Promise<import('./types').NavigationResult> | null}} */
678
+ this.loading = {
679
+ id: null,
680
+ promise: null
681
+ };
682
+
683
+ this.stores = {
684
+ url: notifiable_store({}),
685
+ page: notifiable_store({}),
686
+ navigating: writable(/** @type {Navigating | null} */ (null)),
687
+ session: writable(session),
688
+ updated: create_updated_store()
689
+ };
690
+
691
+ this.$session = null;
692
+
693
+ this.root = null;
694
+
695
+ let ready = false;
696
+ this.stores.session.subscribe(async (value) => {
697
+ this.$session = value;
698
+
699
+ if (!ready || !this.router) return;
700
+ this.session_id += 1;
701
+
702
+ const info = this.router.parse(new URL(location.href));
703
+ if (info) this.update(info, [], true);
704
+ });
705
+ ready = true;
706
+ }
707
+
708
+ disable_scroll_handling() {
709
+ if (import.meta.env.DEV && this.started && !this.updating) {
710
+ throw new Error('Can only disable scroll handling during navigation');
711
+ }
712
+
713
+ if (this.updating || !this.started) {
714
+ this.autoscroll = false;
715
+ }
716
+ }
717
+
718
+ /**
719
+ * @param {{
720
+ * status: number;
721
+ * error: Error;
722
+ * nodes: Array<Promise<CSRComponent>>;
723
+ * url: URL;
724
+ * params: Record<string, string>;
725
+ * }} selected
726
+ */
727
+ async start({ status, error, nodes, url, params }) {
728
+ /** @type {Array<import('./types').BranchNode | undefined>} */
729
+ const branch = [];
730
+
731
+ /** @type {Record<string, any>} */
732
+ let stuff = {};
733
+
734
+ /** @type {import('./types').NavigationResult | undefined} */
735
+ let result;
736
+
737
+ let error_args;
738
+
739
+ // url.hash is empty when coming from the server
740
+ url.hash = window.location.hash;
741
+
742
+ try {
743
+ for (let i = 0; i < nodes.length; i += 1) {
744
+ const is_leaf = i === nodes.length - 1;
745
+
746
+ let props;
747
+
748
+ if (is_leaf) {
749
+ const serialized = document.querySelector('[data-type="svelte-props"]');
750
+ if (serialized) {
751
+ props = JSON.parse(/** @type {string} */ (serialized.textContent));
752
+ }
753
+ }
754
+
755
+ const node = await this._load_node({
756
+ module: await nodes[i],
757
+ url,
758
+ params,
759
+ stuff,
760
+ status: is_leaf ? status : undefined,
761
+ error: is_leaf ? error : undefined,
762
+ props
763
+ });
764
+
765
+ if (props) {
766
+ node.uses.dependencies.add(url.href);
767
+ }
768
+
769
+ branch.push(node);
770
+
771
+ if (node && node.loaded) {
772
+ if (node.loaded.error) {
773
+ if (error) throw node.loaded.error;
774
+ error_args = {
775
+ status: node.loaded.status,
776
+ error: node.loaded.error,
777
+ url
778
+ };
779
+ } else if (node.loaded.stuff) {
780
+ stuff = {
781
+ ...stuff,
782
+ ...node.loaded.stuff
783
+ };
784
+ }
785
+ }
786
+ }
787
+
788
+ result = error_args
789
+ ? await this._load_error(error_args)
790
+ : await this._get_navigation_result_from_branch({
791
+ url,
792
+ params,
793
+ stuff,
794
+ branch,
795
+ status,
796
+ error
797
+ });
798
+ } catch (e) {
799
+ if (error) throw e;
800
+
801
+ result = await this._load_error({
802
+ status: 500,
803
+ error: coalesce_to_error(e),
804
+ url
805
+ });
806
+ }
807
+
808
+ if (result.redirect) {
809
+ // this is a real edge case — `load` would need to return
810
+ // a redirect but only in the browser
811
+ location.href = new URL(result.redirect, location.href).href;
812
+ return;
813
+ }
814
+
815
+ this._init(result);
816
+ }
817
+
818
+ /**
819
+ * @param {import('./types').NavigationInfo} info
820
+ * @param {string[]} chain
821
+ * @param {boolean} no_cache
822
+ * @param {{hash?: string, scroll: { x: number, y: number } | null, keepfocus: boolean}} [opts]
823
+ */
824
+ async handle_navigation(info, chain, no_cache, opts) {
825
+ if (this.started) {
826
+ this.stores.navigating.set({
827
+ from: this.current.url,
828
+ to: info.url
829
+ });
830
+ }
831
+
832
+ await this.update(info, chain, no_cache, opts);
833
+ }
834
+
835
+ /**
836
+ * @param {import('./types').NavigationInfo} info
837
+ * @param {string[]} chain
838
+ * @param {boolean} no_cache
839
+ * @param {{hash?: string, scroll: { x: number, y: number } | null, keepfocus: boolean}} [opts]
840
+ */
841
+ async update(info, chain, no_cache, opts) {
842
+ const token = (this.token = {});
843
+ let navigation_result = await this._get_navigation_result(info, no_cache);
844
+
845
+ // abort if user navigated during update
846
+ if (token !== this.token) return;
847
+
848
+ this.invalid.clear();
849
+
850
+ if (navigation_result.redirect) {
851
+ if (chain.length > 10 || chain.includes(info.url.pathname)) {
852
+ navigation_result = await this._load_error({
853
+ status: 500,
854
+ error: new Error('Redirect loop'),
855
+ url: info.url
856
+ });
857
+ } else {
858
+ if (this.router) {
859
+ this.router.goto(
860
+ new URL(navigation_result.redirect, info.url).href,
861
+ { replaceState: true },
862
+ [...chain, info.url.pathname]
863
+ );
864
+ } else {
865
+ location.href = new URL(navigation_result.redirect, location.href).href;
866
+ }
867
+
868
+ return;
869
+ }
870
+ } else if (navigation_result.props?.page?.status >= 400) {
871
+ const updated = await this.stores.updated.check();
872
+ if (updated) {
873
+ location.href = info.url.href;
874
+ return;
875
+ }
876
+ }
877
+
878
+ this.updating = true;
879
+
880
+ if (this.started) {
881
+ this.current = navigation_result.state;
882
+
883
+ this.root.$set(navigation_result.props);
884
+ this.stores.navigating.set(null);
885
+ } else {
886
+ this._init(navigation_result);
887
+ }
888
+
889
+ // opts must be passed if we're navigating
890
+ if (opts) {
891
+ const { scroll, keepfocus } = opts;
892
+
893
+ if (!keepfocus) {
894
+ getSelection()?.removeAllRanges();
895
+ document.body.focus();
896
+ }
897
+
898
+ // need to render the DOM before we can scroll to the rendered elements
899
+ await tick();
900
+
901
+ if (this.autoscroll) {
902
+ const deep_linked = info.url.hash && document.getElementById(info.url.hash.slice(1));
903
+ if (scroll) {
904
+ scrollTo(scroll.x, scroll.y);
905
+ } else if (deep_linked) {
906
+ // Here we use `scrollIntoView` on the element instead of `scrollTo`
907
+ // because it natively supports the `scroll-margin` and `scroll-behavior`
908
+ // CSS properties.
909
+ deep_linked.scrollIntoView();
910
+ } else {
911
+ scrollTo(0, 0);
912
+ }
913
+ }
914
+ } else {
915
+ // in this case we're simply invalidating
916
+ await tick();
917
+ }
918
+
919
+ this.loading.promise = null;
920
+ this.loading.id = null;
921
+ this.autoscroll = true;
922
+ this.updating = false;
923
+
924
+ if (!this.router) return;
925
+
926
+ const leaf_node = navigation_result.state.branch[navigation_result.state.branch.length - 1];
927
+ if (leaf_node && leaf_node.module.router === false) {
928
+ this.router.disable();
929
+ } else {
930
+ this.router.enable();
931
+ }
932
+ }
933
+
934
+ /**
935
+ * @param {import('./types').NavigationInfo} info
936
+ * @returns {Promise<import('./types').NavigationResult>}
937
+ */
938
+ load(info) {
939
+ this.loading.promise = this._get_navigation_result(info, false);
940
+ this.loading.id = info.id;
941
+
942
+ return this.loading.promise;
943
+ }
944
+
945
+ /** @param {string} href */
946
+ invalidate(href) {
947
+ this.invalid.add(href);
948
+
949
+ if (!this.invalidating) {
950
+ this.invalidating = Promise.resolve().then(async () => {
951
+ const info = this.router && this.router.parse(new URL(location.href));
952
+ if (info) await this.update(info, [], true);
953
+
954
+ this.invalidating = null;
955
+ });
956
+ }
957
+
958
+ return this.invalidating;
959
+ }
960
+
961
+ /** @param {import('./types').NavigationResult} result */
962
+ _init(result) {
963
+ this.current = result.state;
964
+
965
+ const style = document.querySelector('style[data-svelte]');
966
+ if (style) style.remove();
967
+
968
+ this.root = new this.Root({
969
+ target: this.target,
970
+ props: {
971
+ stores: this.stores,
972
+ ...result.props
973
+ },
974
+ hydrate: true
975
+ });
976
+
977
+ this.started = true;
978
+
979
+ if (this.router) {
980
+ const navigation = { from: null, to: new URL(location.href) };
981
+ this.router.callbacks.after_navigate.forEach((fn) => fn(navigation));
982
+ }
983
+ }
984
+
985
+ /**
986
+ * @param {import('./types').NavigationInfo} info
987
+ * @param {boolean} no_cache
988
+ * @returns {Promise<import('./types').NavigationResult>}
989
+ */
990
+ async _get_navigation_result(info, no_cache) {
991
+ if (this.loading.id === info.id && this.loading.promise) {
992
+ return this.loading.promise;
993
+ }
994
+
995
+ for (let i = 0; i < info.routes.length; i += 1) {
996
+ const route = info.routes[i];
997
+
998
+ // load code for subsequent routes immediately, if they are as
999
+ // likely to match the current path/query as the current one
1000
+ let j = i + 1;
1001
+ while (j < info.routes.length) {
1002
+ const next = info.routes[j];
1003
+ if (next[0].toString() === route[0].toString()) {
1004
+ next[1].forEach((loader) => loader());
1005
+ j += 1;
1006
+ } else {
1007
+ break;
1008
+ }
1009
+ }
1010
+
1011
+ const result = await this._load(
1012
+ {
1013
+ route,
1014
+ info
1015
+ },
1016
+ no_cache
1017
+ );
1018
+ if (result) return result;
1019
+ }
1020
+
1021
+ return await this._load_error({
1022
+ status: 404,
1023
+ error: new Error(`Not found: ${info.url.pathname}`),
1024
+ url: info.url
1025
+ });
1026
+ }
1027
+
1028
+ /**
1029
+ *
1030
+ * @param {{
1031
+ * url: URL;
1032
+ * params: Record<string, string>;
1033
+ * stuff: Record<string, any>;
1034
+ * branch: Array<import('./types').BranchNode | undefined>;
1035
+ * status: number;
1036
+ * error?: Error;
1037
+ * }} opts
1038
+ */
1039
+ async _get_navigation_result_from_branch({ url, params, stuff, branch, status, error }) {
1040
+ const filtered = /** @type {import('./types').BranchNode[] } */ (branch.filter(Boolean));
1041
+ const redirect = filtered.find((f) => f.loaded && f.loaded.redirect);
1042
+
1043
+ /** @type {import('./types').NavigationResult} */
1044
+ const result = {
1045
+ redirect: redirect && redirect.loaded ? redirect.loaded.redirect : undefined,
1046
+ state: {
1047
+ url,
1048
+ params,
1049
+ branch,
1050
+ session_id: this.session_id
1051
+ },
1052
+ props: {
1053
+ components: filtered.map((node) => node.module.default)
1054
+ }
1055
+ };
1056
+
1057
+ for (let i = 0; i < filtered.length; i += 1) {
1058
+ const loaded = filtered[i].loaded;
1059
+ result.props[`props_${i}`] = loaded ? await loaded.props : null;
1060
+ }
1061
+
1062
+ if (!this.current.url || url.href !== this.current.url.href) {
1063
+ result.props.page = { url, params, status, error, stuff };
1064
+
1065
+ // TODO remove this for 1.0
1066
+ /**
1067
+ * @param {string} property
1068
+ * @param {string} replacement
1069
+ */
1070
+ const print_error = (property, replacement) => {
1071
+ Object.defineProperty(result.props.page, property, {
1072
+ get: () => {
1073
+ throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
1074
+ }
1075
+ });
1076
+ };
1077
+
1078
+ print_error('origin', 'origin');
1079
+ print_error('path', 'pathname');
1080
+ print_error('query', 'searchParams');
1081
+ }
1082
+
1083
+ const leaf = filtered[filtered.length - 1];
1084
+ const maxage = leaf.loaded && leaf.loaded.maxage;
1085
+
1086
+ if (maxage) {
1087
+ const key = url.pathname + url.search; // omit hash
1088
+ let ready = false;
1089
+
1090
+ const clear = () => {
1091
+ if (this.cache.get(key) === result) {
1092
+ this.cache.delete(key);
1093
+ }
1094
+
1095
+ unsubscribe();
1096
+ clearTimeout(timeout);
1097
+ };
1098
+
1099
+ const timeout = setTimeout(clear, maxage * 1000);
1100
+
1101
+ const unsubscribe = this.stores.session.subscribe(() => {
1102
+ if (ready) clear();
1103
+ });
1104
+
1105
+ ready = true;
1106
+
1107
+ this.cache.set(key, result);
1108
+ }
1109
+
1110
+ return result;
1111
+ }
1112
+
1113
+ /**
1114
+ * @param {{
1115
+ * status?: number;
1116
+ * error?: Error;
1117
+ * module: CSRComponent;
1118
+ * url: URL;
1119
+ * params: Record<string, string>;
1120
+ * stuff: Record<string, any>;
1121
+ * props?: Record<string, any>;
1122
+ * }} options
1123
+ * @returns
1124
+ */
1125
+ async _load_node({ status, error, module, url, params, stuff, props }) {
1126
+ /** @type {import('./types').BranchNode} */
1127
+ const node = {
1128
+ module,
1129
+ uses: {
1130
+ params: new Set(),
1131
+ url: false,
1132
+ session: false,
1133
+ stuff: false,
1134
+ dependencies: new Set()
1135
+ },
1136
+ loaded: null,
1137
+ stuff
1138
+ };
1139
+
1140
+ if (props) {
1141
+ // shadow endpoint props means we need to mark this URL as a dependency of itself
1142
+ node.uses.dependencies.add(url.href);
1143
+ }
1144
+
1145
+ /** @type {Record<string, string>} */
1146
+ const uses_params = {};
1147
+ for (const key in params) {
1148
+ Object.defineProperty(uses_params, key, {
1149
+ get() {
1150
+ node.uses.params.add(key);
1151
+ return params[key];
1152
+ },
1153
+ enumerable: true
1154
+ });
1155
+ }
1156
+
1157
+ const session = this.$session;
1158
+
1159
+ if (module.load) {
1160
+ const { started } = this;
1161
+
1162
+ /** @type {import('types/page').LoadInput | import('types/page').ErrorLoadInput} */
1163
+ const load_input = {
1164
+ params: uses_params,
1165
+ props: props || {},
1166
+ get url() {
1167
+ node.uses.url = true;
1168
+ return url;
1169
+ },
1170
+ get session() {
1171
+ node.uses.session = true;
1172
+ return session;
1173
+ },
1174
+ get stuff() {
1175
+ node.uses.stuff = true;
1176
+ return { ...stuff };
1177
+ },
1178
+ fetch(resource, info) {
1179
+ const requested = typeof resource === 'string' ? resource : resource.url;
1180
+ const { href } = new URL(requested, url);
1181
+ node.uses.dependencies.add(href);
1182
+
1183
+ return started ? fetch(resource, info) : initial_fetch(resource, info);
1184
+ }
1185
+ };
1186
+
1187
+ if (import.meta.env.DEV) {
1188
+ // TODO remove this for 1.0
1189
+ Object.defineProperty(load_input, 'page', {
1190
+ get: () => {
1191
+ throw new Error('`page` in `load` functions has been replaced by `url` and `params`');
1192
+ }
1193
+ });
1194
+ }
1195
+
1196
+ if (error) {
1197
+ /** @type {import('types/page').ErrorLoadInput} */ (load_input).status = status;
1198
+ /** @type {import('types/page').ErrorLoadInput} */ (load_input).error = error;
1199
+ }
1200
+
1201
+ const loaded = await module.load.call(null, load_input);
1202
+
1203
+ if (!loaded) {
1204
+ throw new Error('load function must return a value');
1205
+ }
1206
+
1207
+ node.loaded = normalize(loaded);
1208
+ if (node.loaded.stuff) node.stuff = node.loaded.stuff;
1209
+ } else if (props) {
1210
+ node.loaded = normalize({ props });
1211
+ }
1212
+
1213
+ return node;
1214
+ }
1215
+
1216
+ /**
1217
+ * @param {import('./types').NavigationCandidate} selected
1218
+ * @param {boolean} no_cache
1219
+ * @returns {Promise<import('./types').NavigationResult | undefined>} undefined if fallthrough
1220
+ */
1221
+ async _load({ route, info: { url, path } }, no_cache) {
1222
+ const key = url.pathname + url.search;
1223
+
1224
+ if (!no_cache) {
1225
+ const cached = this.cache.get(key);
1226
+ if (cached) return cached;
1227
+ }
1228
+
1229
+ const [pattern, a, b, get_params, has_shadow] = route;
1230
+ const params = get_params
1231
+ ? // the pattern is for the route which we've already matched to this path
1232
+ get_params(/** @type {RegExpExecArray} */ (pattern.exec(path)))
1233
+ : {};
1234
+
1235
+ const changed = this.current.url && {
1236
+ url: key !== this.current.url.pathname + this.current.url.search,
1237
+ params: Object.keys(params).filter((key) => this.current.params[key] !== params[key]),
1238
+ session: this.session_id !== this.current.session_id
1239
+ };
1240
+
1241
+ /** @type {Array<import('./types').BranchNode | undefined>} */
1242
+ let branch = [];
1243
+
1244
+ /** @type {Record<string, any>} */
1245
+ let stuff = {};
1246
+ let stuff_changed = false;
1247
+
1248
+ /** @type {number | undefined} */
1249
+ let status = 200;
1250
+
1251
+ /** @type {Error | undefined} */
1252
+ let error;
1253
+
1254
+ // preload modules
1255
+ a.forEach((loader) => loader());
1256
+
1257
+ load: for (let i = 0; i < a.length; i += 1) {
1258
+ /** @type {import('./types').BranchNode | undefined} */
1259
+ let node;
1260
+
1261
+ try {
1262
+ if (!a[i]) continue;
1263
+
1264
+ const module = await a[i]();
1265
+ const previous = this.current.branch[i];
1266
+
1267
+ const changed_since_last_render =
1268
+ !previous ||
1269
+ module !== previous.module ||
1270
+ (changed.url && previous.uses.url) ||
1271
+ changed.params.some((param) => previous.uses.params.has(param)) ||
1272
+ (changed.session && previous.uses.session) ||
1273
+ Array.from(previous.uses.dependencies).some((dep) => this.invalid.has(dep)) ||
1274
+ (stuff_changed && previous.uses.stuff);
1275
+
1276
+ if (changed_since_last_render) {
1277
+ /** @type {Record<string, any>} */
1278
+ let props = {};
1279
+
1280
+ if (has_shadow && i === a.length - 1) {
1281
+ const res = await fetch(
1282
+ `${url.pathname}${url.pathname.endsWith('/') ? '' : '/'}__data.json`,
1283
+ {
1284
+ headers: {
1285
+ 'x-sveltekit-load': 'true'
1286
+ }
1287
+ }
1288
+ );
1289
+
1290
+ if (res.ok) {
1291
+ const redirect = res.headers.get('x-sveltekit-location');
1292
+
1293
+ if (redirect) {
1294
+ return {
1295
+ redirect,
1296
+ props: {},
1297
+ state: this.current
1298
+ };
1299
+ }
1300
+
1301
+ props = await res.json();
1302
+ } else {
1303
+ status = res.status;
1304
+ error = new Error('Failed to load data');
1305
+ }
1306
+ }
1307
+
1308
+ if (!error) {
1309
+ node = await this._load_node({
1310
+ module,
1311
+ url,
1312
+ params,
1313
+ props,
1314
+ stuff
1315
+ });
1316
+ }
1317
+
1318
+ if (node && node.loaded) {
1319
+ if (node.loaded.fallthrough) {
1320
+ return;
1321
+ }
1322
+ if (node.loaded.error) {
1323
+ status = node.loaded.status;
1324
+ error = node.loaded.error;
1325
+ }
1326
+
1327
+ if (node.loaded.redirect) {
1328
+ return {
1329
+ redirect: node.loaded.redirect,
1330
+ props: {},
1331
+ state: this.current
1332
+ };
1333
+ }
1334
+
1335
+ if (node.loaded.stuff) {
1336
+ stuff_changed = true;
1337
+ }
1338
+ }
1339
+ } else {
1340
+ node = previous;
1341
+ }
1342
+ } catch (e) {
1343
+ status = 500;
1344
+ error = coalesce_to_error(e);
1345
+ }
1346
+
1347
+ if (error) {
1348
+ while (i--) {
1349
+ if (b[i]) {
1350
+ let error_loaded;
1351
+
1352
+ /** @type {import('./types').BranchNode | undefined} */
1353
+ let node_loaded;
1354
+ let j = i;
1355
+ while (!(node_loaded = branch[j])) {
1356
+ j -= 1;
1357
+ }
1358
+
1359
+ try {
1360
+ error_loaded = await this._load_node({
1361
+ status,
1362
+ error,
1363
+ module: await b[i](),
1364
+ url,
1365
+ params,
1366
+ stuff: node_loaded.stuff
1367
+ });
1368
+
1369
+ if (error_loaded && error_loaded.loaded && error_loaded.loaded.error) {
1370
+ continue;
1371
+ }
1372
+
1373
+ if (error_loaded && error_loaded.loaded && error_loaded.loaded.stuff) {
1374
+ stuff = {
1375
+ ...stuff,
1376
+ ...error_loaded.loaded.stuff
1377
+ };
1378
+ }
1379
+
1380
+ branch = branch.slice(0, j + 1).concat(error_loaded);
1381
+ break load;
1382
+ } catch (e) {
1383
+ continue;
1384
+ }
1385
+ }
1386
+ }
1387
+
1388
+ return await this._load_error({
1389
+ status,
1390
+ error,
1391
+ url
1392
+ });
1393
+ } else {
1394
+ if (node && node.loaded && node.loaded.stuff) {
1395
+ stuff = {
1396
+ ...stuff,
1397
+ ...node.loaded.stuff
1398
+ };
1399
+ }
1400
+
1401
+ branch.push(node);
1402
+ }
1403
+ }
1404
+
1405
+ return await this._get_navigation_result_from_branch({
1406
+ url,
1407
+ params,
1408
+ stuff,
1409
+ branch,
1410
+ status,
1411
+ error
1412
+ });
1413
+ }
1414
+
1415
+ /**
1416
+ * @param {{
1417
+ * status: number;
1418
+ * error: Error;
1419
+ * url: URL;
1420
+ * }} opts
1421
+ */
1422
+ async _load_error({ status, error, url }) {
1423
+ /** @type {Record<string, string>} */
1424
+ const params = {}; // error page does not have params
1425
+
1426
+ const node = await this._load_node({
1427
+ module: await this.fallback[0],
1428
+ url,
1429
+ params,
1430
+ stuff: {}
1431
+ });
1432
+ const error_node = await this._load_node({
1433
+ status,
1434
+ error,
1435
+ module: await this.fallback[1],
1436
+ url,
1437
+ params,
1438
+ stuff: (node && node.loaded && node.loaded.stuff) || {}
1439
+ });
1440
+
1441
+ const branch = [node, error_node];
1442
+ const stuff = { ...node?.loaded?.stuff, ...error_node?.loaded?.stuff };
1443
+
1444
+ return await this._get_navigation_result_from_branch({
1445
+ url,
1446
+ params,
1447
+ stuff,
1448
+ branch,
1449
+ status,
1450
+ error
1451
+ });
1452
+ }
1453
+ }
1454
+
1455
+ /**
1456
+ * @param {{
1457
+ * paths: {
1458
+ * assets: string;
1459
+ * base: string;
1460
+ * },
1461
+ * target: Node;
1462
+ * session: any;
1463
+ * route: boolean;
1464
+ * spa: boolean;
1465
+ * trailing_slash: import('types/internal').TrailingSlash;
1466
+ * hydrate: {
1467
+ * status: number;
1468
+ * error: Error;
1469
+ * nodes: Array<Promise<import('types/internal').CSRComponent>>;
1470
+ * url: URL;
1471
+ * params: Record<string, string>;
1472
+ * };
1473
+ * }} opts
1474
+ */
1475
+ async function start({ paths, target, session, route, spa, trailing_slash, hydrate }) {
1476
+ const renderer = new Renderer({
1477
+ Root,
1478
+ fallback,
1479
+ target,
1480
+ session
1481
+ });
1482
+
1483
+ const router = route
1484
+ ? new Router({
1485
+ base: paths.base,
1486
+ routes,
1487
+ trailing_slash,
1488
+ renderer
1489
+ })
1490
+ : null;
1491
+
1492
+ init({ router, renderer });
1493
+ set_paths(paths);
1494
+
1495
+ if (hydrate) await renderer.start(hydrate);
1496
+ if (router) {
1497
+ if (spa) router.goto(location.href, { replaceState: true }, []);
1498
+ router.init_listeners();
1499
+ }
1500
+
1501
+ dispatchEvent(new CustomEvent('sveltekit:start'));
1502
+ }
1503
+
1504
+ export { start };