@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
|
@@ -0,0 +1,1289 @@
|
|
|
1
|
+
import { onMount, tick } from 'svelte';
|
|
2
|
+
import { normalize_error } from '../../utils/error.js';
|
|
3
|
+
import { LoadURL, decode_params, normalize_path } from '../../utils/url.js';
|
|
4
|
+
import { find_anchor, get_base_uri, get_href, scroll_state } from './utils.js';
|
|
5
|
+
import { lock_fetch, unlock_fetch, initial_fetch, native_fetch } from './fetcher.js';
|
|
6
|
+
import { parse } from './parse.js';
|
|
7
|
+
import { error } from '../../index/index.js';
|
|
8
|
+
|
|
9
|
+
import Root from '__GENERATED__/root.svelte';
|
|
10
|
+
import { nodes, dictionary, matchers } from '__GENERATED__/client-manifest.js';
|
|
11
|
+
import { HttpError, Redirect } from '../../index/private.js';
|
|
12
|
+
import { stores } from './singletons.js';
|
|
13
|
+
|
|
14
|
+
const SCROLL_KEY = 'sveltekit:scroll';
|
|
15
|
+
const INDEX_KEY = 'sveltekit:index';
|
|
16
|
+
|
|
17
|
+
const routes = parse(nodes, dictionary, matchers);
|
|
18
|
+
|
|
19
|
+
// we import the root layout/error nodes eagerly, so that
|
|
20
|
+
// connectivity errors after initialisation don't nuke the app
|
|
21
|
+
const default_layout = nodes[0]();
|
|
22
|
+
const default_error = nodes[1]();
|
|
23
|
+
|
|
24
|
+
// We track the scroll position associated with each history entry in sessionStorage,
|
|
25
|
+
// rather than on history.state itself, because when navigation is driven by
|
|
26
|
+
// popstate it's too late to update the scroll position associated with the
|
|
27
|
+
// state we're navigating from
|
|
28
|
+
|
|
29
|
+
/** @typedef {{ x: number, y: number }} ScrollPosition */
|
|
30
|
+
/** @type {Record<number, ScrollPosition>} */
|
|
31
|
+
let scroll_positions = {};
|
|
32
|
+
try {
|
|
33
|
+
scroll_positions = JSON.parse(sessionStorage[SCROLL_KEY]);
|
|
34
|
+
} catch {
|
|
35
|
+
// do nothing
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @param {number} index */
|
|
39
|
+
function update_scroll_positions(index) {
|
|
40
|
+
scroll_positions[index] = scroll_state();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {{
|
|
45
|
+
* target: Element;
|
|
46
|
+
* base: string;
|
|
47
|
+
* trailing_slash: import('types').TrailingSlash;
|
|
48
|
+
* }} opts
|
|
49
|
+
* @returns {import('./types').Client}
|
|
50
|
+
*/
|
|
51
|
+
export function create_client({ target, base, trailing_slash }) {
|
|
52
|
+
/** @type {Array<((href: string) => boolean)>} */
|
|
53
|
+
const invalidated = [];
|
|
54
|
+
|
|
55
|
+
/** @type {{id: string | null, promise: Promise<import('./types').NavigationResult | undefined> | null}} */
|
|
56
|
+
const load_cache = {
|
|
57
|
+
id: null,
|
|
58
|
+
promise: null
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const callbacks = {
|
|
62
|
+
/** @type {Array<(opts: { from: URL, to: URL | null, cancel: () => void }) => void>} */
|
|
63
|
+
before_navigate: [],
|
|
64
|
+
|
|
65
|
+
/** @type {Array<(opts: { from: URL | null, to: URL }) => void>} */
|
|
66
|
+
after_navigate: []
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** @type {import('./types').NavigationState} */
|
|
70
|
+
let current = {
|
|
71
|
+
branch: [],
|
|
72
|
+
error: null,
|
|
73
|
+
session_id: 0,
|
|
74
|
+
// @ts-ignore - we need the initial value to be null
|
|
75
|
+
url: null
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
let started = false;
|
|
79
|
+
let autoscroll = true;
|
|
80
|
+
let updating = false;
|
|
81
|
+
let session_id = 1;
|
|
82
|
+
|
|
83
|
+
/** @type {Promise<void> | null} */
|
|
84
|
+
let invalidating = null;
|
|
85
|
+
|
|
86
|
+
/** @type {import('svelte').SvelteComponent} */
|
|
87
|
+
let root;
|
|
88
|
+
|
|
89
|
+
let router_enabled = true;
|
|
90
|
+
|
|
91
|
+
// keeping track of the history index in order to prevent popstate navigation events if needed
|
|
92
|
+
let current_history_index = history.state?.[INDEX_KEY];
|
|
93
|
+
|
|
94
|
+
if (!current_history_index) {
|
|
95
|
+
// we use Date.now() as an offset so that cross-document navigations
|
|
96
|
+
// within the app don't result in data loss
|
|
97
|
+
current_history_index = Date.now();
|
|
98
|
+
|
|
99
|
+
// create initial history entry, so we can return here
|
|
100
|
+
history.replaceState(
|
|
101
|
+
{ ...history.state, [INDEX_KEY]: current_history_index },
|
|
102
|
+
'',
|
|
103
|
+
location.href
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// if we reload the page, or Cmd-Shift-T back to it,
|
|
108
|
+
// recover scroll position
|
|
109
|
+
const scroll = scroll_positions[current_history_index];
|
|
110
|
+
if (scroll) {
|
|
111
|
+
history.scrollRestoration = 'manual';
|
|
112
|
+
scrollTo(scroll.x, scroll.y);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let hash_navigating = false;
|
|
116
|
+
|
|
117
|
+
/** @type {import('types').Page} */
|
|
118
|
+
let page;
|
|
119
|
+
|
|
120
|
+
/** @type {{}} */
|
|
121
|
+
let token;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param {string | URL} url
|
|
125
|
+
* @param {{ noscroll?: boolean; replaceState?: boolean; keepfocus?: boolean; state?: any }} opts
|
|
126
|
+
* @param {string[]} redirect_chain
|
|
127
|
+
*/
|
|
128
|
+
async function goto(
|
|
129
|
+
url,
|
|
130
|
+
{ noscroll = false, replaceState = false, keepfocus = false, state = {} },
|
|
131
|
+
redirect_chain
|
|
132
|
+
) {
|
|
133
|
+
if (typeof url === 'string') {
|
|
134
|
+
url = new URL(url, get_base_uri(document));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (router_enabled) {
|
|
138
|
+
return navigate({
|
|
139
|
+
url,
|
|
140
|
+
scroll: noscroll ? scroll_state() : null,
|
|
141
|
+
keepfocus,
|
|
142
|
+
redirect_chain,
|
|
143
|
+
details: {
|
|
144
|
+
state,
|
|
145
|
+
replaceState
|
|
146
|
+
},
|
|
147
|
+
accepted: () => {},
|
|
148
|
+
blocked: () => {}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
await native_navigation(url);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** @param {URL} url */
|
|
156
|
+
async function prefetch(url) {
|
|
157
|
+
const intent = get_navigation_intent(url);
|
|
158
|
+
|
|
159
|
+
if (!intent) {
|
|
160
|
+
throw new Error('Attempted to prefetch a URL that does not belong to this app');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
load_cache.promise = load_route(intent);
|
|
164
|
+
load_cache.id = intent.id;
|
|
165
|
+
|
|
166
|
+
return load_cache.promise;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Returns `true` if update completes, `false` if it is aborted
|
|
171
|
+
* @param {URL} url
|
|
172
|
+
* @param {string[]} redirect_chain
|
|
173
|
+
* @param {{hash?: string, scroll: { x: number, y: number } | null, keepfocus: boolean, details: { replaceState: boolean, state: any } | null}} [opts]
|
|
174
|
+
* @param {() => void} [callback]
|
|
175
|
+
*/
|
|
176
|
+
async function update(url, redirect_chain, opts, callback) {
|
|
177
|
+
const intent = get_navigation_intent(url);
|
|
178
|
+
|
|
179
|
+
const current_token = (token = {});
|
|
180
|
+
let navigation_result = intent && (await load_route(intent));
|
|
181
|
+
|
|
182
|
+
if (
|
|
183
|
+
!navigation_result &&
|
|
184
|
+
url.origin === location.origin &&
|
|
185
|
+
url.pathname === location.pathname
|
|
186
|
+
) {
|
|
187
|
+
// this could happen in SPA fallback mode if the user navigated to
|
|
188
|
+
// `/non-existent-page`. if we fall back to reloading the page, it
|
|
189
|
+
// will create an infinite loop. so whereas we normally handle
|
|
190
|
+
// unknown routes by going to the server, in this special case
|
|
191
|
+
// we render a client-side error page instead
|
|
192
|
+
navigation_result = await load_root_error_page({
|
|
193
|
+
status: 404,
|
|
194
|
+
error: new Error(`Not found: ${url.pathname}`),
|
|
195
|
+
url,
|
|
196
|
+
routeId: null
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (!navigation_result) {
|
|
201
|
+
await native_navigation(url);
|
|
202
|
+
return false; // unnecessary, but TypeScript prefers it this way
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// if this is an internal navigation intent, use the normalized
|
|
206
|
+
// URL for the rest of the function
|
|
207
|
+
url = intent?.url || url;
|
|
208
|
+
|
|
209
|
+
// abort if user navigated during update
|
|
210
|
+
if (token !== current_token) return false;
|
|
211
|
+
|
|
212
|
+
invalidated.length = 0;
|
|
213
|
+
|
|
214
|
+
if (navigation_result.type === 'redirect') {
|
|
215
|
+
if (redirect_chain.length > 10 || redirect_chain.includes(url.pathname)) {
|
|
216
|
+
navigation_result = await load_root_error_page({
|
|
217
|
+
status: 500,
|
|
218
|
+
error: new Error('Redirect loop'),
|
|
219
|
+
url,
|
|
220
|
+
routeId: null
|
|
221
|
+
});
|
|
222
|
+
} else {
|
|
223
|
+
if (router_enabled) {
|
|
224
|
+
goto(new URL(navigation_result.location, url).href, {}, [
|
|
225
|
+
...redirect_chain,
|
|
226
|
+
url.pathname
|
|
227
|
+
]);
|
|
228
|
+
} else {
|
|
229
|
+
await native_navigation(new URL(navigation_result.location, location.href));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
} else if (navigation_result.props?.page?.status >= 400) {
|
|
235
|
+
const updated = await stores.updated.check();
|
|
236
|
+
if (updated) {
|
|
237
|
+
await native_navigation(url);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
updating = true;
|
|
242
|
+
|
|
243
|
+
if (opts && opts.details) {
|
|
244
|
+
const { details } = opts;
|
|
245
|
+
const change = details.replaceState ? 0 : 1;
|
|
246
|
+
details.state[INDEX_KEY] = current_history_index += change;
|
|
247
|
+
history[details.replaceState ? 'replaceState' : 'pushState'](details.state, '', url);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (started) {
|
|
251
|
+
current = navigation_result.state;
|
|
252
|
+
|
|
253
|
+
if (navigation_result.props.page) {
|
|
254
|
+
navigation_result.props.page.url = url;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (import.meta.env.DEV) {
|
|
258
|
+
// Nasty hack to silence harmless warnings the user can do nothing about
|
|
259
|
+
const warn = console.warn;
|
|
260
|
+
console.warn = (...args) => {
|
|
261
|
+
if (
|
|
262
|
+
args.length !== 1 ||
|
|
263
|
+
!/<(Layout|Page)(_[\w$]+)?> was created with unknown prop '(data|errors)'/.test(args[0])
|
|
264
|
+
) {
|
|
265
|
+
warn(...args);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
root.$set(navigation_result.props);
|
|
269
|
+
tick().then(() => (console.warn = warn));
|
|
270
|
+
} else {
|
|
271
|
+
root.$set(navigation_result.props);
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
initialize(navigation_result);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// opts must be passed if we're navigating
|
|
278
|
+
if (opts) {
|
|
279
|
+
const { scroll, keepfocus } = opts;
|
|
280
|
+
|
|
281
|
+
if (!keepfocus) {
|
|
282
|
+
// Reset page selection and focus
|
|
283
|
+
// We try to mimic browsers' behaviour as closely as possible by targeting the
|
|
284
|
+
// first scrollable region, but unfortunately it's not a perfect match — e.g.
|
|
285
|
+
// shift-tabbing won't immediately cycle up from the end of the page on Chromium
|
|
286
|
+
// See https://html.spec.whatwg.org/multipage/interaction.html#get-the-focusable-area
|
|
287
|
+
const root = document.body;
|
|
288
|
+
const tabindex = root.getAttribute('tabindex');
|
|
289
|
+
|
|
290
|
+
root.tabIndex = -1;
|
|
291
|
+
root.focus({ preventScroll: true });
|
|
292
|
+
|
|
293
|
+
setTimeout(() => {
|
|
294
|
+
getSelection()?.removeAllRanges();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
// restore `tabindex` as to prevent `root` from stealing input from elements
|
|
298
|
+
if (tabindex !== null) {
|
|
299
|
+
root.setAttribute('tabindex', tabindex);
|
|
300
|
+
} else {
|
|
301
|
+
root.removeAttribute('tabindex');
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// need to render the DOM before we can scroll to the rendered elements
|
|
306
|
+
await tick();
|
|
307
|
+
|
|
308
|
+
if (autoscroll) {
|
|
309
|
+
const deep_linked = url.hash && document.getElementById(url.hash.slice(1));
|
|
310
|
+
if (scroll) {
|
|
311
|
+
scrollTo(scroll.x, scroll.y);
|
|
312
|
+
} else if (deep_linked) {
|
|
313
|
+
// Here we use `scrollIntoView` on the element instead of `scrollTo`
|
|
314
|
+
// because it natively supports the `scroll-margin` and `scroll-behavior`
|
|
315
|
+
// CSS properties.
|
|
316
|
+
deep_linked.scrollIntoView();
|
|
317
|
+
} else {
|
|
318
|
+
scrollTo(0, 0);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
} else {
|
|
322
|
+
// in this case we're simply invalidating
|
|
323
|
+
await tick();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
load_cache.promise = null;
|
|
327
|
+
load_cache.id = null;
|
|
328
|
+
autoscroll = true;
|
|
329
|
+
|
|
330
|
+
if (navigation_result.props.page) {
|
|
331
|
+
page = navigation_result.props.page;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const leaf_node = navigation_result.state.branch[navigation_result.state.branch.length - 1];
|
|
335
|
+
router_enabled = leaf_node?.node.shared?.router !== false;
|
|
336
|
+
|
|
337
|
+
if (callback) callback();
|
|
338
|
+
|
|
339
|
+
updating = false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** @param {import('./types').NavigationFinished} result */
|
|
343
|
+
function initialize(result) {
|
|
344
|
+
current = result.state;
|
|
345
|
+
|
|
346
|
+
const style = document.querySelector('style[data-sveltekit]');
|
|
347
|
+
if (style) style.remove();
|
|
348
|
+
|
|
349
|
+
page = result.props.page;
|
|
350
|
+
|
|
351
|
+
if (import.meta.env.DEV) {
|
|
352
|
+
// Nasty hack to silence harmless warnings the user can do nothing about
|
|
353
|
+
const warn = console.warn;
|
|
354
|
+
console.warn = (...args) => {
|
|
355
|
+
if (
|
|
356
|
+
args.length !== 1 ||
|
|
357
|
+
!/<(Layout|Page)(_[\w$]+)?> was created with unknown prop '(data|errors)'/.test(args[0])
|
|
358
|
+
) {
|
|
359
|
+
warn(...args);
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
root = new Root({
|
|
363
|
+
target,
|
|
364
|
+
props: { ...result.props, stores },
|
|
365
|
+
hydrate: true
|
|
366
|
+
});
|
|
367
|
+
console.warn = warn;
|
|
368
|
+
} else {
|
|
369
|
+
root = new Root({
|
|
370
|
+
target,
|
|
371
|
+
props: { ...result.props, stores },
|
|
372
|
+
hydrate: true
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (router_enabled) {
|
|
377
|
+
const navigation = { from: null, to: new URL(location.href) };
|
|
378
|
+
callbacks.after_navigate.forEach((fn) => fn(navigation));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
started = true;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
*
|
|
386
|
+
* @param {{
|
|
387
|
+
* url: URL;
|
|
388
|
+
* params: Record<string, string>;
|
|
389
|
+
* branch: Array<import('./types').BranchNode | undefined>;
|
|
390
|
+
* status: number;
|
|
391
|
+
* error: HttpError | Error | null;
|
|
392
|
+
* routeId: string | null;
|
|
393
|
+
* validation_errors?: string | undefined;
|
|
394
|
+
* }} opts
|
|
395
|
+
*/
|
|
396
|
+
async function get_navigation_result_from_branch({
|
|
397
|
+
url,
|
|
398
|
+
params,
|
|
399
|
+
branch,
|
|
400
|
+
status,
|
|
401
|
+
error,
|
|
402
|
+
routeId,
|
|
403
|
+
validation_errors
|
|
404
|
+
}) {
|
|
405
|
+
const filtered = /** @type {import('./types').BranchNode[] } */ (branch.filter(Boolean));
|
|
406
|
+
|
|
407
|
+
/** @type {import('./types').NavigationFinished} */
|
|
408
|
+
const result = {
|
|
409
|
+
type: 'loaded',
|
|
410
|
+
state: {
|
|
411
|
+
url,
|
|
412
|
+
params,
|
|
413
|
+
branch,
|
|
414
|
+
error,
|
|
415
|
+
session_id
|
|
416
|
+
},
|
|
417
|
+
props: {
|
|
418
|
+
components: filtered.map((branch_node) => branch_node.node.component),
|
|
419
|
+
errors: validation_errors
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
let data = {};
|
|
424
|
+
let data_changed = false;
|
|
425
|
+
for (let i = 0; i < filtered.length; i += 1) {
|
|
426
|
+
data = { ...data, ...filtered[i].data };
|
|
427
|
+
// Only set props if the node actually updated. This prevents needless rerenders.
|
|
428
|
+
if (data_changed || !current.branch.some((node) => node === filtered[i])) {
|
|
429
|
+
result.props[`data_${i}`] = data;
|
|
430
|
+
data_changed = true;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const page_changed =
|
|
435
|
+
!current.url || url.href !== current.url.href || current.error !== error || data_changed;
|
|
436
|
+
|
|
437
|
+
if (page_changed) {
|
|
438
|
+
result.props.page = { error, params, routeId, status, url, data };
|
|
439
|
+
|
|
440
|
+
// TODO remove this for 1.0
|
|
441
|
+
/**
|
|
442
|
+
* @param {string} property
|
|
443
|
+
* @param {string} replacement
|
|
444
|
+
*/
|
|
445
|
+
const print_error = (property, replacement) => {
|
|
446
|
+
Object.defineProperty(result.props.page, property, {
|
|
447
|
+
get: () => {
|
|
448
|
+
throw new Error(`$page.${property} has been replaced by $page.url.${replacement}`);
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
print_error('origin', 'origin');
|
|
454
|
+
print_error('path', 'pathname');
|
|
455
|
+
print_error('query', 'searchParams');
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
return result;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Call the load function of the given node, if it exists.
|
|
463
|
+
* If `server_data` is passed, this is treated as the initial run and the page endpoint is not requested.
|
|
464
|
+
*
|
|
465
|
+
* @param {{
|
|
466
|
+
* node: import('types').CSRPageNode;
|
|
467
|
+
* parent: () => Promise<Record<string, any>>;
|
|
468
|
+
* url: URL;
|
|
469
|
+
* params: Record<string, string>;
|
|
470
|
+
* routeId: string | null;
|
|
471
|
+
* server_data: Record<string, any> | null;
|
|
472
|
+
* }} options
|
|
473
|
+
* @returns {Promise<import('./types').BranchNode>}
|
|
474
|
+
*/
|
|
475
|
+
async function load_node({ node, parent, url, params, routeId, server_data }) {
|
|
476
|
+
const uses = {
|
|
477
|
+
params: new Set(),
|
|
478
|
+
url: false,
|
|
479
|
+
dependencies: new Set(),
|
|
480
|
+
parent: false
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
/** @param {string[]} deps */
|
|
484
|
+
function depends(...deps) {
|
|
485
|
+
for (const dep of deps) {
|
|
486
|
+
const { href } = new URL(dep, url);
|
|
487
|
+
uses.dependencies.add(href);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** @type {Record<string, any> | null} */
|
|
492
|
+
let data = null;
|
|
493
|
+
|
|
494
|
+
if (node.server) {
|
|
495
|
+
// +page|layout.server.js data means we need to mark this URL as a dependency of itself,
|
|
496
|
+
// unless we want to get clever with usage detection on the server, which could
|
|
497
|
+
// be returned to the client either as payload or custom headers
|
|
498
|
+
uses.dependencies.add(url.href);
|
|
499
|
+
uses.url = true;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** @type {Record<string, string>} */
|
|
503
|
+
const uses_params = {};
|
|
504
|
+
for (const key in params) {
|
|
505
|
+
Object.defineProperty(uses_params, key, {
|
|
506
|
+
get() {
|
|
507
|
+
uses.params.add(key);
|
|
508
|
+
return params[key];
|
|
509
|
+
},
|
|
510
|
+
enumerable: true
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const load_url = new LoadURL(url);
|
|
515
|
+
|
|
516
|
+
if (node.shared?.load) {
|
|
517
|
+
/** @type {import('types').LoadEvent} */
|
|
518
|
+
const load_input = {
|
|
519
|
+
routeId,
|
|
520
|
+
params: uses_params,
|
|
521
|
+
data: server_data,
|
|
522
|
+
get url() {
|
|
523
|
+
uses.url = true;
|
|
524
|
+
return load_url;
|
|
525
|
+
},
|
|
526
|
+
async fetch(resource, init) {
|
|
527
|
+
let requested;
|
|
528
|
+
|
|
529
|
+
if (typeof resource === 'string') {
|
|
530
|
+
requested = resource;
|
|
531
|
+
} else {
|
|
532
|
+
requested = resource.url;
|
|
533
|
+
|
|
534
|
+
// we're not allowed to modify the received `Request` object, so in order
|
|
535
|
+
// to fixup relative urls we create a new equivalent `init` object instead
|
|
536
|
+
init = {
|
|
537
|
+
// the request body must be consumed in memory until browsers
|
|
538
|
+
// implement streaming request bodies and/or the body getter
|
|
539
|
+
body:
|
|
540
|
+
resource.method === 'GET' || resource.method === 'HEAD'
|
|
541
|
+
? undefined
|
|
542
|
+
: await resource.blob(),
|
|
543
|
+
cache: resource.cache,
|
|
544
|
+
credentials: resource.credentials,
|
|
545
|
+
headers: resource.headers,
|
|
546
|
+
integrity: resource.integrity,
|
|
547
|
+
keepalive: resource.keepalive,
|
|
548
|
+
method: resource.method,
|
|
549
|
+
mode: resource.mode,
|
|
550
|
+
redirect: resource.redirect,
|
|
551
|
+
referrer: resource.referrer,
|
|
552
|
+
referrerPolicy: resource.referrerPolicy,
|
|
553
|
+
signal: resource.signal,
|
|
554
|
+
...init
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// we must fixup relative urls so they are resolved from the target page
|
|
559
|
+
const normalized = new URL(requested, url).href;
|
|
560
|
+
depends(normalized);
|
|
561
|
+
|
|
562
|
+
// prerendered pages may be served from any origin, so `initial_fetch` urls shouldn't be normalized
|
|
563
|
+
return started ? native_fetch(normalized, init) : initial_fetch(requested, init);
|
|
564
|
+
},
|
|
565
|
+
setHeaders: () => {}, // noop
|
|
566
|
+
depends,
|
|
567
|
+
get parent() {
|
|
568
|
+
// uses.parent assignment here, not on method inokation, else we wouldn't notice when someone
|
|
569
|
+
// does await parent() inside an if branch which wasn't executed yet.
|
|
570
|
+
uses.parent = true;
|
|
571
|
+
return parent;
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
// TODO remove this for 1.0
|
|
576
|
+
Object.defineProperties(load_input, {
|
|
577
|
+
props: {
|
|
578
|
+
get() {
|
|
579
|
+
throw new Error(
|
|
580
|
+
'@migration task: Replace `props` with `data` stuff https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292693'
|
|
581
|
+
);
|
|
582
|
+
},
|
|
583
|
+
enumerable: false
|
|
584
|
+
},
|
|
585
|
+
session: {
|
|
586
|
+
get() {
|
|
587
|
+
throw new Error(
|
|
588
|
+
'session is no longer available. See https://github.com/sveltejs/kit/discussions/5883'
|
|
589
|
+
);
|
|
590
|
+
},
|
|
591
|
+
enumerable: false
|
|
592
|
+
},
|
|
593
|
+
stuff: {
|
|
594
|
+
get() {
|
|
595
|
+
throw new Error(
|
|
596
|
+
'@migration task: Remove stuff https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292693'
|
|
597
|
+
);
|
|
598
|
+
},
|
|
599
|
+
enumerable: false
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
if (import.meta.env.DEV) {
|
|
604
|
+
try {
|
|
605
|
+
lock_fetch();
|
|
606
|
+
data = (await node.shared.load.call(null, load_input)) ?? null;
|
|
607
|
+
} finally {
|
|
608
|
+
unlock_fetch();
|
|
609
|
+
}
|
|
610
|
+
} else {
|
|
611
|
+
data = (await node.shared.load.call(null, load_input)) ?? null;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
return {
|
|
616
|
+
node,
|
|
617
|
+
data: data || server_data,
|
|
618
|
+
uses
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* @param {import('./types').NavigationIntent} intent
|
|
624
|
+
* @returns {Promise<import('./types').NavigationResult | undefined>}
|
|
625
|
+
*/
|
|
626
|
+
async function load_route({ id, url, params, route }) {
|
|
627
|
+
if (load_cache.id === id && load_cache.promise) {
|
|
628
|
+
return load_cache.promise;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
const { errors, layouts, leaf } = route;
|
|
632
|
+
|
|
633
|
+
const changed = current.url && {
|
|
634
|
+
url: id !== current.url.pathname + current.url.search,
|
|
635
|
+
params: Object.keys(params).filter((key) => current.params[key] !== params[key])
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
// preload modules to avoid waterfall, but handle rejections
|
|
639
|
+
// so they don't get reported to Sentry et al (we don't need
|
|
640
|
+
// to act on the failures at this point)
|
|
641
|
+
[...errors, ...layouts, leaf].forEach((loader) => loader?.().catch(() => {}));
|
|
642
|
+
|
|
643
|
+
const nodes = [...layouts, leaf];
|
|
644
|
+
|
|
645
|
+
// To avoid waterfalls when someone awaits a parent, compute as much as possible here already
|
|
646
|
+
/** @type {boolean[]} */
|
|
647
|
+
const nodes_changed_since_last_render = [];
|
|
648
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
649
|
+
if (!nodes[i]) {
|
|
650
|
+
nodes_changed_since_last_render.push(false);
|
|
651
|
+
} else {
|
|
652
|
+
const previous = current.branch[i];
|
|
653
|
+
const changed_since_last_render =
|
|
654
|
+
!previous ||
|
|
655
|
+
(changed.url && previous.uses.url) ||
|
|
656
|
+
changed.params.some((param) => previous.uses.params.has(param)) ||
|
|
657
|
+
Array.from(previous.uses.dependencies).some((dep) => invalidated.some((fn) => fn(dep))) ||
|
|
658
|
+
(previous.uses.parent && nodes_changed_since_last_render.includes(true));
|
|
659
|
+
nodes_changed_since_last_render.push(changed_since_last_render);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/** @type {import('./types').ServerDataPayload | null} */
|
|
664
|
+
let server_data_payload = null;
|
|
665
|
+
|
|
666
|
+
if (route.uses_server_data) {
|
|
667
|
+
try {
|
|
668
|
+
const res = await native_fetch(
|
|
669
|
+
`${url.pathname}${url.pathname.endsWith('/') ? '' : '/'}__data.json${url.search}`
|
|
670
|
+
);
|
|
671
|
+
|
|
672
|
+
server_data_payload = /** @type {import('./types').ServerDataPayload} */ (await res.json());
|
|
673
|
+
|
|
674
|
+
if (!res.ok) {
|
|
675
|
+
throw server_data_payload;
|
|
676
|
+
}
|
|
677
|
+
} catch (e) {
|
|
678
|
+
throw new Error('TODO render fallback error page');
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
if (server_data_payload.type === 'redirect') {
|
|
682
|
+
return server_data_payload;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
const server_data_nodes = server_data_payload?.nodes;
|
|
687
|
+
|
|
688
|
+
const branch_promises = nodes.map(async (loader, i) => {
|
|
689
|
+
return Promise.resolve().then(async () => {
|
|
690
|
+
if (!loader) return;
|
|
691
|
+
const node = await loader();
|
|
692
|
+
|
|
693
|
+
/** @type {import('./types').BranchNode | undefined} */
|
|
694
|
+
const previous = current.branch[i];
|
|
695
|
+
const changed_since_last_render =
|
|
696
|
+
nodes_changed_since_last_render[i] || !previous || node !== previous.node;
|
|
697
|
+
|
|
698
|
+
if (changed_since_last_render) {
|
|
699
|
+
const payload = server_data_nodes?.[i];
|
|
700
|
+
|
|
701
|
+
if (payload?.status) {
|
|
702
|
+
throw error(payload.status, payload.message);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (payload?.error) {
|
|
706
|
+
throw payload.error;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
return await load_node({
|
|
710
|
+
node,
|
|
711
|
+
url,
|
|
712
|
+
params,
|
|
713
|
+
routeId: route.id,
|
|
714
|
+
parent: async () => {
|
|
715
|
+
const data = {};
|
|
716
|
+
for (let j = 0; j < i; j += 1) {
|
|
717
|
+
Object.assign(data, (await branch_promises[j])?.data);
|
|
718
|
+
}
|
|
719
|
+
return data;
|
|
720
|
+
},
|
|
721
|
+
server_data: payload?.data ?? null
|
|
722
|
+
});
|
|
723
|
+
} else {
|
|
724
|
+
return previous;
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
// if we don't do this, rejections will be unhandled
|
|
730
|
+
for (const p of branch_promises) p.catch(() => {});
|
|
731
|
+
|
|
732
|
+
/** @type {Array<import('./types').BranchNode | undefined>} */
|
|
733
|
+
const branch = [];
|
|
734
|
+
|
|
735
|
+
for (let i = 0; i < nodes.length; i += 1) {
|
|
736
|
+
if (nodes[i]) {
|
|
737
|
+
try {
|
|
738
|
+
branch.push(await branch_promises[i]);
|
|
739
|
+
} catch (e) {
|
|
740
|
+
const error = normalize_error(e);
|
|
741
|
+
|
|
742
|
+
if (error instanceof Redirect) {
|
|
743
|
+
return {
|
|
744
|
+
type: 'redirect',
|
|
745
|
+
location: error.location
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const status = e instanceof HttpError ? e.status : 500;
|
|
750
|
+
|
|
751
|
+
while (i--) {
|
|
752
|
+
if (errors[i]) {
|
|
753
|
+
/** @type {import('./types').BranchNode | undefined} */
|
|
754
|
+
let error_loaded;
|
|
755
|
+
|
|
756
|
+
let j = i;
|
|
757
|
+
while (!branch[j]) j -= 1;
|
|
758
|
+
|
|
759
|
+
try {
|
|
760
|
+
error_loaded = {
|
|
761
|
+
node: await errors[i](),
|
|
762
|
+
data: {},
|
|
763
|
+
uses: {
|
|
764
|
+
params: new Set(),
|
|
765
|
+
url: false,
|
|
766
|
+
dependencies: new Set(),
|
|
767
|
+
parent: false
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
return await get_navigation_result_from_branch({
|
|
772
|
+
url,
|
|
773
|
+
params,
|
|
774
|
+
branch: branch.slice(0, j + 1).concat(error_loaded),
|
|
775
|
+
status,
|
|
776
|
+
error,
|
|
777
|
+
routeId: route.id
|
|
778
|
+
});
|
|
779
|
+
} catch (e) {
|
|
780
|
+
continue;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
return await load_root_error_page({
|
|
786
|
+
status,
|
|
787
|
+
error,
|
|
788
|
+
url,
|
|
789
|
+
routeId: route.id
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
} else {
|
|
793
|
+
// push an empty slot so we can rewind past gaps to the
|
|
794
|
+
// layout that corresponds with an +error.svelte page
|
|
795
|
+
branch.push(undefined);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
return await get_navigation_result_from_branch({
|
|
800
|
+
url,
|
|
801
|
+
params,
|
|
802
|
+
branch,
|
|
803
|
+
status: 200,
|
|
804
|
+
error: null,
|
|
805
|
+
routeId: route.id
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* @param {{
|
|
811
|
+
* status: number;
|
|
812
|
+
* error: HttpError | Error;
|
|
813
|
+
* url: URL;
|
|
814
|
+
* routeId: string | null
|
|
815
|
+
* }} opts
|
|
816
|
+
*/
|
|
817
|
+
async function load_root_error_page({ status, error, url, routeId }) {
|
|
818
|
+
/** @type {Record<string, string>} */
|
|
819
|
+
const params = {}; // error page does not have params
|
|
820
|
+
|
|
821
|
+
const root_layout = await load_node({
|
|
822
|
+
node: await default_layout,
|
|
823
|
+
url,
|
|
824
|
+
params,
|
|
825
|
+
routeId,
|
|
826
|
+
parent: () => Promise.resolve({}),
|
|
827
|
+
server_data: null // TODO!!!!!
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
const root_error = {
|
|
831
|
+
node: await default_error,
|
|
832
|
+
data: null,
|
|
833
|
+
// TODO make this unnecessary
|
|
834
|
+
uses: {
|
|
835
|
+
params: new Set(),
|
|
836
|
+
url: false,
|
|
837
|
+
dependencies: new Set(),
|
|
838
|
+
parent: false
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
return await get_navigation_result_from_branch({
|
|
843
|
+
url,
|
|
844
|
+
params,
|
|
845
|
+
branch: [root_layout, root_error],
|
|
846
|
+
status,
|
|
847
|
+
error,
|
|
848
|
+
routeId
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** @param {URL} url */
|
|
853
|
+
function get_navigation_intent(url) {
|
|
854
|
+
if (url.origin !== location.origin || !url.pathname.startsWith(base)) return;
|
|
855
|
+
|
|
856
|
+
const path = decodeURI(url.pathname.slice(base.length) || '/');
|
|
857
|
+
|
|
858
|
+
for (const route of routes) {
|
|
859
|
+
const params = route.exec(path);
|
|
860
|
+
|
|
861
|
+
if (params) {
|
|
862
|
+
const normalized = new URL(
|
|
863
|
+
url.origin + normalize_path(url.pathname, trailing_slash) + url.search + url.hash
|
|
864
|
+
);
|
|
865
|
+
const id = normalized.pathname + normalized.search;
|
|
866
|
+
/** @type {import('./types').NavigationIntent} */
|
|
867
|
+
const intent = { id, route, params: decode_params(params), url: normalized };
|
|
868
|
+
return intent;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* @param {{
|
|
875
|
+
* url: URL;
|
|
876
|
+
* scroll: { x: number, y: number } | null;
|
|
877
|
+
* keepfocus: boolean;
|
|
878
|
+
* redirect_chain: string[];
|
|
879
|
+
* details: {
|
|
880
|
+
* replaceState: boolean;
|
|
881
|
+
* state: any;
|
|
882
|
+
* } | null;
|
|
883
|
+
* accepted: () => void;
|
|
884
|
+
* blocked: () => void;
|
|
885
|
+
* }} opts
|
|
886
|
+
*/
|
|
887
|
+
async function navigate({ url, scroll, keepfocus, redirect_chain, details, accepted, blocked }) {
|
|
888
|
+
const from = current.url;
|
|
889
|
+
let should_block = false;
|
|
890
|
+
|
|
891
|
+
const navigation = {
|
|
892
|
+
from,
|
|
893
|
+
to: url,
|
|
894
|
+
cancel: () => (should_block = true)
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
callbacks.before_navigate.forEach((fn) => fn(navigation));
|
|
898
|
+
|
|
899
|
+
if (should_block) {
|
|
900
|
+
blocked();
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
update_scroll_positions(current_history_index);
|
|
905
|
+
|
|
906
|
+
accepted();
|
|
907
|
+
|
|
908
|
+
if (started) {
|
|
909
|
+
stores.navigating.set({
|
|
910
|
+
from: current.url,
|
|
911
|
+
to: url
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
await update(
|
|
916
|
+
url,
|
|
917
|
+
redirect_chain,
|
|
918
|
+
{
|
|
919
|
+
scroll,
|
|
920
|
+
keepfocus,
|
|
921
|
+
details
|
|
922
|
+
},
|
|
923
|
+
() => {
|
|
924
|
+
const navigation = { from, to: url };
|
|
925
|
+
callbacks.after_navigate.forEach((fn) => fn(navigation));
|
|
926
|
+
|
|
927
|
+
stores.navigating.set(null);
|
|
928
|
+
}
|
|
929
|
+
);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* Loads `href` the old-fashioned way, with a full page reload.
|
|
934
|
+
* Returns a `Promise` that never resolves (to prevent any
|
|
935
|
+
* subsequent work, e.g. history manipulation, from happening)
|
|
936
|
+
* @param {URL} url
|
|
937
|
+
*/
|
|
938
|
+
function native_navigation(url) {
|
|
939
|
+
location.href = url.href;
|
|
940
|
+
return new Promise(() => {});
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
if (import.meta.hot) {
|
|
944
|
+
import.meta.hot.on('vite:beforeUpdate', () => {
|
|
945
|
+
if (current.error) location.reload();
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
return {
|
|
950
|
+
after_navigate: (fn) => {
|
|
951
|
+
onMount(() => {
|
|
952
|
+
callbacks.after_navigate.push(fn);
|
|
953
|
+
|
|
954
|
+
return () => {
|
|
955
|
+
const i = callbacks.after_navigate.indexOf(fn);
|
|
956
|
+
callbacks.after_navigate.splice(i, 1);
|
|
957
|
+
};
|
|
958
|
+
});
|
|
959
|
+
},
|
|
960
|
+
|
|
961
|
+
before_navigate: (fn) => {
|
|
962
|
+
onMount(() => {
|
|
963
|
+
callbacks.before_navigate.push(fn);
|
|
964
|
+
|
|
965
|
+
return () => {
|
|
966
|
+
const i = callbacks.before_navigate.indexOf(fn);
|
|
967
|
+
callbacks.before_navigate.splice(i, 1);
|
|
968
|
+
};
|
|
969
|
+
});
|
|
970
|
+
},
|
|
971
|
+
|
|
972
|
+
disable_scroll_handling: () => {
|
|
973
|
+
if (import.meta.env.DEV && started && !updating) {
|
|
974
|
+
throw new Error('Can only disable scroll handling during navigation');
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
if (updating || !started) {
|
|
978
|
+
autoscroll = false;
|
|
979
|
+
}
|
|
980
|
+
},
|
|
981
|
+
|
|
982
|
+
goto: (href, opts = {}) => goto(href, opts, []),
|
|
983
|
+
|
|
984
|
+
invalidate: (resource) => {
|
|
985
|
+
if (resource === undefined) {
|
|
986
|
+
// Force rerun of all load functions, regardless of their dependencies
|
|
987
|
+
for (const node of current.branch) {
|
|
988
|
+
node?.uses.dependencies.add('');
|
|
989
|
+
}
|
|
990
|
+
invalidated.push(() => true);
|
|
991
|
+
} else if (typeof resource === 'function') {
|
|
992
|
+
invalidated.push(resource);
|
|
993
|
+
} else {
|
|
994
|
+
const { href } = new URL(resource, location.href);
|
|
995
|
+
invalidated.push((dep) => dep === href);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
if (!invalidating) {
|
|
999
|
+
invalidating = Promise.resolve().then(async () => {
|
|
1000
|
+
await update(new URL(location.href), []);
|
|
1001
|
+
|
|
1002
|
+
invalidating = null;
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
return invalidating;
|
|
1007
|
+
},
|
|
1008
|
+
|
|
1009
|
+
prefetch: async (href) => {
|
|
1010
|
+
const url = new URL(href, get_base_uri(document));
|
|
1011
|
+
await prefetch(url);
|
|
1012
|
+
},
|
|
1013
|
+
|
|
1014
|
+
// TODO rethink this API
|
|
1015
|
+
prefetch_routes: async (pathnames) => {
|
|
1016
|
+
const matching = pathnames
|
|
1017
|
+
? routes.filter((route) => pathnames.some((pathname) => route.exec(pathname)))
|
|
1018
|
+
: routes;
|
|
1019
|
+
|
|
1020
|
+
const promises = matching.map((r) => {
|
|
1021
|
+
return Promise.all([...r.layouts, r.leaf].map((load) => load?.()));
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
await Promise.all(promises);
|
|
1025
|
+
},
|
|
1026
|
+
|
|
1027
|
+
_start_router: () => {
|
|
1028
|
+
history.scrollRestoration = 'manual';
|
|
1029
|
+
|
|
1030
|
+
// Adopted from Nuxt.js
|
|
1031
|
+
// Reset scrollRestoration to auto when leaving page, allowing page reload
|
|
1032
|
+
// and back-navigation from other pages to use the browser to restore the
|
|
1033
|
+
// scrolling position.
|
|
1034
|
+
addEventListener('beforeunload', (e) => {
|
|
1035
|
+
let should_block = false;
|
|
1036
|
+
|
|
1037
|
+
const navigation = {
|
|
1038
|
+
from: current.url,
|
|
1039
|
+
to: null,
|
|
1040
|
+
cancel: () => (should_block = true)
|
|
1041
|
+
};
|
|
1042
|
+
|
|
1043
|
+
callbacks.before_navigate.forEach((fn) => fn(navigation));
|
|
1044
|
+
|
|
1045
|
+
if (should_block) {
|
|
1046
|
+
e.preventDefault();
|
|
1047
|
+
e.returnValue = '';
|
|
1048
|
+
} else {
|
|
1049
|
+
history.scrollRestoration = 'auto';
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
addEventListener('visibilitychange', () => {
|
|
1054
|
+
if (document.visibilityState === 'hidden') {
|
|
1055
|
+
update_scroll_positions(current_history_index);
|
|
1056
|
+
|
|
1057
|
+
try {
|
|
1058
|
+
sessionStorage[SCROLL_KEY] = JSON.stringify(scroll_positions);
|
|
1059
|
+
} catch {
|
|
1060
|
+
// do nothing
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
/** @param {Event} event */
|
|
1066
|
+
const trigger_prefetch = (event) => {
|
|
1067
|
+
const a = find_anchor(event);
|
|
1068
|
+
if (a && a.href && a.hasAttribute('sveltekit:prefetch')) {
|
|
1069
|
+
prefetch(get_href(a));
|
|
1070
|
+
}
|
|
1071
|
+
};
|
|
1072
|
+
|
|
1073
|
+
/** @type {NodeJS.Timeout} */
|
|
1074
|
+
let mousemove_timeout;
|
|
1075
|
+
|
|
1076
|
+
/** @param {MouseEvent|TouchEvent} event */
|
|
1077
|
+
const handle_mousemove = (event) => {
|
|
1078
|
+
clearTimeout(mousemove_timeout);
|
|
1079
|
+
mousemove_timeout = setTimeout(() => {
|
|
1080
|
+
// event.composedPath(), which is used in find_anchor, will be empty if the event is read in a timeout
|
|
1081
|
+
// add a layer of indirection to address that
|
|
1082
|
+
event.target?.dispatchEvent(
|
|
1083
|
+
new CustomEvent('sveltekit:trigger_prefetch', { bubbles: true })
|
|
1084
|
+
);
|
|
1085
|
+
}, 20);
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
addEventListener('touchstart', trigger_prefetch);
|
|
1089
|
+
addEventListener('mousemove', handle_mousemove);
|
|
1090
|
+
addEventListener('sveltekit:trigger_prefetch', trigger_prefetch);
|
|
1091
|
+
|
|
1092
|
+
/** @param {MouseEvent} event */
|
|
1093
|
+
addEventListener('click', (event) => {
|
|
1094
|
+
if (!router_enabled) return;
|
|
1095
|
+
|
|
1096
|
+
// Adapted from https://github.com/visionmedia/page.js
|
|
1097
|
+
// MIT license https://github.com/visionmedia/page.js#license
|
|
1098
|
+
if (event.button || event.which !== 1) return;
|
|
1099
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
1100
|
+
if (event.defaultPrevented) return;
|
|
1101
|
+
|
|
1102
|
+
const a = find_anchor(event);
|
|
1103
|
+
if (!a) return;
|
|
1104
|
+
|
|
1105
|
+
if (!a.href) return;
|
|
1106
|
+
|
|
1107
|
+
const is_svg_a_element = a instanceof SVGAElement;
|
|
1108
|
+
const url = get_href(a);
|
|
1109
|
+
|
|
1110
|
+
// Ignore non-HTTP URL protocols (e.g. `mailto:`, `tel:`, `myapp:`, etc.)
|
|
1111
|
+
// MEMO: Without this condition, firefox will open mailer twice.
|
|
1112
|
+
// See:
|
|
1113
|
+
// - https://github.com/sveltejs/kit/issues/4045
|
|
1114
|
+
// - https://github.com/sveltejs/kit/issues/5725
|
|
1115
|
+
if (!is_svg_a_element && !(url.protocol === 'https:' || url.protocol === 'http:')) return;
|
|
1116
|
+
|
|
1117
|
+
// Ignore if tag has
|
|
1118
|
+
// 1. 'download' attribute
|
|
1119
|
+
// 2. 'rel' attribute includes external
|
|
1120
|
+
const rel = (a.getAttribute('rel') || '').split(/\s+/);
|
|
1121
|
+
|
|
1122
|
+
if (
|
|
1123
|
+
a.hasAttribute('download') ||
|
|
1124
|
+
rel.includes('external') ||
|
|
1125
|
+
a.hasAttribute('sveltekit:reload')
|
|
1126
|
+
) {
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
// Ignore if <a> has a target
|
|
1131
|
+
if (is_svg_a_element ? a.target.baseVal : a.target) return;
|
|
1132
|
+
|
|
1133
|
+
// Check if new url only differs by hash and use the browser default behavior in that case
|
|
1134
|
+
// This will ensure the `hashchange` event is fired
|
|
1135
|
+
// Removing the hash does a full page navigation in the browser, so make sure a hash is present
|
|
1136
|
+
const [base, hash] = url.href.split('#');
|
|
1137
|
+
if (hash !== undefined && base === location.href.split('#')[0]) {
|
|
1138
|
+
// set this flag to distinguish between navigations triggered by
|
|
1139
|
+
// clicking a hash link and those triggered by popstate
|
|
1140
|
+
hash_navigating = true;
|
|
1141
|
+
|
|
1142
|
+
update_scroll_positions(current_history_index);
|
|
1143
|
+
|
|
1144
|
+
stores.page.set({ ...page, url });
|
|
1145
|
+
stores.page.notify();
|
|
1146
|
+
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
navigate({
|
|
1151
|
+
url,
|
|
1152
|
+
scroll: a.hasAttribute('sveltekit:noscroll') ? scroll_state() : null,
|
|
1153
|
+
keepfocus: false,
|
|
1154
|
+
redirect_chain: [],
|
|
1155
|
+
details: {
|
|
1156
|
+
state: {},
|
|
1157
|
+
replaceState: url.href === location.href
|
|
1158
|
+
},
|
|
1159
|
+
accepted: () => event.preventDefault(),
|
|
1160
|
+
blocked: () => event.preventDefault()
|
|
1161
|
+
});
|
|
1162
|
+
});
|
|
1163
|
+
|
|
1164
|
+
addEventListener('popstate', (event) => {
|
|
1165
|
+
if (event.state && router_enabled) {
|
|
1166
|
+
// if a popstate-driven navigation is cancelled, we need to counteract it
|
|
1167
|
+
// with history.go, which means we end up back here, hence this check
|
|
1168
|
+
if (event.state[INDEX_KEY] === current_history_index) return;
|
|
1169
|
+
|
|
1170
|
+
navigate({
|
|
1171
|
+
url: new URL(location.href),
|
|
1172
|
+
scroll: scroll_positions[event.state[INDEX_KEY]],
|
|
1173
|
+
keepfocus: false,
|
|
1174
|
+
redirect_chain: [],
|
|
1175
|
+
details: null,
|
|
1176
|
+
accepted: () => {
|
|
1177
|
+
current_history_index = event.state[INDEX_KEY];
|
|
1178
|
+
},
|
|
1179
|
+
blocked: () => {
|
|
1180
|
+
const delta = current_history_index - event.state[INDEX_KEY];
|
|
1181
|
+
history.go(delta);
|
|
1182
|
+
}
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
});
|
|
1186
|
+
|
|
1187
|
+
addEventListener('hashchange', () => {
|
|
1188
|
+
// if the hashchange happened as a result of clicking on a link,
|
|
1189
|
+
// we need to update history, otherwise we have to leave it alone
|
|
1190
|
+
if (hash_navigating) {
|
|
1191
|
+
hash_navigating = false;
|
|
1192
|
+
history.replaceState(
|
|
1193
|
+
{ ...history.state, [INDEX_KEY]: ++current_history_index },
|
|
1194
|
+
'',
|
|
1195
|
+
location.href
|
|
1196
|
+
);
|
|
1197
|
+
}
|
|
1198
|
+
});
|
|
1199
|
+
|
|
1200
|
+
// fix link[rel=icon], because browsers will occasionally try to load relative
|
|
1201
|
+
// URLs after a pushState/replaceState, resulting in a 404 — see
|
|
1202
|
+
// https://github.com/sveltejs/kit/issues/3748#issuecomment-1125980897
|
|
1203
|
+
for (const link of document.querySelectorAll('link')) {
|
|
1204
|
+
if (link.rel === 'icon') link.href = link.href;
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
addEventListener('pageshow', (event) => {
|
|
1208
|
+
// If the user navigates to another site and then uses the back button and
|
|
1209
|
+
// bfcache hits, we need to set navigating to null, the site doesn't know
|
|
1210
|
+
// the navigation away from it was successful.
|
|
1211
|
+
// Info about bfcache here: https://web.dev/bfcache
|
|
1212
|
+
if (event.persisted) {
|
|
1213
|
+
stores.navigating.set(null);
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
},
|
|
1217
|
+
|
|
1218
|
+
_hydrate: async ({ status, error, node_ids, params, routeId }) => {
|
|
1219
|
+
const url = new URL(location.href);
|
|
1220
|
+
|
|
1221
|
+
/** @type {import('./types').NavigationFinished | undefined} */
|
|
1222
|
+
let result;
|
|
1223
|
+
|
|
1224
|
+
try {
|
|
1225
|
+
/**
|
|
1226
|
+
* @param {string} type
|
|
1227
|
+
* @param {any} fallback
|
|
1228
|
+
*/
|
|
1229
|
+
const parse = (type, fallback) => {
|
|
1230
|
+
const script = document.querySelector(`script[sveltekit\\:data-type="${type}"]`);
|
|
1231
|
+
return script?.textContent ? JSON.parse(script.textContent) : fallback;
|
|
1232
|
+
};
|
|
1233
|
+
const server_data = parse('server_data', []);
|
|
1234
|
+
const validation_errors = parse('validation_errors', undefined);
|
|
1235
|
+
|
|
1236
|
+
const branch_promises = node_ids.map(async (n, i) => {
|
|
1237
|
+
return load_node({
|
|
1238
|
+
node: await nodes[n](),
|
|
1239
|
+
url,
|
|
1240
|
+
params,
|
|
1241
|
+
routeId,
|
|
1242
|
+
parent: async () => {
|
|
1243
|
+
const data = {};
|
|
1244
|
+
for (let j = 0; j < i; j += 1) {
|
|
1245
|
+
Object.assign(data, (await branch_promises[j]).data);
|
|
1246
|
+
}
|
|
1247
|
+
return data;
|
|
1248
|
+
},
|
|
1249
|
+
server_data: server_data[i] ?? null
|
|
1250
|
+
});
|
|
1251
|
+
});
|
|
1252
|
+
|
|
1253
|
+
result = await get_navigation_result_from_branch({
|
|
1254
|
+
url,
|
|
1255
|
+
params,
|
|
1256
|
+
branch: await Promise.all(branch_promises),
|
|
1257
|
+
status,
|
|
1258
|
+
error: /** @type {import('../server/page/types').SerializedHttpError} */ (error)
|
|
1259
|
+
?.__is_http_error
|
|
1260
|
+
? new HttpError(
|
|
1261
|
+
/** @type {import('../server/page/types').SerializedHttpError} */ (error).status,
|
|
1262
|
+
error.message
|
|
1263
|
+
)
|
|
1264
|
+
: error,
|
|
1265
|
+
validation_errors,
|
|
1266
|
+
routeId
|
|
1267
|
+
});
|
|
1268
|
+
} catch (e) {
|
|
1269
|
+
const error = normalize_error(e);
|
|
1270
|
+
|
|
1271
|
+
if (error instanceof Redirect) {
|
|
1272
|
+
// this is a real edge case — `load` would need to return
|
|
1273
|
+
// a redirect but only in the browser
|
|
1274
|
+
await native_navigation(new URL(/** @type {Redirect} */ (e).location, location.href));
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
result = await load_root_error_page({
|
|
1279
|
+
status: error instanceof HttpError ? error.status : 500,
|
|
1280
|
+
error,
|
|
1281
|
+
url,
|
|
1282
|
+
routeId
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
initialize(result);
|
|
1287
|
+
}
|
|
1288
|
+
};
|
|
1289
|
+
}
|