@sveltejs/kit 1.0.0-next.43 → 1.0.0-next.430
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.
- package/README.md +12 -9
- package/package.json +95 -63
- package/src/cli.js +112 -0
- package/src/core/adapt/builder.js +207 -0
- package/src/core/adapt/index.js +19 -0
- package/src/core/config/index.js +86 -0
- package/src/core/config/options.js +488 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/constants.js +5 -0
- package/src/core/env.js +97 -0
- package/src/core/generate_manifest/index.js +99 -0
- package/src/core/prerender/crawl.js +194 -0
- package/src/core/prerender/prerender.js +378 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +506 -0
- package/src/core/sync/create_manifest_data/types.d.ts +40 -0
- package/src/core/sync/sync.js +59 -0
- package/src/core/sync/utils.js +44 -0
- package/src/core/sync/write_ambient.js +27 -0
- package/src/core/sync/write_client_manifest.js +82 -0
- package/src/core/sync/write_matchers.js +25 -0
- package/src/core/sync/write_root.js +91 -0
- package/src/core/sync/write_tsconfig.js +195 -0
- package/src/core/sync/write_types.js +775 -0
- package/src/core/utils.js +70 -0
- package/src/hooks.js +26 -0
- package/src/index/index.js +45 -0
- package/src/index/private.js +33 -0
- package/src/node/index.js +145 -0
- package/src/node/polyfills.js +40 -0
- package/src/runtime/app/env.js +11 -0
- package/src/runtime/app/navigation.js +22 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +102 -0
- package/src/runtime/client/ambient.d.ts +17 -0
- package/src/runtime/client/client.js +1289 -0
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +36 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +46 -0
- package/src/runtime/client/types.d.ts +105 -0
- package/src/runtime/client/utils.js +113 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/env/dynamic/private.js +1 -0
- package/src/runtime/env/dynamic/public.js +1 -0
- package/src/runtime/env-private.js +7 -0
- package/src/runtime/env-public.js +7 -0
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +16 -0
- package/src/runtime/paths.js +11 -0
- package/src/runtime/server/endpoint.js +58 -0
- package/src/runtime/server/index.js +448 -0
- package/src/runtime/server/page/cookie.js +25 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +249 -0
- package/src/runtime/server/page/fetch.js +266 -0
- package/src/runtime/server/page/index.js +416 -0
- package/src/runtime/server/page/load_data.js +135 -0
- package/src/runtime/server/page/render.js +362 -0
- package/src/runtime/server/page/respond_with_error.js +94 -0
- package/src/runtime/server/page/types.d.ts +44 -0
- package/src/runtime/server/utils.js +116 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +104 -0
- package/src/utils/filesystem.js +108 -0
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +108 -0
- package/src/utils/url.js +97 -0
- package/src/vite/build/build_server.js +337 -0
- package/src/vite/build/build_service_worker.js +90 -0
- package/src/vite/build/utils.js +160 -0
- package/src/vite/dev/index.js +551 -0
- package/src/vite/index.js +574 -0
- package/src/vite/preview/index.js +186 -0
- package/src/vite/types.d.ts +3 -0
- package/src/vite/utils.js +345 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +357 -0
- package/types/index.d.ts +343 -0
- package/types/internal.d.ts +308 -0
- package/types/private.d.ts +209 -0
- package/CHANGELOG.md +0 -431
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/env.js +0 -5
- package/assets/runtime/app/navigation.js +0 -41
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -93
- package/assets/runtime/chunks/utils.js +0 -19
- package/assets/runtime/internal/singletons.js +0 -23
- package/assets/runtime/internal/start.js +0 -770
- package/assets/runtime/paths.js +0 -12
- package/dist/.DS_Store +0 -0
- package/dist/chunks/index.js +0 -3521
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -538
- package/dist/chunks/index5.js +0 -761
- package/dist/chunks/index6.js +0 -322
- package/dist/chunks/standard.js +0 -99
- package/dist/chunks/utils.js +0 -83
- package/dist/cli.js +0 -546
- package/dist/ssr.js +0 -2581
|
@@ -1,770 +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 '../chunks/utils.js';
|
|
4
|
-
import { writable } from 'svelte/store';
|
|
5
|
-
import { init } from './singletons.js';
|
|
6
|
-
import { set_paths } from '../paths.js';
|
|
7
|
-
|
|
8
|
-
/** @param {MouseEvent} event */
|
|
9
|
-
function which(event) {
|
|
10
|
-
return event.which === null ? event.button : event.which;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function scroll_state() {
|
|
14
|
-
return {
|
|
15
|
-
x: pageXOffset,
|
|
16
|
-
y: pageYOffset
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
class Router {
|
|
21
|
-
/** @param {{
|
|
22
|
-
* base: string;
|
|
23
|
-
* host: string;
|
|
24
|
-
* pages: import('../../types').Page[];
|
|
25
|
-
* ignore: RegExp[];
|
|
26
|
-
* }} opts */
|
|
27
|
-
constructor({ base, host, pages, ignore }) {
|
|
28
|
-
this.base = base;
|
|
29
|
-
this.host = host;
|
|
30
|
-
this.pages = pages;
|
|
31
|
-
this.ignore = ignore;
|
|
32
|
-
|
|
33
|
-
this.history = window.history || {
|
|
34
|
-
pushState: () => {},
|
|
35
|
-
replaceState: () => {},
|
|
36
|
-
scrollRestoration: 'auto'
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** @param {import('./renderer').Renderer} renderer */
|
|
41
|
-
init(renderer) {
|
|
42
|
-
/** @type {import('./renderer').Renderer} */
|
|
43
|
-
this.renderer = renderer;
|
|
44
|
-
renderer.router = this;
|
|
45
|
-
|
|
46
|
-
if ('scrollRestoration' in this.history) {
|
|
47
|
-
this.history.scrollRestoration = 'manual';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Adopted from Nuxt.js
|
|
51
|
-
// Reset scrollRestoration to auto when leaving page, allowing page reload
|
|
52
|
-
// and back-navigation from other pages to use the browser to restore the
|
|
53
|
-
// scrolling position.
|
|
54
|
-
addEventListener('beforeunload', () => {
|
|
55
|
-
this.history.scrollRestoration = 'auto';
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// Setting scrollRestoration to manual again when returning to this page.
|
|
59
|
-
addEventListener('load', () => {
|
|
60
|
-
this.history.scrollRestoration = 'manual';
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// There's no API to capture the scroll location right before the user
|
|
64
|
-
// hits the back/forward button, so we listen for scroll events
|
|
65
|
-
|
|
66
|
-
/** @type {NodeJS.Timeout} */
|
|
67
|
-
let scroll_timer;
|
|
68
|
-
addEventListener('scroll', () => {
|
|
69
|
-
clearTimeout(scroll_timer);
|
|
70
|
-
scroll_timer = setTimeout(() => {
|
|
71
|
-
// Store the scroll location in the history
|
|
72
|
-
// This will persist even if we navigate away from the site and come back
|
|
73
|
-
const new_state = {
|
|
74
|
-
...(history.state || {}),
|
|
75
|
-
'sveltekit:scroll': scroll_state()
|
|
76
|
-
};
|
|
77
|
-
history.replaceState(new_state, document.title, window.location.href);
|
|
78
|
-
}, 50);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
/** @param {MouseEvent} event */
|
|
82
|
-
addEventListener('click', (event) => {
|
|
83
|
-
// Adapted from https://github.com/visionmedia/page.js
|
|
84
|
-
// MIT license https://github.com/visionmedia/page.js#license
|
|
85
|
-
if (which(event) !== 1) return;
|
|
86
|
-
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
87
|
-
if (event.defaultPrevented) return;
|
|
88
|
-
|
|
89
|
-
/** @type {HTMLAnchorElement | SVGAElement} */
|
|
90
|
-
const a = find_anchor(event.target);
|
|
91
|
-
if (!a) return;
|
|
92
|
-
|
|
93
|
-
if (!a.href) return;
|
|
94
|
-
|
|
95
|
-
// check if link is inside an svg
|
|
96
|
-
// in this case, both href and target are always inside an object
|
|
97
|
-
const svg = typeof a.href === 'object' && a.href.constructor.name === 'SVGAnimatedString';
|
|
98
|
-
const href = String(svg ? a.href.baseVal : a.href);
|
|
99
|
-
|
|
100
|
-
if (href === location.href) {
|
|
101
|
-
if (!location.hash) event.preventDefault();
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Ignore if tag has
|
|
106
|
-
// 1. 'download' attribute
|
|
107
|
-
// 2. rel='external' attribute
|
|
108
|
-
if (a.hasAttribute('download') || a.getAttribute('rel') === 'external') return;
|
|
109
|
-
|
|
110
|
-
// Ignore if <a> has a target
|
|
111
|
-
if (svg ? a.target.baseVal : a.target) return;
|
|
112
|
-
|
|
113
|
-
const url = new URL(href);
|
|
114
|
-
|
|
115
|
-
// Don't handle hash changes
|
|
116
|
-
if (url.pathname === location.pathname && url.search === location.search) return;
|
|
117
|
-
|
|
118
|
-
const selected = this.select(url);
|
|
119
|
-
if (selected) {
|
|
120
|
-
const noscroll = a.hasAttribute('sveltekit:noscroll');
|
|
121
|
-
this.renderer.notify(selected);
|
|
122
|
-
this.history.pushState({}, '', url.href);
|
|
123
|
-
this.navigate(selected, noscroll ? scroll_state() : null, [], url.hash);
|
|
124
|
-
event.preventDefault();
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
addEventListener('popstate', (event) => {
|
|
129
|
-
if (event.state) {
|
|
130
|
-
const url = new URL(location.href);
|
|
131
|
-
const selected = this.select(url);
|
|
132
|
-
if (selected) {
|
|
133
|
-
this.navigate(selected, event.state['sveltekit:scroll'], []);
|
|
134
|
-
} else {
|
|
135
|
-
// eslint-disable-next-line
|
|
136
|
-
location.href = location.href; // nosonar
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
// make it possible to reset focus
|
|
142
|
-
document.body.setAttribute('tabindex', '-1');
|
|
143
|
-
|
|
144
|
-
// load current page
|
|
145
|
-
this.history.replaceState({}, '', location.href);
|
|
146
|
-
|
|
147
|
-
const selected = this.select(new URL(location.href));
|
|
148
|
-
if (selected) return this.renderer.start(selected);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @param {URL} url
|
|
153
|
-
* @returns {import('./types').NavigationTarget}
|
|
154
|
-
*/
|
|
155
|
-
select(url) {
|
|
156
|
-
if (url.origin !== location.origin) return null;
|
|
157
|
-
if (!url.pathname.startsWith(this.base)) return null;
|
|
158
|
-
|
|
159
|
-
let path = url.pathname.slice(this.base.length);
|
|
160
|
-
|
|
161
|
-
if (path === '') {
|
|
162
|
-
path = '/';
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// avoid accidental clashes between server routes and page routes
|
|
166
|
-
if (this.ignore.some((pattern) => pattern.test(path))) return;
|
|
167
|
-
|
|
168
|
-
for (const route of this.pages) {
|
|
169
|
-
const match = route.pattern.exec(path);
|
|
170
|
-
|
|
171
|
-
if (match) {
|
|
172
|
-
const query = new URLSearchParams(url.search);
|
|
173
|
-
const params = route.params(match);
|
|
174
|
-
|
|
175
|
-
const page = { host: this.host, path, query, params };
|
|
176
|
-
|
|
177
|
-
return { href: url.href, route, match, page };
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* @param {string} href
|
|
184
|
-
* @param {{ noscroll?: boolean, replaceState?: boolean }} opts
|
|
185
|
-
* @param {string[]} chain
|
|
186
|
-
*/
|
|
187
|
-
async goto(href, { noscroll = false, replaceState = false } = {}, chain) {
|
|
188
|
-
const url = new URL(href, get_base_uri(document));
|
|
189
|
-
const selected = this.select(url);
|
|
190
|
-
|
|
191
|
-
if (selected) {
|
|
192
|
-
this.renderer.notify(selected);
|
|
193
|
-
|
|
194
|
-
// TODO shouldn't need to pass the hash here
|
|
195
|
-
this.history[replaceState ? 'replaceState' : 'pushState']({}, '', href);
|
|
196
|
-
return this.navigate(selected, noscroll ? scroll_state() : null, chain, url.hash);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
location.href = href;
|
|
200
|
-
return new Promise(() => {
|
|
201
|
-
/* never resolves */
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* @param {*} selected
|
|
207
|
-
* @param {{ x: number, y: number }} scroll
|
|
208
|
-
* @param {string[]} chain
|
|
209
|
-
* @param {string} [hash]
|
|
210
|
-
*/
|
|
211
|
-
async navigate(selected, scroll, chain, hash) {
|
|
212
|
-
// remove trailing slashes
|
|
213
|
-
if (location.pathname.endsWith('/') && location.pathname !== '/') {
|
|
214
|
-
history.replaceState({}, '', `${location.pathname.slice(0, -1)}${location.search}`);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
await this.renderer.render(selected, chain);
|
|
218
|
-
|
|
219
|
-
document.body.focus();
|
|
220
|
-
|
|
221
|
-
const deep_linked = hash && document.getElementById(hash.slice(1));
|
|
222
|
-
if (scroll) {
|
|
223
|
-
scrollTo(scroll.x, scroll.y);
|
|
224
|
-
} else if (deep_linked) {
|
|
225
|
-
// scroll is an element id (from a hash), we need to compute y
|
|
226
|
-
scrollTo(0, deep_linked.getBoundingClientRect().top + scrollY);
|
|
227
|
-
} else {
|
|
228
|
-
scrollTo(0, 0);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* @param {import('../types').LoadResult} loaded
|
|
235
|
-
* @returns {import('../types').LoadResult}
|
|
236
|
-
*/
|
|
237
|
-
function normalize(loaded) {
|
|
238
|
-
// TODO should this behaviour be dev-only?
|
|
239
|
-
|
|
240
|
-
if (loaded.error) {
|
|
241
|
-
const error = typeof loaded.error === 'string' ? new Error(loaded.error) : loaded.error;
|
|
242
|
-
const status = loaded.status;
|
|
243
|
-
|
|
244
|
-
if (!(error instanceof Error)) {
|
|
245
|
-
return {
|
|
246
|
-
status: 500,
|
|
247
|
-
error: new Error(
|
|
248
|
-
`"error" property returned from load() must be a string or instance of Error, received type "${typeof error}"`
|
|
249
|
-
)
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (!status || status < 400 || status > 599) {
|
|
254
|
-
console.warn('"error" returned from load() without a valid status code — defaulting to 500');
|
|
255
|
-
return { status: 500, error };
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return { status, error };
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (loaded.redirect) {
|
|
262
|
-
if (!loaded.status || Math.floor(loaded.status / 100) !== 3) {
|
|
263
|
-
return {
|
|
264
|
-
status: 500,
|
|
265
|
-
error: new Error(
|
|
266
|
-
'"redirect" property returned from load() must be accompanied by a 3xx status code'
|
|
267
|
-
)
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (typeof loaded.redirect !== 'string') {
|
|
272
|
-
return {
|
|
273
|
-
status: 500,
|
|
274
|
-
error: new Error('"redirect" property returned from load() must be a string')
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return loaded;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/** @param {any} value */
|
|
283
|
-
function page_store(value) {
|
|
284
|
-
const store = writable(value);
|
|
285
|
-
let ready = true;
|
|
286
|
-
|
|
287
|
-
function notify() {
|
|
288
|
-
ready = true;
|
|
289
|
-
store.update((val) => val);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/** @param {any} new_value */
|
|
293
|
-
function set(new_value) {
|
|
294
|
-
ready = false;
|
|
295
|
-
store.set(new_value);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/** @param {(value: any) => void} run */
|
|
299
|
-
function subscribe(run) {
|
|
300
|
-
/** @type {any} */
|
|
301
|
-
let old_value;
|
|
302
|
-
return store.subscribe((new_value) => {
|
|
303
|
-
if (old_value === undefined || (ready && new_value !== old_value)) {
|
|
304
|
-
run((old_value = new_value));
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return { notify, set, subscribe };
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
class Renderer {
|
|
313
|
-
/** @param {{
|
|
314
|
-
* Root: import('../../types').CSRComponent;
|
|
315
|
-
* layout: import('../../types').CSRComponent;
|
|
316
|
-
* target: Node;
|
|
317
|
-
* error: Error;
|
|
318
|
-
* status: number;
|
|
319
|
-
* session: any;
|
|
320
|
-
* }} opts */
|
|
321
|
-
constructor({ Root, layout, target, error, status, session }) {
|
|
322
|
-
this.Root = Root;
|
|
323
|
-
this.layout = layout;
|
|
324
|
-
this.layout_loader = () => layout;
|
|
325
|
-
|
|
326
|
-
/** @type {import('./router').Router} */
|
|
327
|
-
this.router = null;
|
|
328
|
-
|
|
329
|
-
// TODO ideally we wouldn't need to store these...
|
|
330
|
-
this.target = target;
|
|
331
|
-
|
|
332
|
-
this.initial = {
|
|
333
|
-
error,
|
|
334
|
-
status
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
this.current = {
|
|
338
|
-
page: null,
|
|
339
|
-
query: null,
|
|
340
|
-
session_changed: false,
|
|
341
|
-
nodes: [],
|
|
342
|
-
contexts: []
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
this.caches = new Map();
|
|
346
|
-
|
|
347
|
-
this.prefetching = {
|
|
348
|
-
href: null,
|
|
349
|
-
promise: null
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
this.stores = {
|
|
353
|
-
page: page_store({}),
|
|
354
|
-
navigating: writable(null),
|
|
355
|
-
session: writable(session)
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
this.$session = null;
|
|
359
|
-
|
|
360
|
-
this.root = null;
|
|
361
|
-
|
|
362
|
-
/** @param {MouseEvent} event */
|
|
363
|
-
const trigger_prefetch = (event) => {
|
|
364
|
-
/** @type {HTMLAnchorElement | SVGAElement} */
|
|
365
|
-
const a = find_anchor(event.target);
|
|
366
|
-
|
|
367
|
-
if (a && a.hasAttribute('sveltekit:prefetch')) {
|
|
368
|
-
this.prefetch(new URL(a.href));
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
/** @type {NodeJS.Timeout} */
|
|
373
|
-
let mousemove_timeout;
|
|
374
|
-
|
|
375
|
-
/** @param {MouseEvent} event */
|
|
376
|
-
const handle_mousemove = (event) => {
|
|
377
|
-
clearTimeout(mousemove_timeout);
|
|
378
|
-
mousemove_timeout = setTimeout(() => {
|
|
379
|
-
trigger_prefetch(event);
|
|
380
|
-
}, 20);
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
addEventListener('touchstart', trigger_prefetch);
|
|
384
|
-
addEventListener('mousemove', handle_mousemove);
|
|
385
|
-
|
|
386
|
-
let ready = false;
|
|
387
|
-
this.stores.session.subscribe(async (value) => {
|
|
388
|
-
this.$session = value;
|
|
389
|
-
|
|
390
|
-
if (!ready) return;
|
|
391
|
-
this.current.session_changed = true;
|
|
392
|
-
|
|
393
|
-
const selected = this.router.select(new URL(location.href));
|
|
394
|
-
this.render(selected);
|
|
395
|
-
});
|
|
396
|
-
ready = true;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/** @param {import('./types').NavigationTarget} selected */
|
|
400
|
-
async start(selected) {
|
|
401
|
-
/** @type {Record<string, any>} */
|
|
402
|
-
const props = {
|
|
403
|
-
stores: this.stores,
|
|
404
|
-
error: this.initial.error,
|
|
405
|
-
status: this.initial.status,
|
|
406
|
-
page: selected.page
|
|
407
|
-
};
|
|
408
|
-
|
|
409
|
-
if (this.initial.error) {
|
|
410
|
-
props.components = [this.layout.default];
|
|
411
|
-
} else {
|
|
412
|
-
const hydrated = await this.hydrate(selected);
|
|
413
|
-
|
|
414
|
-
if (hydrated.redirect) {
|
|
415
|
-
throw new Error('TODO client-side redirects');
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
Object.assign(props, hydrated.props);
|
|
419
|
-
this.current = hydrated.state;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
// remove dev-mode SSR <style> insert, since it doesn't apply
|
|
423
|
-
// to hydrated markup (HMR requires hashes to be rewritten)
|
|
424
|
-
// TODO only in dev
|
|
425
|
-
// TODO it seems this doesn't always work with the classname
|
|
426
|
-
// stabilisation in vite-plugin-svelte? see e.g.
|
|
427
|
-
// hn.svelte.dev
|
|
428
|
-
// const style = document.querySelector('style[data-svelte]');
|
|
429
|
-
// if (style) style.remove();
|
|
430
|
-
|
|
431
|
-
this.root = new this.Root({
|
|
432
|
-
target: this.target,
|
|
433
|
-
props,
|
|
434
|
-
hydrate: true
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
this.initial = null;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
/** @param {import('./types').NavigationTarget} selected */
|
|
441
|
-
notify(selected) {
|
|
442
|
-
this.stores.navigating.set({
|
|
443
|
-
from: this.current.page,
|
|
444
|
-
to: selected.page
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
/**
|
|
449
|
-
* @param {import('./types').NavigationTarget} selected
|
|
450
|
-
* @param {string[]} chain
|
|
451
|
-
*/
|
|
452
|
-
async render(selected, chain) {
|
|
453
|
-
const token = (this.token = {});
|
|
454
|
-
|
|
455
|
-
const hydrated = await this.hydrate(selected);
|
|
456
|
-
|
|
457
|
-
if (this.token === token) {
|
|
458
|
-
if (hydrated.redirect) {
|
|
459
|
-
if (chain.length > 10 || chain.includes(this.current.page.path)) {
|
|
460
|
-
hydrated.props.status = 500;
|
|
461
|
-
hydrated.props.error = new Error('Redirect loop');
|
|
462
|
-
} else {
|
|
463
|
-
this.router.goto(hydrated.redirect, { replaceState: true }, [
|
|
464
|
-
...chain,
|
|
465
|
-
this.current.page.path
|
|
466
|
-
]);
|
|
467
|
-
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
// check render wasn't aborted
|
|
473
|
-
this.current = hydrated.state;
|
|
474
|
-
|
|
475
|
-
this.root.$set(hydrated.props);
|
|
476
|
-
this.stores.navigating.set(null);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/** @param {import('./types').NavigationTarget} selected */
|
|
481
|
-
async hydrate({ route, page }) {
|
|
482
|
-
/** @type {Record<string, any>} */
|
|
483
|
-
const props = {
|
|
484
|
-
status: 200,
|
|
485
|
-
|
|
486
|
-
/** @type {Error} */
|
|
487
|
-
error: null,
|
|
488
|
-
|
|
489
|
-
/** @type {import('../../types').CSRComponent[]} */
|
|
490
|
-
components: []
|
|
491
|
-
};
|
|
492
|
-
|
|
493
|
-
const fetcher = (url, opts) => {
|
|
494
|
-
if (this.initial) {
|
|
495
|
-
const script = document.querySelector(`script[type="svelte-data"][url="${url}"]`);
|
|
496
|
-
if (script) {
|
|
497
|
-
const { body, ...init } = JSON.parse(script.textContent);
|
|
498
|
-
return Promise.resolve(new Response(body, init));
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
return fetch(url, opts);
|
|
503
|
-
};
|
|
504
|
-
|
|
505
|
-
const query = page.query.toString();
|
|
506
|
-
|
|
507
|
-
// TODO come up with a better name
|
|
508
|
-
/** @typedef {{
|
|
509
|
-
* component: import('../../types').CSRComponent;
|
|
510
|
-
* uses: {
|
|
511
|
-
* params: Set<string>;
|
|
512
|
-
* query: boolean;
|
|
513
|
-
* session: boolean;
|
|
514
|
-
* context: boolean;
|
|
515
|
-
* }
|
|
516
|
-
* }} Branch */
|
|
517
|
-
|
|
518
|
-
const state = {
|
|
519
|
-
page,
|
|
520
|
-
query,
|
|
521
|
-
session_changed: false,
|
|
522
|
-
/** @type {Branch[]} */
|
|
523
|
-
nodes: [],
|
|
524
|
-
/** @type {Record<string, any>[]} */
|
|
525
|
-
contexts: []
|
|
526
|
-
};
|
|
527
|
-
|
|
528
|
-
const component_promises = [this.layout_loader(), ...route.parts.map((loader) => loader())];
|
|
529
|
-
const props_promises = [];
|
|
530
|
-
|
|
531
|
-
/** @type {Record<string, any>} */
|
|
532
|
-
let context;
|
|
533
|
-
let redirect;
|
|
534
|
-
|
|
535
|
-
const changed = {
|
|
536
|
-
params: Object.keys(page.params).filter((key) => {
|
|
537
|
-
return !this.current.page || this.current.page.params[key] !== page.params[key];
|
|
538
|
-
}),
|
|
539
|
-
query: query !== this.current.query,
|
|
540
|
-
session: this.current.session_changed,
|
|
541
|
-
context: false
|
|
542
|
-
};
|
|
543
|
-
|
|
544
|
-
try {
|
|
545
|
-
for (let i = 0; i < component_promises.length; i += 1) {
|
|
546
|
-
const previous = this.current.nodes[i];
|
|
547
|
-
const previous_context = this.current.contexts[i];
|
|
548
|
-
|
|
549
|
-
const { default: component, preload, load } = await component_promises[i];
|
|
550
|
-
props.components[i] = component;
|
|
551
|
-
|
|
552
|
-
if (preload) {
|
|
553
|
-
throw new Error(
|
|
554
|
-
'preload has been deprecated in favour of load. Please consult the documentation: https://kit.svelte.dev/docs#load'
|
|
555
|
-
);
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
const changed_since_last_render =
|
|
559
|
-
!previous ||
|
|
560
|
-
component !== previous.component ||
|
|
561
|
-
changed.params.some((param) => previous.uses.params.has(param)) ||
|
|
562
|
-
(changed.query && previous.uses.query) ||
|
|
563
|
-
(changed.session && previous.uses.session) ||
|
|
564
|
-
(changed.context && previous.uses.context);
|
|
565
|
-
|
|
566
|
-
if (changed_since_last_render) {
|
|
567
|
-
const hash = page.path + query;
|
|
568
|
-
|
|
569
|
-
// see if we have some cached data
|
|
570
|
-
const cache = this.caches.get(component);
|
|
571
|
-
const cached = cache && cache.get(hash);
|
|
572
|
-
|
|
573
|
-
/** @type {Branch} */
|
|
574
|
-
let node;
|
|
575
|
-
|
|
576
|
-
/** @type {import('../../types').LoadResult} */
|
|
577
|
-
let loaded;
|
|
578
|
-
|
|
579
|
-
if (cached && (!changed.context || !cached.node.uses.context)) {
|
|
580
|
-
({ node, loaded } = cached);
|
|
581
|
-
} else {
|
|
582
|
-
node = {
|
|
583
|
-
component,
|
|
584
|
-
uses: {
|
|
585
|
-
params: new Set(),
|
|
586
|
-
query: false,
|
|
587
|
-
session: false,
|
|
588
|
-
context: false
|
|
589
|
-
}
|
|
590
|
-
};
|
|
591
|
-
|
|
592
|
-
const params = {};
|
|
593
|
-
for (const key in page.params) {
|
|
594
|
-
Object.defineProperty(params, key, {
|
|
595
|
-
get() {
|
|
596
|
-
node.uses.params.add(key);
|
|
597
|
-
return page.params[key];
|
|
598
|
-
},
|
|
599
|
-
enumerable: true
|
|
600
|
-
});
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
const session = this.$session;
|
|
604
|
-
|
|
605
|
-
loaded =
|
|
606
|
-
load &&
|
|
607
|
-
(await load.call(null, {
|
|
608
|
-
page: {
|
|
609
|
-
host: page.host,
|
|
610
|
-
path: page.path,
|
|
611
|
-
params,
|
|
612
|
-
get query() {
|
|
613
|
-
node.uses.query = true;
|
|
614
|
-
return page.query;
|
|
615
|
-
}
|
|
616
|
-
},
|
|
617
|
-
get session() {
|
|
618
|
-
node.uses.session = true;
|
|
619
|
-
return session;
|
|
620
|
-
},
|
|
621
|
-
get context() {
|
|
622
|
-
node.uses.context = true;
|
|
623
|
-
return { ...context };
|
|
624
|
-
},
|
|
625
|
-
fetch: fetcher
|
|
626
|
-
}));
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
if (loaded) {
|
|
630
|
-
loaded = normalize(loaded);
|
|
631
|
-
|
|
632
|
-
if (loaded.error) {
|
|
633
|
-
// TODO sticking the status on the error object is kinda hacky
|
|
634
|
-
loaded.error.status = loaded.status;
|
|
635
|
-
throw loaded.error;
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
if (loaded.redirect) {
|
|
639
|
-
// TODO return from here?
|
|
640
|
-
redirect = loaded.redirect;
|
|
641
|
-
break;
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
if (loaded.context) {
|
|
645
|
-
changed.context = true;
|
|
646
|
-
|
|
647
|
-
context = {
|
|
648
|
-
...context,
|
|
649
|
-
...loaded.context
|
|
650
|
-
};
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
if (loaded.maxage) {
|
|
654
|
-
if (!this.caches.has(component)) {
|
|
655
|
-
this.caches.set(component, new Map());
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
const cache = this.caches.get(component);
|
|
659
|
-
const cached = { node, loaded };
|
|
660
|
-
|
|
661
|
-
cache.set(hash, cached);
|
|
662
|
-
|
|
663
|
-
let ready = false;
|
|
664
|
-
|
|
665
|
-
const timeout = setTimeout(() => {
|
|
666
|
-
clear();
|
|
667
|
-
}, loaded.maxage * 1000);
|
|
668
|
-
|
|
669
|
-
const clear = () => {
|
|
670
|
-
if (cache.get(hash) === cached) {
|
|
671
|
-
cache.delete(hash);
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
unsubscribe();
|
|
675
|
-
clearTimeout(timeout);
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
const unsubscribe = this.stores.session.subscribe(() => {
|
|
679
|
-
if (ready) clear();
|
|
680
|
-
});
|
|
681
|
-
|
|
682
|
-
ready = true;
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
props_promises[i] = loaded.props;
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
state.nodes[i] = node;
|
|
689
|
-
state.contexts[i] = context;
|
|
690
|
-
} else {
|
|
691
|
-
state.nodes[i] = previous;
|
|
692
|
-
state.contexts[i] = context = previous_context;
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
const new_props = await Promise.all(props_promises);
|
|
697
|
-
|
|
698
|
-
new_props.forEach((p, i) => {
|
|
699
|
-
if (p) {
|
|
700
|
-
props[`props_${i}`] = p;
|
|
701
|
-
}
|
|
702
|
-
});
|
|
703
|
-
|
|
704
|
-
if (!this.current.page || page.path !== this.current.page.path) {
|
|
705
|
-
props.page = page;
|
|
706
|
-
}
|
|
707
|
-
} catch (error) {
|
|
708
|
-
props.error = error;
|
|
709
|
-
props.status = error.status || 500;
|
|
710
|
-
state.nodes = [];
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
return { redirect, props, state };
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
/** @param {URL} url */
|
|
717
|
-
async prefetch(url) {
|
|
718
|
-
const page = this.router.select(url);
|
|
719
|
-
|
|
720
|
-
if (page) {
|
|
721
|
-
if (url.href !== this.prefetching.href) {
|
|
722
|
-
this.prefetching = { href: url.href, promise: this.hydrate(page) };
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
return this.prefetching.promise;
|
|
726
|
-
} else {
|
|
727
|
-
throw new Error(`Could not prefetch ${url.href}`);
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
/** @param {{
|
|
733
|
-
* paths: {
|
|
734
|
-
* assets: string;
|
|
735
|
-
* base: string;
|
|
736
|
-
* },
|
|
737
|
-
* target: Node;
|
|
738
|
-
* host: string;
|
|
739
|
-
* session: any;
|
|
740
|
-
* error: Error;
|
|
741
|
-
* status: number;
|
|
742
|
-
* }} opts */
|
|
743
|
-
async function start({ paths, target, host, session, error, status }) {
|
|
744
|
-
const router = new Router({
|
|
745
|
-
base: paths.base,
|
|
746
|
-
host,
|
|
747
|
-
pages,
|
|
748
|
-
ignore
|
|
749
|
-
});
|
|
750
|
-
|
|
751
|
-
const renderer = new Renderer({
|
|
752
|
-
Root,
|
|
753
|
-
layout,
|
|
754
|
-
target,
|
|
755
|
-
error,
|
|
756
|
-
status,
|
|
757
|
-
session
|
|
758
|
-
});
|
|
759
|
-
|
|
760
|
-
init({ router, renderer });
|
|
761
|
-
set_paths(paths);
|
|
762
|
-
|
|
763
|
-
await router.init(renderer);
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
if (import.meta.env.VITE_SVELTEKIT_SERVICE_WORKER) {
|
|
767
|
-
navigator.serviceWorker.register(import.meta.env.VITE_SVELTEKIT_SERVICE_WORKER);
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
export { start };
|