@sveltejs/kit 1.0.0-next.5 → 1.0.0-next.500
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 +94 -52
- package/postinstall.js +38 -0
- package/scripts/special-types/$env+dynamic+private.md +10 -0
- package/scripts/special-types/$env+dynamic+public.md +8 -0
- package/scripts/special-types/$env+static+private.md +19 -0
- package/scripts/special-types/$env+static+public.md +7 -0
- package/scripts/special-types/$lib.md +5 -0
- package/src/cli.js +108 -0
- package/src/constants.js +7 -0
- package/src/core/adapt/builder.js +206 -0
- package/src/core/adapt/index.js +31 -0
- package/src/core/config/default-error.html +56 -0
- package/src/core/config/index.js +110 -0
- package/src/core/config/options.js +504 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/env.js +121 -0
- package/src/core/generate_manifest/index.js +93 -0
- package/src/core/prerender/crawl.js +198 -0
- package/src/core/prerender/entities.js +2252 -0
- package/src/core/prerender/prerender.js +431 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +488 -0
- package/src/core/sync/create_manifest_data/types.d.ts +37 -0
- package/src/core/sync/sync.js +70 -0
- package/src/core/sync/utils.js +33 -0
- package/src/core/sync/write_ambient.js +53 -0
- package/src/core/sync/write_client_manifest.js +106 -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/index.js +678 -0
- package/src/core/utils.js +70 -0
- package/src/exports/hooks/index.js +1 -0
- package/src/exports/hooks/sequence.js +44 -0
- package/src/exports/index.js +45 -0
- package/src/exports/node/index.js +173 -0
- package/src/exports/node/polyfills.js +41 -0
- package/src/exports/vite/build/build_server.js +378 -0
- package/src/exports/vite/build/build_service_worker.js +90 -0
- package/src/exports/vite/build/utils.js +180 -0
- package/src/exports/vite/dev/index.js +577 -0
- package/src/exports/vite/graph_analysis/index.js +277 -0
- package/src/exports/vite/graph_analysis/types.d.ts +5 -0
- package/src/exports/vite/graph_analysis/utils.js +30 -0
- package/src/exports/vite/index.js +598 -0
- package/src/exports/vite/preview/index.js +189 -0
- package/src/exports/vite/types.d.ts +3 -0
- package/src/exports/vite/utils.js +157 -0
- package/src/runtime/app/env.js +1 -0
- package/src/runtime/app/environment.js +11 -0
- package/src/runtime/app/forms.js +114 -0
- package/src/runtime/app/navigation.js +23 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +102 -0
- package/src/runtime/client/ambient.d.ts +26 -0
- package/src/runtime/client/client.js +1583 -0
- package/src/runtime/client/fetcher.js +107 -0
- package/src/runtime/client/parse.js +60 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +37 -0
- package/src/runtime/client/types.d.ts +84 -0
- package/src/runtime/client/utils.js +159 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/control.js +98 -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 +6 -0
- package/src/runtime/env-public.js +6 -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/cookie.js +127 -0
- package/src/runtime/server/data/index.js +136 -0
- package/src/runtime/server/endpoint.js +90 -0
- package/src/runtime/server/index.js +340 -0
- package/src/runtime/server/page/actions.js +243 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +250 -0
- package/src/runtime/server/page/fetch.js +301 -0
- package/src/runtime/server/page/index.js +304 -0
- package/src/runtime/server/page/load_data.js +124 -0
- package/src/runtime/server/page/render.js +342 -0
- package/src/runtime/server/page/respond_with_error.js +104 -0
- package/src/runtime/server/page/serialize_data.js +87 -0
- package/src/runtime/server/page/types.d.ts +41 -0
- package/src/runtime/server/utils.js +179 -0
- package/src/utils/array.js +9 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +46 -0
- package/src/utils/filesystem.js +137 -0
- package/src/utils/functions.js +16 -0
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +117 -0
- package/src/utils/unit_test.js +11 -0
- package/src/utils/url.js +142 -0
- package/svelte-kit.js +2 -0
- package/types/ambient.d.ts +426 -0
- package/types/index.d.ts +444 -0
- package/types/internal.d.ts +378 -0
- package/types/private.d.ts +224 -0
- package/CHANGELOG.md +0 -177
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/navigation.js +0 -47
- package/assets/runtime/app/navigation.js.map +0 -1
- package/assets/runtime/app/stores.js +0 -78
- package/assets/runtime/app/stores.js.map +0 -1
- package/assets/runtime/internal/singletons.js +0 -10
- package/assets/runtime/internal/singletons.js.map +0 -1
- package/assets/runtime/internal/start.js +0 -517
- package/assets/runtime/internal/start.js.map +0 -1
- package/dist/api.js +0 -40
- package/dist/api.js.map +0 -1
- package/dist/cli.js +0 -128
- package/dist/cli.js.map +0 -1
- package/dist/create_app.js +0 -550
- package/dist/create_app.js.map +0 -1
- package/dist/index.js +0 -8331
- package/dist/index.js.map +0 -1
- package/dist/index2.js +0 -509
- package/dist/index2.js.map +0 -1
- package/dist/index3.js +0 -63
- package/dist/index3.js.map +0 -1
- package/dist/index4.js +0 -466
- package/dist/index4.js.map +0 -1
- package/dist/index5.js +0 -276
- package/dist/index5.js.map +0 -1
- package/dist/package.js +0 -235
- package/dist/package.js.map +0 -1
- package/dist/renderer.js +0 -2398
- package/dist/renderer.js.map +0 -1
- package/dist/standard.js +0 -101
- package/dist/standard.js.map +0 -1
- package/dist/utils.js +0 -58
- package/dist/utils.js.map +0 -1
- package/svelte-kit +0 -3
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
2
|
+
import { dirname, join } from 'path';
|
|
3
|
+
import { pathToFileURL, URL } from 'url';
|
|
4
|
+
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
|
|
5
|
+
import { installPolyfills } from '../../exports/node/polyfills.js';
|
|
6
|
+
import { is_root_relative, resolve } from '../../utils/url.js';
|
|
7
|
+
import { queue } from './queue.js';
|
|
8
|
+
import { crawl } from './crawl.js';
|
|
9
|
+
import { escape_html_attr } from '../../utils/escape.js';
|
|
10
|
+
import { logger } from '../utils.js';
|
|
11
|
+
import { load_config } from '../config/index.js';
|
|
12
|
+
import { affects_path } from '../../utils/routing.js';
|
|
13
|
+
import { get_option } from '../../runtime/server/utils.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
|
|
17
|
+
* @typedef {import('types').Logger} Logger
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const [, , client_out_dir, results_path, verbose, env] = process.argv;
|
|
21
|
+
|
|
22
|
+
prerender();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {Parameters<PrerenderErrorHandler>[0]} details
|
|
26
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
27
|
+
*/
|
|
28
|
+
function format_error({ status, path, referrer, referenceType }, config) {
|
|
29
|
+
const message =
|
|
30
|
+
status === 404 && !path.startsWith(config.paths.base)
|
|
31
|
+
? `${path} does not begin with \`base\`, which is configured in \`paths.base\` and can be imported from \`$app/paths\``
|
|
32
|
+
: path;
|
|
33
|
+
|
|
34
|
+
return `${status} ${message}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {Logger} log
|
|
39
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
40
|
+
* @returns {PrerenderErrorHandler}
|
|
41
|
+
*/
|
|
42
|
+
function normalise_error_handler(log, config) {
|
|
43
|
+
switch (config.prerender.onError) {
|
|
44
|
+
case 'continue':
|
|
45
|
+
return (details) => {
|
|
46
|
+
log.error(format_error(details, config));
|
|
47
|
+
};
|
|
48
|
+
case 'fail':
|
|
49
|
+
return (details) => {
|
|
50
|
+
throw new Error(format_error(details, config));
|
|
51
|
+
};
|
|
52
|
+
default:
|
|
53
|
+
return config.prerender.onError;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const OK = 2;
|
|
58
|
+
const REDIRECT = 3;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {{
|
|
62
|
+
* prerendered: import('types').Prerendered;
|
|
63
|
+
* prerender_map: import('types').PrerenderMap;
|
|
64
|
+
* }} data
|
|
65
|
+
*/
|
|
66
|
+
const output_and_exit = (data) => {
|
|
67
|
+
writeFileSync(
|
|
68
|
+
results_path,
|
|
69
|
+
JSON.stringify(data, (_key, value) =>
|
|
70
|
+
value instanceof Map ? Array.from(value.entries()) : value
|
|
71
|
+
)
|
|
72
|
+
);
|
|
73
|
+
process.exit(0);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export async function prerender() {
|
|
77
|
+
/** @type {import('types').Prerendered} */
|
|
78
|
+
const prerendered = {
|
|
79
|
+
pages: new Map(),
|
|
80
|
+
assets: new Map(),
|
|
81
|
+
redirects: new Map(),
|
|
82
|
+
paths: []
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** @type {import('types').PrerenderMap} */
|
|
86
|
+
const prerender_map = new Map();
|
|
87
|
+
|
|
88
|
+
/** @type {import('types').ValidatedKitConfig} */
|
|
89
|
+
const config = (await load_config()).kit;
|
|
90
|
+
|
|
91
|
+
if (!config.prerender.enabled) {
|
|
92
|
+
output_and_exit({ prerendered, prerender_map });
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @type {import('types').Logger} */
|
|
97
|
+
const log = logger({
|
|
98
|
+
verbose: verbose === 'true'
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
installPolyfills();
|
|
102
|
+
const { fetch } = globalThis;
|
|
103
|
+
globalThis.fetch = async (info, init) => {
|
|
104
|
+
/** @type {string} */
|
|
105
|
+
let url;
|
|
106
|
+
|
|
107
|
+
/** @type {RequestInit} */
|
|
108
|
+
let opts = {};
|
|
109
|
+
|
|
110
|
+
if (info instanceof Request) {
|
|
111
|
+
url = info.url;
|
|
112
|
+
|
|
113
|
+
opts = {
|
|
114
|
+
method: info.method,
|
|
115
|
+
headers: info.headers,
|
|
116
|
+
body: info.body,
|
|
117
|
+
mode: info.mode,
|
|
118
|
+
credentials: info.credentials,
|
|
119
|
+
cache: info.cache,
|
|
120
|
+
redirect: info.redirect,
|
|
121
|
+
referrer: info.referrer,
|
|
122
|
+
integrity: info.integrity
|
|
123
|
+
};
|
|
124
|
+
} else {
|
|
125
|
+
url = info.toString();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (url.startsWith(config.prerender.origin + '/')) {
|
|
129
|
+
const request = new Request(url, opts);
|
|
130
|
+
const response = await server.respond(request, {
|
|
131
|
+
getClientAddress,
|
|
132
|
+
prerendering: {
|
|
133
|
+
dependencies: new Map()
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const decoded = new URL(url).pathname;
|
|
138
|
+
|
|
139
|
+
save(
|
|
140
|
+
'dependencies',
|
|
141
|
+
response,
|
|
142
|
+
Buffer.from(await response.clone().arrayBuffer()),
|
|
143
|
+
decoded,
|
|
144
|
+
encodeURI(decoded),
|
|
145
|
+
null,
|
|
146
|
+
'fetched'
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
return response;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return fetch(info, init);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const server_root = join(config.outDir, 'output');
|
|
156
|
+
|
|
157
|
+
/** @type {import('types').ServerModule} */
|
|
158
|
+
const { Server, override } = await import(pathToFileURL(`${server_root}/server/index.js`).href);
|
|
159
|
+
|
|
160
|
+
/** @type {import('types').SSRManifest} */
|
|
161
|
+
const manifest = (await import(pathToFileURL(`${server_root}/server/manifest.js`).href)).manifest;
|
|
162
|
+
|
|
163
|
+
override({
|
|
164
|
+
paths: config.paths,
|
|
165
|
+
prerendering: true,
|
|
166
|
+
read: (file) => readFileSync(join(config.files.assets, file))
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const server = new Server(manifest);
|
|
170
|
+
await server.init({ env: JSON.parse(env) });
|
|
171
|
+
|
|
172
|
+
const error = normalise_error_handler(log, config);
|
|
173
|
+
|
|
174
|
+
const q = queue(config.prerender.concurrency);
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param {string} path
|
|
178
|
+
* @param {boolean} is_html
|
|
179
|
+
*/
|
|
180
|
+
function output_filename(path, is_html) {
|
|
181
|
+
const file = path.slice(config.paths.base.length + 1) || 'index.html';
|
|
182
|
+
|
|
183
|
+
if (is_html && !file.endsWith('.html')) {
|
|
184
|
+
return file + (file.endsWith('/') ? 'index.html' : '.html');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return file;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const files = new Set(walk(client_out_dir).map(posixify));
|
|
191
|
+
const seen = new Set();
|
|
192
|
+
const written = new Set();
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {string | null} referrer
|
|
196
|
+
* @param {string} decoded
|
|
197
|
+
* @param {string} [encoded]
|
|
198
|
+
*/
|
|
199
|
+
function enqueue(referrer, decoded, encoded) {
|
|
200
|
+
if (seen.has(decoded)) return;
|
|
201
|
+
seen.add(decoded);
|
|
202
|
+
|
|
203
|
+
const file = decoded.slice(config.paths.base.length + 1);
|
|
204
|
+
if (files.has(file)) return;
|
|
205
|
+
|
|
206
|
+
return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* @param {string} decoded
|
|
211
|
+
* @param {string} encoded
|
|
212
|
+
* @param {string?} referrer
|
|
213
|
+
*/
|
|
214
|
+
async function visit(decoded, encoded, referrer) {
|
|
215
|
+
if (!decoded.startsWith(config.paths.base)) {
|
|
216
|
+
error({ status: 404, path: decoded, referrer, referenceType: 'linked' });
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** @type {Map<string, import('types').PrerenderDependency>} */
|
|
221
|
+
const dependencies = new Map();
|
|
222
|
+
|
|
223
|
+
const response = await server.respond(new Request(config.prerender.origin + encoded), {
|
|
224
|
+
getClientAddress,
|
|
225
|
+
prerendering: {
|
|
226
|
+
dependencies
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
231
|
+
|
|
232
|
+
save('pages', response, body, decoded, encoded, referrer, 'linked');
|
|
233
|
+
|
|
234
|
+
for (const [dependency_path, result] of dependencies) {
|
|
235
|
+
// this seems circuitous, but using new URL allows us to not care
|
|
236
|
+
// whether dependency_path is encoded or not
|
|
237
|
+
const encoded_dependency_path = new URL(dependency_path, 'http://localhost').pathname;
|
|
238
|
+
const decoded_dependency_path = decodeURI(encoded_dependency_path);
|
|
239
|
+
|
|
240
|
+
const headers = Object.fromEntries(result.response.headers);
|
|
241
|
+
|
|
242
|
+
const prerender = headers['x-sveltekit-prerender'];
|
|
243
|
+
if (prerender) {
|
|
244
|
+
const route_id = headers['x-sveltekit-routeid'];
|
|
245
|
+
const existing_value = prerender_map.get(route_id);
|
|
246
|
+
if (existing_value !== 'auto') {
|
|
247
|
+
prerender_map.set(route_id, prerender === 'true' ? true : 'auto');
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const body = result.body ?? new Uint8Array(await result.response.arrayBuffer());
|
|
252
|
+
save(
|
|
253
|
+
'dependencies',
|
|
254
|
+
result.response,
|
|
255
|
+
body,
|
|
256
|
+
decoded_dependency_path,
|
|
257
|
+
encoded_dependency_path,
|
|
258
|
+
decoded,
|
|
259
|
+
'fetched'
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// avoid triggering `filterSerializeResponseHeaders` guard
|
|
264
|
+
const headers = Object.fromEntries(response.headers);
|
|
265
|
+
|
|
266
|
+
if (config.prerender.crawl && headers['content-type'] === 'text/html') {
|
|
267
|
+
for (const href of crawl(body.toString())) {
|
|
268
|
+
if (href.startsWith('data:') || href.startsWith('#')) continue;
|
|
269
|
+
|
|
270
|
+
const resolved = resolve(encoded, href);
|
|
271
|
+
if (!is_root_relative(resolved)) continue;
|
|
272
|
+
|
|
273
|
+
const { pathname, search } = new URL(resolved, 'http://localhost');
|
|
274
|
+
|
|
275
|
+
if (search) {
|
|
276
|
+
// TODO warn that query strings have no effect on statically-exported pages
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
enqueue(decoded, decodeURI(pathname), pathname);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* @param {'pages' | 'dependencies'} category
|
|
286
|
+
* @param {Response} response
|
|
287
|
+
* @param {string | Uint8Array} body
|
|
288
|
+
* @param {string} decoded
|
|
289
|
+
* @param {string} encoded
|
|
290
|
+
* @param {string | null} referrer
|
|
291
|
+
* @param {'linked' | 'fetched'} referenceType
|
|
292
|
+
*/
|
|
293
|
+
function save(category, response, body, decoded, encoded, referrer, referenceType) {
|
|
294
|
+
const response_type = Math.floor(response.status / 100);
|
|
295
|
+
const headers = Object.fromEntries(response.headers);
|
|
296
|
+
|
|
297
|
+
const type = headers['content-type'];
|
|
298
|
+
const is_html = response_type === REDIRECT || type === 'text/html';
|
|
299
|
+
|
|
300
|
+
const file = output_filename(decoded, is_html);
|
|
301
|
+
const dest = `${config.outDir}/output/prerendered/${category}/${file}`;
|
|
302
|
+
|
|
303
|
+
if (written.has(file)) return;
|
|
304
|
+
|
|
305
|
+
if (response_type === REDIRECT) {
|
|
306
|
+
const location = headers['location'];
|
|
307
|
+
|
|
308
|
+
if (location) {
|
|
309
|
+
const resolved = resolve(encoded, location);
|
|
310
|
+
if (is_root_relative(resolved)) {
|
|
311
|
+
enqueue(decoded, decodeURI(resolved), resolved);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (!headers['x-sveltekit-normalize']) {
|
|
315
|
+
mkdirp(dirname(dest));
|
|
316
|
+
|
|
317
|
+
log.warn(`${response.status} ${decoded} -> ${location}`);
|
|
318
|
+
|
|
319
|
+
writeFileSync(
|
|
320
|
+
dest,
|
|
321
|
+
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
written.add(file);
|
|
325
|
+
|
|
326
|
+
if (!prerendered.redirects.has(decoded)) {
|
|
327
|
+
prerendered.redirects.set(decoded, {
|
|
328
|
+
status: response.status,
|
|
329
|
+
location: resolved
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
prerendered.paths.push(decoded);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
log.warn(`location header missing on redirect received from ${decoded}`);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (response.status === 200) {
|
|
343
|
+
mkdirp(dirname(dest));
|
|
344
|
+
|
|
345
|
+
log.info(`${response.status} ${decoded}`);
|
|
346
|
+
writeFileSync(dest, body);
|
|
347
|
+
written.add(file);
|
|
348
|
+
|
|
349
|
+
if (is_html) {
|
|
350
|
+
prerendered.pages.set(decoded, {
|
|
351
|
+
file
|
|
352
|
+
});
|
|
353
|
+
} else {
|
|
354
|
+
prerendered.assets.set(decoded, {
|
|
355
|
+
type
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
prerendered.paths.push(decoded);
|
|
360
|
+
} else if (response_type !== OK) {
|
|
361
|
+
error({ status: response.status, path: decoded, referrer, referenceType });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
for (const route of manifest._.routes) {
|
|
366
|
+
try {
|
|
367
|
+
if (route.endpoint) {
|
|
368
|
+
const mod = await route.endpoint();
|
|
369
|
+
if (mod.prerender !== undefined) {
|
|
370
|
+
if (mod.prerender && (mod.POST || mod.PATCH || mod.PUT || mod.DELETE)) {
|
|
371
|
+
throw new Error(
|
|
372
|
+
`Cannot prerender a +server file with POST, PATCH, PUT, or DELETE (${route.id})`
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
prerender_map.set(route.id, mod.prerender);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (route.page) {
|
|
381
|
+
const nodes = await Promise.all(
|
|
382
|
+
[...route.page.layouts, route.page.leaf].map((n) => {
|
|
383
|
+
if (n !== undefined) return manifest._.nodes[n]();
|
|
384
|
+
})
|
|
385
|
+
);
|
|
386
|
+
const prerender = get_option(nodes, 'prerender') ?? false;
|
|
387
|
+
|
|
388
|
+
prerender_map.set(route.id, prerender);
|
|
389
|
+
}
|
|
390
|
+
} catch (e) {
|
|
391
|
+
// We failed to import the module, which indicates it can't be prerendered
|
|
392
|
+
// TODO should we catch these? It's almost certainly a bug in the app
|
|
393
|
+
console.error(e);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
for (const entry of config.prerender.entries) {
|
|
398
|
+
if (entry === '*') {
|
|
399
|
+
for (const [id, prerender] of prerender_map) {
|
|
400
|
+
if (prerender) {
|
|
401
|
+
if (id.includes('[')) continue;
|
|
402
|
+
const path = `/${id.split('/').filter(affects_path).join('/')}`;
|
|
403
|
+
enqueue(null, config.paths.base + path);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
enqueue(null, config.paths.base + entry);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
await q.done();
|
|
412
|
+
|
|
413
|
+
const rendered = await server.respond(new Request(config.prerender.origin + '/[fallback]'), {
|
|
414
|
+
getClientAddress,
|
|
415
|
+
prerendering: {
|
|
416
|
+
fallback: true,
|
|
417
|
+
dependencies: new Map()
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
const file = `${config.outDir}/output/prerendered/fallback.html`;
|
|
422
|
+
mkdirp(dirname(file));
|
|
423
|
+
writeFileSync(file, await rendered.text());
|
|
424
|
+
|
|
425
|
+
output_and_exit({ prerendered, prerender_map });
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** @return {string} */
|
|
429
|
+
function getClientAddress() {
|
|
430
|
+
throw new Error('Cannot read clientAddress during prerendering');
|
|
431
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {{
|
|
3
|
+
* fn: () => Promise<any>,
|
|
4
|
+
* fulfil: (value: any) => void,
|
|
5
|
+
* reject: (error: Error) => void
|
|
6
|
+
* }} Task
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** @param {number} concurrency */
|
|
10
|
+
export function queue(concurrency) {
|
|
11
|
+
/** @type {Task[]} */
|
|
12
|
+
const tasks = [];
|
|
13
|
+
|
|
14
|
+
let current = 0;
|
|
15
|
+
|
|
16
|
+
/** @type {(value?: any) => void} */
|
|
17
|
+
let fulfil;
|
|
18
|
+
|
|
19
|
+
/** @type {(error: Error) => void} */
|
|
20
|
+
let reject;
|
|
21
|
+
|
|
22
|
+
let closed = false;
|
|
23
|
+
|
|
24
|
+
const done = new Promise((f, r) => {
|
|
25
|
+
fulfil = f;
|
|
26
|
+
reject = r;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
done.catch(() => {
|
|
30
|
+
// this is necessary in case a catch handler is never added
|
|
31
|
+
// to the done promise by the user
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
function dequeue() {
|
|
35
|
+
if (current < concurrency) {
|
|
36
|
+
const task = tasks.shift();
|
|
37
|
+
|
|
38
|
+
if (task) {
|
|
39
|
+
current += 1;
|
|
40
|
+
const promise = Promise.resolve(task.fn());
|
|
41
|
+
|
|
42
|
+
promise
|
|
43
|
+
.then(task.fulfil, (err) => {
|
|
44
|
+
task.reject(err);
|
|
45
|
+
reject(err);
|
|
46
|
+
})
|
|
47
|
+
.then(() => {
|
|
48
|
+
current -= 1;
|
|
49
|
+
dequeue();
|
|
50
|
+
});
|
|
51
|
+
} else if (current === 0) {
|
|
52
|
+
closed = true;
|
|
53
|
+
fulfil();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
/** @param {() => any} fn */
|
|
60
|
+
add: (fn) => {
|
|
61
|
+
if (closed) throw new Error('Cannot add tasks to a queue that has ended');
|
|
62
|
+
|
|
63
|
+
const promise = new Promise((fulfil, reject) => {
|
|
64
|
+
tasks.push({ fn, fulfil, reject });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
dequeue();
|
|
68
|
+
return promise;
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
done: () => {
|
|
72
|
+
if (current === 0) {
|
|
73
|
+
closed = true;
|
|
74
|
+
fulfil();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return done;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|