@sveltejs/kit 1.0.0-next.26 → 1.0.0-next.260

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