@sveltejs/kit 1.0.0-next.36 → 1.0.0-next.362

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 (64) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +28 -0
  3. package/assets/app/navigation.js +24 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/app/stores.js +97 -0
  6. package/assets/client/singletons.js +13 -0
  7. package/assets/client/start.js +1793 -0
  8. package/assets/components/error.svelte +18 -2
  9. package/assets/env.js +8 -0
  10. package/assets/paths.js +13 -0
  11. package/assets/server/index.js +3460 -0
  12. package/dist/chunks/_commonjsHelpers.js +3 -0
  13. package/dist/chunks/error.js +661 -0
  14. package/dist/chunks/index.js +15744 -0
  15. package/dist/chunks/index2.js +210 -0
  16. package/dist/chunks/multipart-parser.js +445 -0
  17. package/dist/chunks/sync.js +980 -0
  18. package/dist/chunks/write_tsconfig.js +274 -0
  19. package/dist/cli.js +64 -117
  20. package/dist/hooks.js +28 -0
  21. package/dist/node/polyfills.js +12237 -0
  22. package/dist/node.js +5855 -0
  23. package/dist/vite.js +3141 -0
  24. package/package.json +100 -55
  25. package/types/ambient.d.ts +345 -0
  26. package/types/index.d.ts +293 -0
  27. package/types/internal.d.ts +318 -0
  28. package/types/private.d.ts +235 -0
  29. package/CHANGELOG.md +0 -377
  30. package/assets/runtime/app/navigation.js +0 -23
  31. package/assets/runtime/app/navigation.js.map +0 -1
  32. package/assets/runtime/app/paths.js +0 -2
  33. package/assets/runtime/app/paths.js.map +0 -1
  34. package/assets/runtime/app/stores.js +0 -78
  35. package/assets/runtime/app/stores.js.map +0 -1
  36. package/assets/runtime/internal/singletons.js +0 -15
  37. package/assets/runtime/internal/singletons.js.map +0 -1
  38. package/assets/runtime/internal/start.js +0 -614
  39. package/assets/runtime/internal/start.js.map +0 -1
  40. package/assets/runtime/utils-85ebcc60.js +0 -18
  41. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  42. package/dist/api.js +0 -28
  43. package/dist/api.js.map +0 -1
  44. package/dist/cli.js.map +0 -1
  45. package/dist/create_app.js +0 -502
  46. package/dist/create_app.js.map +0 -1
  47. package/dist/index.js +0 -327
  48. package/dist/index.js.map +0 -1
  49. package/dist/index2.js +0 -3497
  50. package/dist/index2.js.map +0 -1
  51. package/dist/index3.js +0 -296
  52. package/dist/index3.js.map +0 -1
  53. package/dist/index4.js +0 -311
  54. package/dist/index4.js.map +0 -1
  55. package/dist/index5.js +0 -221
  56. package/dist/index5.js.map +0 -1
  57. package/dist/index6.js +0 -730
  58. package/dist/index6.js.map +0 -1
  59. package/dist/renderer.js +0 -2429
  60. package/dist/renderer.js.map +0 -1
  61. package/dist/standard.js +0 -100
  62. package/dist/standard.js.map +0 -1
  63. package/dist/utils.js +0 -61
  64. package/dist/utils.js.map +0 -1
