@sveltejs/kit 1.0.0-next.31 → 1.0.0-next.310

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 (74) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +20 -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 +1655 -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 +2862 -0
  12. package/dist/chunks/amp_hook.js +56 -0
  13. package/dist/chunks/cert.js +28154 -0
  14. package/dist/chunks/constants.js +663 -0
  15. package/dist/chunks/filesystem.js +110 -0
  16. package/dist/chunks/index.js +515 -0
  17. package/dist/chunks/index2.js +1326 -0
  18. package/dist/chunks/index3.js +118 -0
  19. package/dist/chunks/index4.js +185 -0
  20. package/dist/chunks/index5.js +251 -0
  21. package/dist/chunks/index6.js +15585 -0
  22. package/dist/chunks/index7.js +4207 -0
  23. package/dist/chunks/misc.js +78 -0
  24. package/dist/chunks/multipart-parser.js +449 -0
  25. package/dist/chunks/object.js +83 -0
  26. package/dist/chunks/sync.js +983 -0
  27. package/dist/chunks/url.js +56 -0
  28. package/dist/cli.js +1023 -91
  29. package/dist/hooks.js +28 -0
  30. package/dist/install-fetch.js +6518 -0
  31. package/dist/node.js +94 -0
  32. package/package.json +92 -54
  33. package/svelte-kit.js +2 -0
  34. package/types/ambient.d.ts +298 -0
  35. package/types/index.d.ts +258 -0
  36. package/types/internal.d.ts +314 -0
  37. package/types/private.d.ts +269 -0
  38. package/CHANGELOG.md +0 -344
  39. package/assets/runtime/app/navigation.js +0 -23
  40. package/assets/runtime/app/navigation.js.map +0 -1
  41. package/assets/runtime/app/paths.js +0 -2
  42. package/assets/runtime/app/paths.js.map +0 -1
  43. package/assets/runtime/app/stores.js +0 -78
  44. package/assets/runtime/app/stores.js.map +0 -1
  45. package/assets/runtime/internal/singletons.js +0 -15
  46. package/assets/runtime/internal/singletons.js.map +0 -1
  47. package/assets/runtime/internal/start.js +0 -591
  48. package/assets/runtime/internal/start.js.map +0 -1
  49. package/assets/runtime/utils-85ebcc60.js +0 -18
  50. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  51. package/dist/api.js +0 -44
  52. package/dist/api.js.map +0 -1
  53. package/dist/cli.js.map +0 -1
  54. package/dist/create_app.js +0 -580
  55. package/dist/create_app.js.map +0 -1
  56. package/dist/index.js +0 -375
  57. package/dist/index.js.map +0 -1
  58. package/dist/index2.js +0 -12205
  59. package/dist/index2.js.map +0 -1
  60. package/dist/index3.js +0 -549
  61. package/dist/index3.js.map +0 -1
  62. package/dist/index4.js +0 -74
  63. package/dist/index4.js.map +0 -1
  64. package/dist/index5.js +0 -468
  65. package/dist/index5.js.map +0 -1
  66. package/dist/index6.js +0 -735
  67. package/dist/index6.js.map +0 -1
  68. package/dist/renderer.js +0 -2425
  69. package/dist/renderer.js.map +0 -1
  70. package/dist/standard.js +0 -103
  71. package/dist/standard.js.map +0 -1
  72. package/dist/utils.js +0 -58
  73. package/dist/utils.js.map +0 -1
  74. package/svelte-kit +0 -3
