@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,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
|
+
}
|
|
@@ -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
|
+
}
|