@@ -1,78 +0,0 @@
1
- import { getContext } from 'svelte';
2
-
3
- // const ssr = (import.meta as any).env.SSR;
4
- const ssr = typeof window === 'undefined'; // TODO why doesn't previous line work in build?
5
-
6
- // TODO remove this (for 1.0? after 1.0?)
7
- let warned = false;
8
- function stores() {
9
- if (!warned) {
10
- console.error('stores() is deprecated; use getStores() instead');
11
- warned = true;
12
- }
13
- return getStores();
14
- }
15
-
16
- const getStores = () => {
17
- const stores = getContext('__svelte__');
18
-
19
- return {
20
- page: {
21
- subscribe: stores.page.subscribe
22
- },
23
- navigating: {
24
- subscribe: stores.navigating.subscribe
25
- },
26
- get preloading() {
27
- console.error('stores.preloading is deprecated; use stores.navigating instead');
28
- return {
29
- subscribe: stores.navigating.subscribe
30
- };
31
- },
32
- session: stores.session
33
- };
34
- };
35
-
36
- const page = {
37
- subscribe(fn) {
38
- const store = getStores().page;
39
- return store.subscribe(fn);
40
- }
41
- };
42
-
43
- const navigating = {
44
- subscribe(fn) {
45
- const store = getStores().navigating;
46
- return store.subscribe(fn);
47
- }
48
- };
49
-
50
- const error = (verb) => {
51
- throw new Error(
52
- ssr
53
- ? `Can only ${verb} session store in browser`
54
- : `Cannot ${verb} session store before subscribing`
55
- );
56
- };
57
-
58
- const session = {
59
- subscribe(fn) {
60
- const store = getStores().session;
61
-
62
- if (!ssr) {
63
- session.set = store.set;
64
- session.update = store.update;
65
- }
66
-
67
- return store.subscribe(fn);
68
- },
69
- set: (value) => {
70
- error('set');
71
- },
72
- update: (updater) => {
73
- error('update');
74
- }
75
- };
76
-
77
- export { getStores, navigating, page, session, stores };
78
- //# sourceMappingURL=stores.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stores.js","sources":["../../../src/runtime/app/stores/index.js"],"sourcesContent":["import { getContext } from 'svelte';\n\n// const ssr = (import.meta as any).env.SSR;\nconst ssr = typeof window === 'undefined'; // TODO why doesn't previous line work in build?\n\n// TODO remove this (for 1.0? after 1.0?)\nlet warned = false;\nexport function stores() {\n\tif (!warned) {\n\t\tconsole.error('stores() is deprecated; use getStores() instead');\n\t\twarned = true;\n\t}\n\treturn getStores();\n}\n\nexport const getStores = () => {\n\tconst stores = getContext('__svelte__');\n\n\treturn {\n\t\tpage: {\n\t\t\tsubscribe: stores.page.subscribe\n\t\t},\n\t\tnavigating: {\n\t\t\tsubscribe: stores.navigating.subscribe\n\t\t},\n\t\tget preloading() {\n\t\t\tconsole.error('stores.preloading is deprecated; use stores.navigating instead');\n\t\t\treturn {\n\t\t\t\tsubscribe: stores.navigating.subscribe\n\t\t\t};\n\t\t},\n\t\tsession: stores.session\n\t};\n};\n\nexport const page = {\n\tsubscribe(fn) {\n\t\tconst store = getStores().page;\n\t\treturn store.subscribe(fn);\n\t}\n};\n\nexport const navigating = {\n\tsubscribe(fn) {\n\t\tconst store = getStores().navigating;\n\t\treturn store.subscribe(fn);\n\t}\n};\n\nconst error = (verb) => {\n\tthrow new Error(\n\t\tssr\n\t\t\t? `Can only ${verb} session store in browser`\n\t\t\t: `Cannot ${verb} session store before subscribing`\n\t);\n};\n\nexport const session = {\n\tsubscribe(fn) {\n\t\tconst store = getStores().session;\n\n\t\tif (!ssr) {\n\t\t\tsession.set = store.set;\n\t\t\tsession.update = store.update;\n\t\t}\n\n\t\treturn store.subscribe(fn);\n\t},\n\tset: (value) => {\n\t\terror('set');\n\t},\n\tupdate: (updater) => {\n\t\terror('update');\n\t}\n};\n"],"names":[],"mappings":";;AAEA;AACA,MAAM,GAAG,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC;AAC1C;AACA;AACA,IAAI,MAAM,GAAG,KAAK,CAAC;AACZ,SAAS,MAAM,GAAG;AACzB,CAAC,IAAI,CAAC,MAAM,EAAE;AACd,EAAE,OAAO,CAAC,KAAK,CAAC,iDAAiD,CAAC,CAAC;AACnE,EAAE,MAAM,GAAG,IAAI,CAAC;AAChB,EAAE;AACF,CAAC,OAAO,SAAS,EAAE,CAAC;AACpB,CAAC;AACD;AACY,MAAC,SAAS,GAAG,MAAM;AAC/B,CAAC,MAAM,MAAM,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;AACzC;AACA,CAAC,OAAO;AACR,EAAE,IAAI,EAAE;AACR,GAAG,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,SAAS;AACnC,GAAG;AACH,EAAE,UAAU,EAAE;AACd,GAAG,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,SAAS;AACzC,GAAG;AACH,EAAE,IAAI,UAAU,GAAG;AACnB,GAAG,OAAO,CAAC,KAAK,CAAC,gEAAgE,CAAC,CAAC;AACnF,GAAG,OAAO;AACV,IAAI,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,SAAS;AAC1C,IAAI,CAAC;AACL,GAAG;AACH,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO;AACzB,EAAE,CAAC;AACH,EAAE;AACF;AACY,MAAC,IAAI,GAAG;AACpB,CAAC,SAAS,CAAC,EAAE,EAAE;AACf,EAAE,MAAM,KAAK,GAAG,SAAS,EAAE,CAAC,IAAI,CAAC;AACjC,EAAE,OAAO,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAC7B,EAAE;AACF,EAAE;AACF;AACY,MAAC,UAAU,GAAG;AAC1B,CAAC,SAAS,CAAC,EAAE,EAAE;AACf,EAAE,MAAM,KAAK,GAAG,SAAS,EAAE,CAAC,UAAU,CAAC;AACvC,EAAE,OAAO,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAC7B,EAAE;AACF,EAAE;AACF;AACA,MAAM,KAAK,GAAG,CAAC,IAAI,KAAK;AACxB,CAAC,MAAM,IAAI,KAAK;AAChB,EAAE,GAAG;AACL,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,yBAAyB,CAAC;AAChD,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,iCAAiC,CAAC;AACtD,EAAE,CAAC;AACH,CAAC,CAAC;AACF;AACY,MAAC,OAAO,GAAG;AACvB,CAAC,SAAS,CAAC,EAAE,EAAE;AACf,EAAE,MAAM,KAAK,GAAG,SAAS,EAAE,CAAC,OAAO,CAAC;AACpC;AACA,EAAE,IAAI,CAAC,GAAG,EAAE;AACZ,GAAG,OAAO,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;AAC3B,GAAG,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;AACjC,GAAG;AACH;AACA,EAAE,OAAO,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAC7B,EAAE;AACF,CAAC,GAAG,EAAE,CAAC,KAAK,KAAK;AACjB,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;AACf,EAAE;AACF,CAAC,MAAM,EAAE,CAAC,OAAO,KAAK;AACtB,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;AAClB,EAAE;AACF;;;;"}
@@ -1,15 +0,0 @@
1
- let router;
2
- let renderer;
3
- let base;
4
- let assets;
5
-
6
- function init(opts) {
7
- ({ router, renderer } = opts);
8
- }
9
-
10
- function set_paths(paths) {
11
- ({ base, assets } = paths);
12
- }
13
-
14
- export { assets, base, init, renderer, router, set_paths };
15
- //# sourceMappingURL=singletons.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"singletons.js","sources":["../../../src/runtime/internal/singletons.js"],"sourcesContent":["export let router;\nexport let renderer;\nexport let base;\nexport let assets;\n\nexport function init(opts) {\n\t({ router, renderer } = opts);\n}\n\nexport function set_paths(paths) {\n\t({ base, assets } = paths);\n}\n"],"names":[],"mappings":"AAAU,IAAC,OAAO;AACR,IAAC,SAAS;AACV,IAAC,KAAK;AACN,IAAC,OAAO;AAClB;AACO,SAAS,IAAI,CAAC,IAAI,EAAE;AAC3B,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE;AAC/B,CAAC;AACD;AACO,SAAS,SAAS,CAAC,KAAK,EAAE;AACjC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE;AAC5B;;;;"}
@@ -1,614 +0,0 @@
1
- import Root from '../../generated/root.svelte';
2
- import { pages, ignore, layout } from '../../generated/manifest.js';
3
- import { f as find_anchor, g as get_base_uri } from '../utils-85ebcc60.js';
4
- import { writable } from 'svelte/store';
5
- import { init, set_paths } from './singletons.js';
6
-
7
- function which(event) {
8
- return event.which === null ? event.button : event.which;
9
- }
10
-
11
- function scroll_state() {
12
- return {
13
- x: pageXOffset,
14
- y: pageYOffset
15
- };
16
- }
17
-
18
- class Router {
19
- constructor({ base, host, pages, ignore }) {
20
- this.base = base;
21
- this.host = host;
22
- this.pages = pages;
23
- this.ignore = ignore;
24
-
25
- this.history = window.history || {
26
- pushState: () => {},
27
- replaceState: () => {},
28
- scrollRestoration: 'auto'
29
- };
30
- }
31
-
32
- init({ renderer }) {
33
- this.renderer = renderer;
34
- renderer.router = this;
35
-
36
- if ('scrollRestoration' in this.history) {
37
- this.history.scrollRestoration = 'manual';
38
- }
39
-
40
- // Adopted from Nuxt.js
41
- // Reset scrollRestoration to auto when leaving page, allowing page reload
42
- // and back-navigation from other pages to use the browser to restore the
43
- // scrolling position.
44
- addEventListener('beforeunload', () => {
45
- this.history.scrollRestoration = 'auto';
46
- });
47
-
48
- // Setting scrollRestoration to manual again when returning to this page.
49
- addEventListener('load', () => {
50
- this.history.scrollRestoration = 'manual';
51
- });
52
-
53
- // There's no API to capture the scroll location right before the user
54
- // hits the back/forward button, so we listen for scroll events
55
- let scroll_timer;
56
- addEventListener('scroll', () => {
57
- clearTimeout(scroll_timer);
58
- scroll_timer = setTimeout(() => {
59
- // Store the scroll location in the history
60
- // This will persist even if we navigate away from the site and come back
61
- const new_state = {
62
- ...(history.state || {}),
63
- 'sveltekit:scroll': scroll_state()
64
- };
65
- history.replaceState(new_state, document.title, window.location);
66
- }, 50);
67
- });
68
-
69
- addEventListener('click', (event) => {
70
- // Adapted from https://github.com/visionmedia/page.js
71
- // MIT license https://github.com/visionmedia/page.js#license
72
- if (which(event) !== 1) return;
73
- if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
74
- if (event.defaultPrevented) return;
75
-
76
- const a = find_anchor(event.target);
77
- if (!a) return;
78
-
79
- if (!a.href) return;
80
-
81
- // check if link is inside an svg
82
- // in this case, both href and target are always inside an object
83
- const svg = typeof a.href === 'object' && a.href.constructor.name === 'SVGAnimatedString';
84
- const href = String(svg ? a.href.baseVal : a.href);
85
-
86
- if (href === location.href) {
87
- if (!location.hash) event.preventDefault();
88
- return;
89
- }
90
-
91
- // Ignore if tag has
92
- // 1. 'download' attribute
93
- // 2. rel='external' attribute
94
- if (a.hasAttribute('download') || a.getAttribute('rel') === 'external') return;
95
-
96
- // Ignore if <a> has a target
97
- if (svg ? a.target.baseVal : a.target) return;
98
-
99
- const url = new URL(href);
100
-
101
- // Don't handle hash changes
102
- if (url.pathname === location.pathname && url.search === location.search) return;
103
-
104
- const selected = this.select(url);
105
- if (selected) {
106
- const noscroll = a.hasAttribute('sveltekit:noscroll');
107
- this.renderer.notify(selected);
108
- this.history.pushState({}, '', url.href);
109
- this.navigate(selected, noscroll ? scroll_state() : false, url.hash);
110
- event.preventDefault();
111
- }
112
- });
113
-
114
- addEventListener('popstate', (event) => {
115
- if (event.state) {
116
- const url = new URL(location.href);
117
- const selected = this.select(url);
118
- if (selected) {
119
- this.navigate(selected, event.state['sveltekit:scroll']);
120
- } else {
121
- // eslint-disable-next-line
122
- location.href = location.href; // nosonar
123
- }
124
- }
125
- });
126
-
127
- // make it possible to reset focus
128
- document.body.setAttribute('tabindex', '-1');
129
-
130
- // load current page
131
- this.history.replaceState({}, '', location.href);
132
-
133
- const selected = this.select(new URL(location.href));
134
- if (selected) return this.renderer.start(selected);
135
- }
136
-
137
- select(url) {
138
- if (url.origin !== location.origin) return null;
139
- if (!url.pathname.startsWith(this.base)) return null;
140
-
141
- let path = url.pathname.slice(this.base.length);
142
-
143
- if (path === '') {
144
- path = '/';
145
- }
146
-
147
- // avoid accidental clashes between server routes and page routes
148
- if (this.ignore.some((pattern) => pattern.test(path))) return;
149
-
150
- for (const route of this.pages) {
151
- const match = route.pattern.exec(path);
152
-
153
- if (match) {
154
- const query = new URLSearchParams(url.search);
155
- const params = route.params(match);
156
-
157
- const page = { host: this.host, path, query, params };
158
-
159
- return { href: url.href, route, match, page };
160
- }
161
- }
162
- }
163
-
164
- async goto(href, { noscroll = false, replaceState = false } = {}) {
165
- const url = new URL(href, get_base_uri(document));
166
- const selected = this.select(url);
167
-
168
- if (selected) {
169
- this.renderer.notify(selected);
170
-
171
- // TODO shouldn't need to pass the hash here
172
- this.history[replaceState ? 'replaceState' : 'pushState']({}, '', href);
173
- return this.navigate(selected, noscroll ? scroll_state() : false, url.hash);
174
- }
175
-
176
- location.href = href;
177
- return new Promise(() => {
178
- /* never resolves */
179
- });
180
- }
181
-
182
- async navigate(selected, scroll, hash) {
183
- // remove trailing slashes
184
- if (location.pathname.endsWith('/') && location.pathname !== '/') {
185
- history.replaceState({}, '', `${location.pathname.slice(0, -1)}${location.search}`);
186
- }
187
-
188
- await this.renderer.render(selected);
189
-
190
- document.body.focus();
191
-
192
- const deep_linked = hash && document.getElementById(hash.slice(1));
193
- if (scroll) {
194
- scrollTo(scroll.x, scroll.y);
195
- } else if (deep_linked) {
196
- // scroll is an element id (from a hash), we need to compute y
197
- scrollTo(0, deep_linked.getBoundingClientRect().top + scrollY);
198
- } else {
199
- scrollTo(0, 0);
200
- }
201
- }
202
- }
203
-
204
- function page_store(value) {
205
- const store = writable(value);
206
- let ready = true;
207
-
208
- function notify() {
209
- ready = true;
210
- store.update((val) => val);
211
- }
212
-
213
- function set(new_value) {
214
- ready = false;
215
- store.set(new_value);
216
- }
217
-
218
- function subscribe(run) {
219
- let old_value;
220
- return store.subscribe((new_value) => {
221
- if (old_value === undefined || (ready && new_value !== old_value)) {
222
- run((old_value = new_value));
223
- }
224
- });
225
- }
226
-
227
- return { notify, set, subscribe };
228
- }
229
-
230
- class Renderer {
231
- constructor({ Root, layout, target, error, status, preloaded, session }) {
232
- this.Root = Root;
233
- this.layout = layout;
234
- this.layout_loader = () => layout;
235
-
236
- // TODO ideally we wouldn't need to store these...
237
- this.target = target;
238
-
239
- this.initial = {
240
- preloaded,
241
- error,
242
- status
243
- };
244
-
245
- this.current = {
246
- page: null,
247
- query: null,
248
- session_changed: false,
249
- nodes: [],
250
- contexts: []
251
- };
252
-
253
- this.caches = new Map();
254
-
255
- this.prefetching = {
256
- href: null,
257
- promise: null
258
- };
259
-
260
- this.stores = {
261
- page: page_store({}),
262
- navigating: writable(null),
263
- session: writable(session)
264
- };
265
-
266
- this.$session = null;
267
-
268
- this.root = null;
269
-
270
- const trigger_prefetch = (event) => {
271
- const a = find_anchor(event.target);
272
-
273
- if (a && a.hasAttribute('sveltekit:prefetch')) {
274
- this.prefetch(new URL(a.href));
275
- }
276
- };
277
-
278
- let mousemove_timeout;
279
- const handle_mousemove = (event) => {
280
- clearTimeout(mousemove_timeout);
281
- mousemove_timeout = setTimeout(() => {
282
- trigger_prefetch(event);
283
- }, 20);
284
- };
285
-
286
- addEventListener('touchstart', trigger_prefetch);
287
- addEventListener('mousemove', handle_mousemove);
288
-
289
- let ready = false;
290
- this.stores.session.subscribe(async (value) => {
291
- this.$session = value;
292
-
293
- if (!ready) return;
294
- this.current.session_changed = true;
295
-
296
- const selected = this.router.select(new URL(location.href));
297
- this.render(selected);
298
- });
299
- ready = true;
300
- }
301
-
302
- async start(selected) {
303
- const props = {
304
- stores: this.stores,
305
- error: this.initial.error,
306
- status: this.initial.status,
307
- page: selected.page
308
- };
309
-
310
- if (this.initial.error) {
311
- props.components = [this.layout.default];
312
- } else {
313
- const hydrated = await this.hydrate(selected);
314
-
315
- if (hydrated.redirect) {
316
- throw new Error('TODO client-side redirects');
317
- }
318
-
319
- Object.assign(props, hydrated.props);
320
- this.current = hydrated.state;
321
- }
322
-
323
- // remove dev-mode SSR <style> insert, since it doesn't apply
324
- // to hydrated markup (HMR requires hashes to be rewritten)
325
- // TODO only in dev
326
- // TODO it seems this doesn't always work with the classname
327
- // stabilisation in vite-plugin-svelte? see e.g.
328
- // hn.svelte.dev
329
- // const style = document.querySelector('style[data-svelte]');
330
- // if (style) style.remove();
331
-
332
- this.root = new this.Root({
333
- target: this.target,
334
- props,
335
- hydrate: true
336
- });
337
-
338
- this.initial = null;
339
- }
340
-
341
- notify(selected) {
342
- this.stores.navigating.set({
343
- from: this.current.page,
344
- to: selected.page
345
- });
346
- }
347
-
348
- async render(selected) {
349
- const token = (this.token = {});
350
-
351
- const hydrated = await this.hydrate(selected);
352
-
353
- if (this.token === token) {
354
- // check render wasn't aborted
355
- this.current = hydrated.state;
356
-
357
- this.root.$set(hydrated.props);
358
- this.stores.navigating.set(null);
359
- }
360
- }
361
-
362
- async hydrate({ route, page }) {
363
- const props = {
364
- error: null,
365
- status: 200,
366
- components: []
367
- };
368
-
369
- const fetcher = (url, opts) => {
370
- if (this.initial) {
371
- const script = document.querySelector(`script[type="svelte-data"][url="${url}"]`);
372
- if (script) {
373
- const { body, ...init } = JSON.parse(script.textContent);
374
- return Promise.resolve(new Response(body, init));
375
- }
376
- }
377
-
378
- return fetch(url, opts);
379
- };
380
-
381
- const query = page.query.toString();
382
-
383
- const state = {
384
- page,
385
- query,
386
- session_changed: false,
387
- nodes: [],
388
- contexts: []
389
- };
390
-
391
- const component_promises = [this.layout_loader(), ...route.parts.map((loader) => loader())];
392
- const props_promises = [];
393
-
394
- let context;
395
- let redirect;
396
-
397
- const changed = {
398
- params: Object.keys(page.params).filter((key) => {
399
- return !this.current.page || this.current.page.params[key] !== page.params[key];
400
- }),
401
- query: query !== this.current.query,
402
- session: this.current.session_changed,
403
- context: false
404
- };
405
-
406
- try {
407
- for (let i = 0; i < component_promises.length; i += 1) {
408
- const previous = this.current.nodes[i];
409
- const previous_context = this.current.contexts[i];
410
-
411
- const { default: component, load } = await component_promises[i];
412
- props.components[i] = component;
413
-
414
- const changed_since_last_render =
415
- !previous ||
416
- component !== previous.component ||
417
- changed.params.some((param) => previous.uses.params.has(param)) ||
418
- (changed.query && previous.uses.query) ||
419
- (changed.session && previous.uses.session) ||
420
- (changed.context && previous.uses.context);
421
-
422
- if (changed_since_last_render) {
423
- const hash = page.path + query;
424
-
425
- // see if we have some cached data
426
- const cache = this.caches.get(component);
427
- const cached = cache && cache.get(hash);
428
-
429
- let node;
430
- let loaded;
431
-
432
- if (cached && (!changed.context || !cached.node.uses.context)) {
433
- ({ node, loaded } = cached);
434
- } else {
435
- node = {
436
- component,
437
- uses: {
438
- params: new Set(),
439
- query: false,
440
- session: false,
441
- context: false
442
- }
443
- };
444
-
445
- const params = {};
446
- for (const key in page.params) {
447
- Object.defineProperty(params, key, {
448
- get() {
449
- node.uses.params.add(key);
450
- return page.params[key];
451
- },
452
- enumerable: true
453
- });
454
- }
455
-
456
- const session = this.$session;
457
-
458
- loaded =
459
- load &&
460
- (await load.call(null, {
461
- page: {
462
- host: page.host,
463
- path: page.path,
464
- params,
465
- get query() {
466
- node.uses.query = true;
467
- return page.query;
468
- }
469
- },
470
- get session() {
471
- node.uses.session = true;
472
- return session;
473
- },
474
- get context() {
475
- node.uses.context = true;
476
- return { ...context };
477
- },
478
- fetch: fetcher
479
- }));
480
- }
481
-
482
- if (loaded) {
483
- if (loaded.error) {
484
- let error = loaded.error;
485
- if (typeof error === 'string') {
486
- error = new Error(error);
487
- }
488
- if (!(error instanceof Error)) {
489
- error = new Error(
490
- `"error" property returned from load() must be a string or instance of Error, received type "${typeof error}"`
491
- );
492
- }
493
- error.status = loaded.status;
494
- throw error;
495
- }
496
-
497
- if (loaded.redirect) {
498
- redirect = loaded.redirect;
499
- break;
500
- }
501
-
502
- if (loaded.context) {
503
- changed.context = true;
504
-
505
- context = {
506
- ...context,
507
- ...loaded.context
508
- };
509
- }
510
-
511
- if (loaded.maxage) {
512
- if (!this.caches.has(component)) {
513
- this.caches.set(component, new Map());
514
- }
515
-
516
- const cache = this.caches.get(component);
517
- const cached = { node, loaded };
518
-
519
- cache.set(hash, cached);
520
-
521
- let ready = false;
522
-
523
- const timeout = setTimeout(() => {
524
- clear();
525
- }, loaded.maxage * 1000);
526
-
527
- const clear = () => {
528
- if (cache.get(hash) === cached) {
529
- cache.delete(hash);
530
- }
531
-
532
- unsubscribe();
533
- clearTimeout(timeout);
534
- };
535
-
536
- const unsubscribe = this.stores.session.subscribe(() => {
537
- if (ready) clear();
538
- });
539
-
540
- ready = true;
541
- }
542
-
543
- props_promises[i] = loaded.props;
544
- }
545
-
546
- state.nodes[i] = node;
547
- state.contexts[i] = context;
548
- } else {
549
- state.nodes[i] = previous;
550
- state.contexts[i] = context = previous_context;
551
- }
552
- }
553
-
554
- const new_props = await Promise.all(props_promises);
555
-
556
- new_props.forEach((p, i) => {
557
- if (p) {
558
- props[`props_${i}`] = p;
559
- }
560
- });
561
-
562
- if (!this.current.page || page.path !== this.current.page.path) {
563
- props.page = page;
564
- }
565
- } catch (error) {
566
- props.error = error;
567
- props.status = error.status || 500;
568
- state.nodes = [];
569
- }
570
-
571
- return { redirect, props, state };
572
- }
573
-
574
- async prefetch(url) {
575
- const page = this.router.select(url);
576
-
577
- if (page) {
578
- if (url.href !== this.prefetching.href) {
579
- this.prefetching = { href: url.href, promise: this.hydrate(page) };
580
- }
581
-
582
- return this.prefetching.promise;
583
- } else {
584
- throw new Error(`Could not prefetch ${url.href}`);
585
- }
586
- }
587
- }
588
-
589
- async function start({ paths, target, host, session, preloaded, error, status }) {
590
- const router = new Router({
591
- base: paths.base,
592
- host,
593
- pages,
594
- ignore
595
- });
596
-
597
- const renderer = new Renderer({
598
- Root,
599
- layout,
600
- target,
601
- preloaded,
602
- error,
603
- status,
604
- session
605
- });
606
-
607
- init({ router, renderer });
608
- set_paths(paths);
609
-
610
- await router.init({ renderer });
611
- }
612
-
613
- export { start };
614
- //# sourceMappingURL=start.js.map