@@ -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,591 +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
- };
251
-
252
- this.caches = new Map();
253
-
254
- this.prefetching = {
255
- href: null,
256
- promise: null
257
- };
258
-
259
- this.stores = {
260
- page: page_store({}),
261
- navigating: writable(null),
262
- session: writable(session)
263
- };
264
-
265
- this.$session = null;
266
-
267
- this.root = null;
268
-
269
- const trigger_prefetch = (event) => {
270
- const a = find_anchor(event.target);
271
-
272
- if (a && a.hasAttribute('sveltekit:prefetch')) {
273
- this.prefetch(new URL(a.href));
274
- }
275
- };
276
-
277
- let mousemove_timeout;
278
- const handle_mousemove = (event) => {
279
- clearTimeout(mousemove_timeout);
280
- mousemove_timeout = setTimeout(() => {
281
- trigger_prefetch(event);
282
- }, 20);
283
- };
284
-
285
- addEventListener('touchstart', trigger_prefetch);
286
- addEventListener('mousemove', handle_mousemove);
287
-
288
- let ready = false;
289
- this.stores.session.subscribe(async (value) => {
290
- this.$session = value;
291
-
292
- if (!ready) return;
293
- this.current.session_changed = true;
294
-
295
- const selected = this.router.select(new URL(location.href));
296
- this.render(selected);
297
- });
298
- ready = true;
299
- }
300
-
301
- async start(selected) {
302
- const props = {
303
- stores: this.stores,
304
- error: this.initial.error,
305
- status: this.initial.status,
306
- page: selected.page
307
- };
308
-
309
- if (this.initial.error) {
310
- props.components = [this.layout.default];
311
- } else {
312
- const hydrated = await this.hydrate(selected);
313
-
314
- if (hydrated.redirect) {
315
- throw new Error('TODO client-side redirects');
316
- }
317
-
318
- Object.assign(props, hydrated.props);
319
- this.current = hydrated.state;
320
- }
321
-
322
- this.root = new this.Root({
323
- target: this.target,
324
- props,
325
- hydrate: true
326
- });
327
-
328
- this.initial = null;
329
- }
330
-
331
- notify(selected) {
332
- this.stores.navigating.set({
333
- from: this.current.page,
334
- to: selected.page
335
- });
336
- }
337
-
338
- async render(selected) {
339
- const token = (this.token = {});
340
-
341
- const hydrated = await this.hydrate(selected);
342
-
343
- if (this.token === token) {
344
- // check render wasn't aborted
345
- this.current = hydrated.state;
346
-
347
- this.root.$set(hydrated.props);
348
- this.stores.navigating.set(null);
349
- }
350
- }
351
-
352
- async hydrate({ route, page }) {
353
- const props = {
354
- error: null,
355
- status: 200,
356
- components: []
357
- };
358
-
359
- const fetcher = (url, opts) => {
360
- if (this.initial) {
361
- const script = document.querySelector(`script[type="svelte-data"][url="${url}"]`);
362
- if (script) {
363
- const { body, ...init } = JSON.parse(script.textContent);
364
- return Promise.resolve(new Response(body, init));
365
- }
366
- }
367
-
368
- return fetch(url, opts);
369
- };
370
-
371
- const query = page.query.toString();
372
-
373
- const state = {
374
- page,
375
- query,
376
- session_changed: false,
377
- nodes: []
378
- };
379
-
380
- const component_promises = [this.layout_loader(), ...route.parts.map((loader) => loader())];
381
- const props_promises = [];
382
-
383
- let context = {};
384
- let redirect;
385
-
386
- const changed = {
387
- params: Object.keys(page.params).filter((key) => {
388
- return !this.current.page || this.current.page.params[key] !== page.params[key];
389
- }),
390
- query: query !== this.current.query,
391
- session: this.current.session_changed,
392
- context: false
393
- };
394
-
395
- try {
396
- for (let i = 0; i < component_promises.length; i += 1) {
397
- const previous = this.current.nodes[i];
398
-
399
- const { default: component, load } = await component_promises[i];
400
- props.components[i] = component;
401
-
402
- const changed_since_last_render =
403
- !previous ||
404
- component !== previous.component ||
405
- changed.params.some((param) => previous.uses.params.has(param)) ||
406
- (changed.query && previous.uses.query) ||
407
- (changed.session && previous.uses.session) ||
408
- (changed.context && previous.uses.context);
409
-
410
- if (changed_since_last_render) {
411
- const hash = page.path + query;
412
-
413
- // see if we have some cached data
414
- const cache = this.caches.get(component);
415
- const cached = cache && cache.get(hash);
416
-
417
- let node;
418
- let loaded;
419
-
420
- if (cached && (!changed.context || !cached.node.uses.context)) {
421
- ({ node, loaded } = cached);
422
- } else {
423
- node = {
424
- component,
425
- uses: {
426
- params: new Set(),
427
- query: false,
428
- session: false,
429
- context: false
430
- }
431
- };
432
-
433
- const params = {};
434
- for (const key in page.params) {
435
- Object.defineProperty(params, key, {
436
- get() {
437
- node.uses.params.add(key);
438
- return page.params[key];
439
- },
440
- enumerable: true
441
- });
442
- }
443
-
444
- const session = this.$session;
445
-
446
- loaded =
447
- load &&
448
- (await load.call(null, {
449
- page: {
450
- ...page,
451
- params,
452
- get query() {
453
- node.uses.query = true;
454
- return page.query;
455
- }
456
- },
457
- get session() {
458
- node.uses.session = true;
459
- return session;
460
- },
461
- get context() {
462
- node.uses.context = true;
463
- return { ...context };
464
- },
465
- fetch: fetcher
466
- }));
467
- }
468
-
469
- if (loaded) {
470
- if (loaded.error) {
471
- const error = new Error(loaded.error.message);
472
- error.status = loaded.error.status;
473
- throw error;
474
- }
475
-
476
- if (loaded.redirect) {
477
- redirect = loaded.redirect;
478
- break;
479
- }
480
-
481
- if (loaded.context) {
482
- changed.context = true;
483
-
484
- context = {
485
- ...context,
486
- ...loaded.context
487
- };
488
- }
489
-
490
- if (loaded.maxage) {
491
- if (!this.caches.has(component)) {
492
- this.caches.set(component, new Map());
493
- }
494
-
495
- const cache = this.caches.get(component);
496
- const cached = { node, loaded };
497
-
498
- cache.set(hash, cached);
499
-
500
- let ready = false;
501
-
502
- const timeout = setTimeout(() => {
503
- clear();
504
- }, loaded.maxage * 1000);
505
-
506
- const clear = () => {
507
- if (cache.get(hash) === cached) {
508
- cache.delete(hash);
509
- }
510
-
511
- unsubscribe();
512
- clearTimeout(timeout);
513
- };
514
-
515
- const unsubscribe = this.stores.session.subscribe(() => {
516
- if (ready) clear();
517
- });
518
-
519
- ready = true;
520
- }
521
-
522
- props_promises[i] = loaded.props;
523
- }
524
-
525
- state.nodes[i] = node;
526
- } else {
527
- state.nodes[i] = previous;
528
- }
529
- }
530
-
531
- const new_props = await Promise.all(props_promises);
532
-
533
- new_props.forEach((p, i) => {
534
- if (p) {
535
- props[`props_${i}`] = p;
536
- }
537
- });
538
-
539
- if (!this.current.page || page.path !== this.current.page.path) {
540
- props.page = page;
541
- }
542
- } catch (error) {
543
- props.error = error;
544
- props.status = 500;
545
- state.nodes = [];
546
- }
547
-
548
- return { redirect, props, state };
549
- }
550
-
551
- async prefetch(url) {
552
- const page = this.router.select(url);
553
-
554
- if (page) {
555
- if (url.href !== this.prefetching.href) {
556
- this.prefetching = { href: url.href, promise: this.hydrate(page) };
557
- }
558
-
559
- return this.prefetching.promise;
560
- } else {
561
- throw new Error(`Could not prefetch ${url.href}`);
562
- }
563
- }
564
- }
565
-
566
- async function start({ paths, target, host, session, preloaded, error, status }) {
567
- const router = new Router({
568
- base: paths.base,
569
- host,
570
- pages,
571
- ignore
572
- });
573
-
574
- const renderer = new Renderer({
575
- Root,
576
- layout,
577
- target,
578
- preloaded,
579
- error,
580
- status,
581
- session
582
- });
583
-
584
- init({ router, renderer });
585
- set_paths(paths);
586
-
587
- await router.init({ renderer });
588
- }
589
-
590
- export { start };
591
- //# sourceMappingURL=start.js.map