@sveltejs/kit 1.0.0-next.405 → 1.0.0-next.406
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/package.json +23 -25
- package/{dist → src}/cli.js +19 -18
- package/{dist/chunks/index3.js → src/core/adapt/builder.js} +6 -59
- package/src/core/adapt/index.js +19 -0
- package/src/core/config/index.js +86 -0
- package/{dist/chunks/index.js → src/core/config/options.js} +7 -194
- package/src/core/config/types.d.ts +1 -0
- package/src/core/constants.js +3 -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 +492 -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 +97 -0
- package/src/core/sync/write_ambient.js +87 -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 +88 -0
- package/{dist/chunks → src/core/sync}/write_tsconfig.js +24 -108
- package/src/core/sync/write_types.js +738 -0
- package/src/core/utils.js +58 -0
- package/{dist → src}/hooks.js +1 -3
- package/src/index/index.js +45 -0
- package/src/index/private.js +31 -0
- package/src/node/index.js +145 -0
- package/src/node/polyfills.js +40 -0
- package/src/packaging/index.js +218 -0
- package/src/packaging/types.d.ts +8 -0
- package/src/packaging/typescript.js +150 -0
- package/src/packaging/utils.js +138 -0
- package/{assets → src/runtime}/app/env.js +3 -5
- package/src/runtime/app/navigation.js +22 -0
- package/src/runtime/app/paths.js +1 -0
- package/{assets → src/runtime}/app/stores.js +6 -9
- package/src/runtime/client/ambient.d.ts +17 -0
- package/{assets/client/start.js → src/runtime/client/client.js} +302 -878
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +36 -0
- package/{assets → src/runtime}/client/singletons.js +2 -4
- package/src/runtime/client/start.js +48 -0
- package/src/runtime/client/types.d.ts +106 -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/{assets → src/runtime}/env/dynamic/private.js +0 -0
- package/{assets → src/runtime}/env/dynamic/public.js +0 -0
- package/{assets → src/runtime}/env-private.js +2 -4
- package/{assets → src/runtime}/env-public.js +2 -4
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +16 -0
- package/{assets → src/runtime}/paths.js +3 -5
- package/src/runtime/server/endpoint.js +42 -0
- package/src/runtime/server/index.js +434 -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 +265 -0
- package/src/runtime/server/page/index.js +423 -0
- package/src/runtime/server/page/load_data.js +94 -0
- package/src/runtime/server/page/render.js +357 -0
- package/src/runtime/server/page/respond_with_error.js +105 -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/{dist/chunks → src/utils}/filesystem.js +22 -24
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +107 -0
- package/src/utils/url.js +97 -0
- package/src/vite/build/build_server.js +333 -0
- package/src/vite/build/build_service_worker.js +90 -0
- package/src/vite/build/utils.js +152 -0
- package/src/vite/dev/index.js +565 -0
- package/src/vite/index.js +536 -0
- package/src/vite/preview/index.js +186 -0
- package/src/vite/types.d.ts +3 -0
- package/src/vite/utils.js +335 -0
- package/svelte-kit.js +1 -10
- package/types/ambient.d.ts +5 -12
- package/types/index.d.ts +86 -44
- package/types/internal.d.ts +50 -72
- package/types/private.d.ts +2 -1
- package/assets/app/navigation.js +0 -24
- package/assets/app/paths.js +0 -1
- package/assets/components/error.svelte +0 -29
- package/assets/env.js +0 -8
- package/assets/server/index.js +0 -3589
- package/dist/chunks/error.js +0 -12
- package/dist/chunks/index2.js +0 -15745
- package/dist/chunks/multipart-parser.js +0 -458
- package/dist/chunks/sync.js +0 -1366
- package/dist/chunks/utils.js +0 -66
- package/dist/node/polyfills.js +0 -17928
- package/dist/node.js +0 -348
- package/dist/prerender.js +0 -788
- package/dist/vite.js +0 -2513
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { s } from '../../utils/misc.js';
|
|
2
|
+
import { parse_route_id } from '../../utils/routing.js';
|
|
3
|
+
import { get_mime_lookup } from '../utils.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generates the data used to write the server-side manifest.js file. This data is used in the Vite
|
|
7
|
+
* build process, to power routing, etc.
|
|
8
|
+
* @param {{
|
|
9
|
+
* build_data: import('types').BuildData;
|
|
10
|
+
* relative_path: string;
|
|
11
|
+
* routes: import('types').RouteData[];
|
|
12
|
+
* format?: 'esm' | 'cjs'
|
|
13
|
+
* }} opts
|
|
14
|
+
*/
|
|
15
|
+
export function generate_manifest({ build_data, relative_path, routes, format = 'esm' }) {
|
|
16
|
+
/** @typedef {{ index: number, path: string }} LookupEntry */
|
|
17
|
+
/** @type {Map<import('types').PageNode, LookupEntry>} */
|
|
18
|
+
const bundled_nodes = new Map();
|
|
19
|
+
|
|
20
|
+
build_data.manifest_data.nodes.forEach((node, i) => {
|
|
21
|
+
bundled_nodes.set(node, {
|
|
22
|
+
path: `${relative_path}/nodes/${i}.js`,
|
|
23
|
+
index: i
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/** @type {(path: string) => string} */
|
|
28
|
+
const load =
|
|
29
|
+
format === 'esm'
|
|
30
|
+
? (path) => `import('${path}')`
|
|
31
|
+
: (path) => `Promise.resolve().then(() => require('${path}'))`;
|
|
32
|
+
|
|
33
|
+
/** @type {(path: string) => string} */
|
|
34
|
+
const loader = (path) => `() => ${load(path)}`;
|
|
35
|
+
|
|
36
|
+
const assets = build_data.manifest_data.assets.map((asset) => asset.file);
|
|
37
|
+
if (build_data.service_worker) {
|
|
38
|
+
assets.push(build_data.service_worker);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @param {import('types').PageNode | undefined} id */
|
|
42
|
+
const get_index = (id) => id && /** @type {LookupEntry} */ (bundled_nodes.get(id)).index;
|
|
43
|
+
|
|
44
|
+
const matchers = new Set();
|
|
45
|
+
|
|
46
|
+
// prettier-ignore
|
|
47
|
+
return `{
|
|
48
|
+
appDir: ${s(build_data.app_dir)},
|
|
49
|
+
assets: new Set(${s(assets)}),
|
|
50
|
+
mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
|
|
51
|
+
_: {
|
|
52
|
+
entry: ${s(build_data.client.entry)},
|
|
53
|
+
nodes: [
|
|
54
|
+
${Array.from(bundled_nodes.values()).map(node => loader(node.path)).join(',\n\t\t\t\t')}
|
|
55
|
+
],
|
|
56
|
+
routes: [
|
|
57
|
+
${routes.map(route => {
|
|
58
|
+
const { pattern, names, types } = parse_route_id(route.id);
|
|
59
|
+
|
|
60
|
+
types.forEach(type => {
|
|
61
|
+
if (type) matchers.add(type);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (route.type === 'page') {
|
|
65
|
+
return `{
|
|
66
|
+
type: 'page',
|
|
67
|
+
id: ${s(route.id)},
|
|
68
|
+
pattern: ${pattern},
|
|
69
|
+
names: ${s(names)},
|
|
70
|
+
types: ${s(types)},
|
|
71
|
+
errors: ${s(route.errors.map(get_index))},
|
|
72
|
+
layouts: ${s(route.layouts.map(get_index))},
|
|
73
|
+
leaf: ${s(get_index(route.leaf))}
|
|
74
|
+
}`.replace(/^\t\t/gm, '');
|
|
75
|
+
} else {
|
|
76
|
+
if (!build_data.server.vite_manifest[route.file]) {
|
|
77
|
+
// this is necessary in cases where a .css file snuck in —
|
|
78
|
+
// perhaps it would be better to disallow these (and others?)
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return `{
|
|
83
|
+
type: 'endpoint',
|
|
84
|
+
id: ${s(route.id)},
|
|
85
|
+
pattern: ${pattern},
|
|
86
|
+
names: ${s(names)},
|
|
87
|
+
types: ${s(types)},
|
|
88
|
+
load: ${loader(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)}
|
|
89
|
+
}`.replace(/^\t\t/gm, '');
|
|
90
|
+
}
|
|
91
|
+
}).filter(Boolean).join(',\n\t\t\t\t')}
|
|
92
|
+
],
|
|
93
|
+
matchers: async () => {
|
|
94
|
+
${Array.from(matchers).map(type => `const { match: ${type} } = await ${load(`${relative_path}/entries/matchers/${type}.js`)}`).join('\n\t\t\t\t')}
|
|
95
|
+
return { ${Array.from(matchers).join(', ')} };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}`.replace(/^\t/gm, '');
|
|
99
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
const DOCTYPE = 'DOCTYPE';
|
|
2
|
+
const CDATA_OPEN = '[CDATA[';
|
|
3
|
+
const CDATA_CLOSE = ']]>';
|
|
4
|
+
const COMMENT_OPEN = '--';
|
|
5
|
+
const COMMENT_CLOSE = '-->';
|
|
6
|
+
|
|
7
|
+
const TAG_OPEN = /[a-zA-Z]/;
|
|
8
|
+
const TAG_CHAR = /[a-zA-Z0-9]/;
|
|
9
|
+
const ATTRIBUTE_NAME = /[^\t\n\f />"'=]/;
|
|
10
|
+
|
|
11
|
+
const WHITESPACE = /[\s\n\r]/;
|
|
12
|
+
|
|
13
|
+
/** @param {string} html */
|
|
14
|
+
export function crawl(html) {
|
|
15
|
+
/** @type {string[]} */
|
|
16
|
+
const hrefs = [];
|
|
17
|
+
|
|
18
|
+
let i = 0;
|
|
19
|
+
main: while (i < html.length) {
|
|
20
|
+
const char = html[i];
|
|
21
|
+
|
|
22
|
+
if (char === '<') {
|
|
23
|
+
if (html[i + 1] === '!') {
|
|
24
|
+
i += 2;
|
|
25
|
+
|
|
26
|
+
if (html.slice(i, i + DOCTYPE.length).toUpperCase() === DOCTYPE) {
|
|
27
|
+
i += DOCTYPE.length;
|
|
28
|
+
while (i < html.length) {
|
|
29
|
+
if (html[i++] === '>') {
|
|
30
|
+
continue main;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// skip cdata
|
|
36
|
+
if (html.slice(i, i + CDATA_OPEN.length) === CDATA_OPEN) {
|
|
37
|
+
i += CDATA_OPEN.length;
|
|
38
|
+
while (i < html.length) {
|
|
39
|
+
if (html.slice(i, i + CDATA_CLOSE.length) === CDATA_CLOSE) {
|
|
40
|
+
i += CDATA_CLOSE.length;
|
|
41
|
+
continue main;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
i += 1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// skip comments
|
|
49
|
+
if (html.slice(i, i + COMMENT_OPEN.length) === COMMENT_OPEN) {
|
|
50
|
+
i += COMMENT_OPEN.length;
|
|
51
|
+
while (i < html.length) {
|
|
52
|
+
if (html.slice(i, i + COMMENT_CLOSE.length) === COMMENT_CLOSE) {
|
|
53
|
+
i += COMMENT_CLOSE.length;
|
|
54
|
+
continue main;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
i += 1;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// parse opening tags
|
|
63
|
+
const start = ++i;
|
|
64
|
+
if (TAG_OPEN.test(html[start])) {
|
|
65
|
+
while (i < html.length) {
|
|
66
|
+
if (!TAG_CHAR.test(html[i])) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
i += 1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const tag = html.slice(start, i).toUpperCase();
|
|
74
|
+
|
|
75
|
+
if (tag === 'SCRIPT' || tag === 'STYLE') {
|
|
76
|
+
while (i < html.length) {
|
|
77
|
+
if (
|
|
78
|
+
html[i] === '<' &&
|
|
79
|
+
html[i + 1] === '/' &&
|
|
80
|
+
html.slice(i + 2, i + 2 + tag.length).toUpperCase() === tag
|
|
81
|
+
) {
|
|
82
|
+
continue main;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
i += 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let href = '';
|
|
90
|
+
let rel = '';
|
|
91
|
+
|
|
92
|
+
while (i < html.length) {
|
|
93
|
+
const start = i;
|
|
94
|
+
|
|
95
|
+
const char = html[start];
|
|
96
|
+
if (char === '>') break;
|
|
97
|
+
|
|
98
|
+
if (ATTRIBUTE_NAME.test(char)) {
|
|
99
|
+
i += 1;
|
|
100
|
+
|
|
101
|
+
while (i < html.length) {
|
|
102
|
+
if (!ATTRIBUTE_NAME.test(html[i])) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
i += 1;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const name = html.slice(start, i).toLowerCase();
|
|
110
|
+
|
|
111
|
+
while (WHITESPACE.test(html[i])) i += 1;
|
|
112
|
+
|
|
113
|
+
if (html[i] === '=') {
|
|
114
|
+
i += 1;
|
|
115
|
+
while (WHITESPACE.test(html[i])) i += 1;
|
|
116
|
+
|
|
117
|
+
let value;
|
|
118
|
+
|
|
119
|
+
if (html[i] === "'" || html[i] === '"') {
|
|
120
|
+
const quote = html[i++];
|
|
121
|
+
|
|
122
|
+
const start = i;
|
|
123
|
+
let escaped = false;
|
|
124
|
+
|
|
125
|
+
while (i < html.length) {
|
|
126
|
+
if (!escaped) {
|
|
127
|
+
const char = html[i];
|
|
128
|
+
|
|
129
|
+
if (html[i] === quote) {
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (char === '\\') {
|
|
134
|
+
escaped = true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
i += 1;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
value = html.slice(start, i);
|
|
142
|
+
} else {
|
|
143
|
+
const start = i;
|
|
144
|
+
while (html[i] !== '>' && !WHITESPACE.test(html[i])) i += 1;
|
|
145
|
+
value = html.slice(start, i);
|
|
146
|
+
|
|
147
|
+
i -= 1;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (name === 'href') {
|
|
151
|
+
href = value;
|
|
152
|
+
} else if (name === 'rel') {
|
|
153
|
+
rel = value;
|
|
154
|
+
} else if (name === 'src') {
|
|
155
|
+
hrefs.push(value);
|
|
156
|
+
} else if (name === 'srcset') {
|
|
157
|
+
const candidates = [];
|
|
158
|
+
let insideURL = true;
|
|
159
|
+
value = value.trim();
|
|
160
|
+
for (let i = 0; i < value.length; i++) {
|
|
161
|
+
if (value[i] === ',' && (!insideURL || (insideURL && value[i + 1] === ' '))) {
|
|
162
|
+
candidates.push(value.slice(0, i));
|
|
163
|
+
value = value.substring(i + 1).trim();
|
|
164
|
+
i = 0;
|
|
165
|
+
insideURL = true;
|
|
166
|
+
} else if (value[i] === ' ') {
|
|
167
|
+
insideURL = false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
candidates.push(value);
|
|
171
|
+
for (const candidate of candidates) {
|
|
172
|
+
const src = candidate.split(WHITESPACE)[0];
|
|
173
|
+
hrefs.push(src);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
} else {
|
|
177
|
+
i -= 1;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
i += 1;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (href && !/\bexternal\b/i.test(rel)) {
|
|
185
|
+
hrefs.push(href);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
i += 1;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return hrefs;
|
|
194
|
+
}
|
|
@@ -0,0 +1,378 @@
|
|
|
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 '../../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
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
|
|
15
|
+
* @typedef {import('types').Logger} Logger
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const [, , client_out_dir, results_path, manifest_path, verbose] = process.argv;
|
|
19
|
+
|
|
20
|
+
prerender();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {Parameters<PrerenderErrorHandler>[0]} details
|
|
24
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
25
|
+
*/
|
|
26
|
+
function format_error({ status, path, referrer, referenceType }, config) {
|
|
27
|
+
const message =
|
|
28
|
+
status === 404 && !path.startsWith(config.paths.base)
|
|
29
|
+
? `${path} does not begin with \`base\`, which is configured in \`paths.base\` and can be imported from \`$app/paths\``
|
|
30
|
+
: path;
|
|
31
|
+
|
|
32
|
+
return `${status} ${message}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {Logger} log
|
|
37
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
38
|
+
* @returns {PrerenderErrorHandler}
|
|
39
|
+
*/
|
|
40
|
+
function normalise_error_handler(log, config) {
|
|
41
|
+
switch (config.prerender.onError) {
|
|
42
|
+
case 'continue':
|
|
43
|
+
return (details) => {
|
|
44
|
+
log.error(format_error(details, config));
|
|
45
|
+
};
|
|
46
|
+
case 'fail':
|
|
47
|
+
return (details) => {
|
|
48
|
+
throw new Error(format_error(details, config));
|
|
49
|
+
};
|
|
50
|
+
default:
|
|
51
|
+
return config.prerender.onError;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const OK = 2;
|
|
56
|
+
const REDIRECT = 3;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {import('types').Prerendered} prerendered
|
|
60
|
+
*/
|
|
61
|
+
const output_and_exit = (prerendered) => {
|
|
62
|
+
writeFileSync(
|
|
63
|
+
results_path,
|
|
64
|
+
JSON.stringify(prerendered, (_key, value) =>
|
|
65
|
+
value instanceof Map ? Array.from(value.entries()) : value
|
|
66
|
+
)
|
|
67
|
+
);
|
|
68
|
+
process.exit(0);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export async function prerender() {
|
|
72
|
+
/** @type {import('types').Prerendered} */
|
|
73
|
+
const prerendered = {
|
|
74
|
+
pages: new Map(),
|
|
75
|
+
assets: new Map(),
|
|
76
|
+
redirects: new Map(),
|
|
77
|
+
paths: []
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** @type {import('types').ValidatedKitConfig} */
|
|
81
|
+
const config = (await load_config()).kit;
|
|
82
|
+
|
|
83
|
+
if (!config.prerender.enabled) {
|
|
84
|
+
output_and_exit(prerendered);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @type {import('types').Logger} */
|
|
89
|
+
const log = logger({
|
|
90
|
+
verbose: verbose === 'true'
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
installPolyfills();
|
|
94
|
+
const { fetch } = globalThis;
|
|
95
|
+
globalThis.fetch = async (info, init) => {
|
|
96
|
+
/** @type {string} */
|
|
97
|
+
let url;
|
|
98
|
+
|
|
99
|
+
/** @type {RequestInit} */
|
|
100
|
+
let opts = {};
|
|
101
|
+
|
|
102
|
+
if (info instanceof Request) {
|
|
103
|
+
url = info.url;
|
|
104
|
+
|
|
105
|
+
opts = {
|
|
106
|
+
method: info.method,
|
|
107
|
+
headers: info.headers,
|
|
108
|
+
body: info.body,
|
|
109
|
+
mode: info.mode,
|
|
110
|
+
credentials: info.credentials,
|
|
111
|
+
cache: info.cache,
|
|
112
|
+
redirect: info.redirect,
|
|
113
|
+
referrer: info.referrer,
|
|
114
|
+
integrity: info.integrity
|
|
115
|
+
};
|
|
116
|
+
} else {
|
|
117
|
+
url = info.toString();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (url.startsWith(config.prerender.origin + '/')) {
|
|
121
|
+
const request = new Request(url, opts);
|
|
122
|
+
const response = await server.respond(request, {
|
|
123
|
+
getClientAddress,
|
|
124
|
+
prerendering: {
|
|
125
|
+
dependencies: new Map()
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const decoded = new URL(url).pathname;
|
|
130
|
+
|
|
131
|
+
save(
|
|
132
|
+
'dependencies',
|
|
133
|
+
response,
|
|
134
|
+
Buffer.from(await response.clone().arrayBuffer()),
|
|
135
|
+
decoded,
|
|
136
|
+
encodeURI(decoded),
|
|
137
|
+
null,
|
|
138
|
+
'fetched'
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
return response;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return fetch(info, init);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const server_root = join(config.outDir, 'output');
|
|
148
|
+
|
|
149
|
+
/** @type {import('types').ServerModule} */
|
|
150
|
+
const { Server, override } = await import(pathToFileURL(`${server_root}/server/index.js`).href);
|
|
151
|
+
|
|
152
|
+
/** @type {import('types').SSRManifest} */
|
|
153
|
+
const manifest = (await import(pathToFileURL(`${server_root}/server/manifest.js`).href)).manifest;
|
|
154
|
+
|
|
155
|
+
override({
|
|
156
|
+
paths: config.paths,
|
|
157
|
+
prerendering: true,
|
|
158
|
+
read: (file) => readFileSync(join(config.files.assets, file))
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const server = new Server(manifest);
|
|
162
|
+
|
|
163
|
+
const error = normalise_error_handler(log, config);
|
|
164
|
+
|
|
165
|
+
const q = queue(config.prerender.concurrency);
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @param {string} path
|
|
169
|
+
* @param {boolean} is_html
|
|
170
|
+
*/
|
|
171
|
+
function output_filename(path, is_html) {
|
|
172
|
+
const file = path.slice(config.paths.base.length + 1) || 'index.html';
|
|
173
|
+
|
|
174
|
+
if (is_html && !file.endsWith('.html')) {
|
|
175
|
+
return file + (file.endsWith('/') ? 'index.html' : '.html');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return file;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const files = new Set(walk(client_out_dir).map(posixify));
|
|
182
|
+
const seen = new Set();
|
|
183
|
+
const written = new Set();
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {string | null} referrer
|
|
187
|
+
* @param {string} decoded
|
|
188
|
+
* @param {string} [encoded]
|
|
189
|
+
*/
|
|
190
|
+
function enqueue(referrer, decoded, encoded) {
|
|
191
|
+
if (seen.has(decoded)) return;
|
|
192
|
+
seen.add(decoded);
|
|
193
|
+
|
|
194
|
+
const file = decoded.slice(config.paths.base.length + 1);
|
|
195
|
+
if (files.has(file)) return;
|
|
196
|
+
|
|
197
|
+
return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @param {string} decoded
|
|
202
|
+
* @param {string} encoded
|
|
203
|
+
* @param {string?} referrer
|
|
204
|
+
*/
|
|
205
|
+
async function visit(decoded, encoded, referrer) {
|
|
206
|
+
if (!decoded.startsWith(config.paths.base)) {
|
|
207
|
+
error({ status: 404, path: decoded, referrer, referenceType: 'linked' });
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** @type {Map<string, import('types').PrerenderDependency>} */
|
|
212
|
+
const dependencies = new Map();
|
|
213
|
+
|
|
214
|
+
const response = await server.respond(new Request(config.prerender.origin + encoded), {
|
|
215
|
+
getClientAddress,
|
|
216
|
+
prerendering: {
|
|
217
|
+
dependencies
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
222
|
+
|
|
223
|
+
save('pages', response, body, decoded, encoded, referrer, 'linked');
|
|
224
|
+
|
|
225
|
+
for (const [dependency_path, result] of dependencies) {
|
|
226
|
+
// this seems circuitous, but using new URL allows us to not care
|
|
227
|
+
// whether dependency_path is encoded or not
|
|
228
|
+
const encoded_dependency_path = new URL(dependency_path, 'http://localhost').pathname;
|
|
229
|
+
const decoded_dependency_path = decodeURI(encoded_dependency_path);
|
|
230
|
+
|
|
231
|
+
const body = result.body ?? new Uint8Array(await result.response.arrayBuffer());
|
|
232
|
+
save(
|
|
233
|
+
'dependencies',
|
|
234
|
+
result.response,
|
|
235
|
+
body,
|
|
236
|
+
decoded_dependency_path,
|
|
237
|
+
encoded_dependency_path,
|
|
238
|
+
decoded,
|
|
239
|
+
'fetched'
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (config.prerender.crawl && response.headers.get('content-type') === 'text/html') {
|
|
244
|
+
for (const href of crawl(body.toString())) {
|
|
245
|
+
if (href.startsWith('data:') || href.startsWith('#')) continue;
|
|
246
|
+
|
|
247
|
+
const resolved = resolve(encoded, href);
|
|
248
|
+
if (!is_root_relative(resolved)) continue;
|
|
249
|
+
|
|
250
|
+
const { pathname, search } = new URL(resolved, 'http://localhost');
|
|
251
|
+
|
|
252
|
+
if (search) {
|
|
253
|
+
// TODO warn that query strings have no effect on statically-exported pages
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
enqueue(decoded, decodeURI(pathname), pathname);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* @param {'pages' | 'dependencies'} category
|
|
263
|
+
* @param {Response} response
|
|
264
|
+
* @param {string | Uint8Array} body
|
|
265
|
+
* @param {string} decoded
|
|
266
|
+
* @param {string} encoded
|
|
267
|
+
* @param {string | null} referrer
|
|
268
|
+
* @param {'linked' | 'fetched'} referenceType
|
|
269
|
+
*/
|
|
270
|
+
function save(category, response, body, decoded, encoded, referrer, referenceType) {
|
|
271
|
+
const response_type = Math.floor(response.status / 100);
|
|
272
|
+
const type = /** @type {string} */ (response.headers.get('content-type'));
|
|
273
|
+
const is_html = response_type === REDIRECT || type === 'text/html';
|
|
274
|
+
|
|
275
|
+
const file = output_filename(decoded, is_html);
|
|
276
|
+
const dest = `${config.outDir}/output/prerendered/${category}/${file}`;
|
|
277
|
+
|
|
278
|
+
if (written.has(file)) return;
|
|
279
|
+
|
|
280
|
+
if (response_type === REDIRECT) {
|
|
281
|
+
const location = response.headers.get('location');
|
|
282
|
+
|
|
283
|
+
if (location) {
|
|
284
|
+
const resolved = resolve(encoded, location);
|
|
285
|
+
if (is_root_relative(resolved)) {
|
|
286
|
+
enqueue(decoded, decodeURI(resolved), resolved);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (!response.headers.get('x-sveltekit-normalize')) {
|
|
290
|
+
mkdirp(dirname(dest));
|
|
291
|
+
|
|
292
|
+
log.warn(`${response.status} ${decoded} -> ${location}`);
|
|
293
|
+
|
|
294
|
+
writeFileSync(
|
|
295
|
+
dest,
|
|
296
|
+
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
written.add(file);
|
|
300
|
+
|
|
301
|
+
if (!prerendered.redirects.has(decoded)) {
|
|
302
|
+
prerendered.redirects.set(decoded, {
|
|
303
|
+
status: response.status,
|
|
304
|
+
location: resolved
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
prerendered.paths.push(decoded);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
log.warn(`location header missing on redirect received from ${decoded}`);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (response.status === 200) {
|
|
318
|
+
mkdirp(dirname(dest));
|
|
319
|
+
|
|
320
|
+
log.info(`${response.status} ${decoded}`);
|
|
321
|
+
writeFileSync(dest, body);
|
|
322
|
+
written.add(file);
|
|
323
|
+
|
|
324
|
+
if (is_html) {
|
|
325
|
+
prerendered.pages.set(decoded, {
|
|
326
|
+
file
|
|
327
|
+
});
|
|
328
|
+
} else {
|
|
329
|
+
prerendered.assets.set(decoded, {
|
|
330
|
+
type
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
prerendered.paths.push(decoded);
|
|
335
|
+
} else if (response_type !== OK) {
|
|
336
|
+
error({ status: response.status, path: decoded, referrer, referenceType });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (config.prerender.enabled) {
|
|
341
|
+
for (const entry of config.prerender.entries) {
|
|
342
|
+
if (entry === '*') {
|
|
343
|
+
/** @type {import('types').ManifestData} */
|
|
344
|
+
const { routes } = (await import(pathToFileURL(manifest_path).href)).manifest._;
|
|
345
|
+
const entries = routes
|
|
346
|
+
.map((route) => (route.type === 'page' && !route.id.includes('[') ? `/${route.id}` : ''))
|
|
347
|
+
.filter(Boolean);
|
|
348
|
+
|
|
349
|
+
for (const entry of entries) {
|
|
350
|
+
enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these?
|
|
351
|
+
}
|
|
352
|
+
} else {
|
|
353
|
+
enqueue(null, config.paths.base + entry);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
await q.done();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const rendered = await server.respond(new Request(config.prerender.origin + '/[fallback]'), {
|
|
361
|
+
getClientAddress,
|
|
362
|
+
prerendering: {
|
|
363
|
+
fallback: true,
|
|
364
|
+
dependencies: new Map()
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
const file = `${config.outDir}/output/prerendered/fallback.html`;
|
|
369
|
+
mkdirp(dirname(file));
|
|
370
|
+
writeFileSync(file, await rendered.text());
|
|
371
|
+
|
|
372
|
+
output_and_exit(prerendered);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** @return {string} */
|
|
376
|
+
function getClientAddress() {
|
|
377
|
+
throw new Error('Cannot read clientAddress during prerendering');
|
|
378
|
+
}
|