@sveltejs/kit 1.0.0-next.44 → 1.0.0-next.440
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 -63
- package/src/cli.js +112 -0
- package/src/core/adapt/builder.js +223 -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 +78 -0
- package/src/core/prerender/crawl.js +194 -0
- package/src/core/prerender/prerender.js +380 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +450 -0
- package/src/core/sync/create_manifest_data/types.d.ts +37 -0
- package/src/core/sync/sync.js +59 -0
- package/src/core/sync/utils.js +33 -0
- package/src/core/sync/write_ambient.js +27 -0
- package/src/core/sync/write_client_manifest.js +92 -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 +577 -0
- package/src/core/sync/write_types/test/layout/+layout.js +5 -0
- package/src/core/sync/write_types/test/layout/+layout.server.js +5 -0
- package/src/core/sync/write_types/test/layout/+layout.svelte +0 -0
- package/src/core/sync/write_types/test/layout/+page.js +5 -0
- package/src/core/sync/write_types/test/layout/+page.server.js +5 -0
- package/src/core/sync/write_types/test/layout/+page.svelte +0 -0
- package/src/core/sync/write_types/test/layout/_expected/$types.d.ts +67 -0
- package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.server.js +5 -0
- package/src/core/sync/write_types/test/layout-advanced/(main)/+layout.svelte +0 -0
- package/src/core/sync/write_types/test/layout-advanced/(main)/+page.js +5 -0
- package/src/core/sync/write_types/test/layout-advanced/(main)/+page@.svelte +0 -0
- package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.js +5 -0
- package/src/core/sync/write_types/test/layout-advanced/(main)/sub/+page.svelte +0 -0
- package/src/core/sync/write_types/test/layout-advanced/+layout.js +5 -0
- package/src/core/sync/write_types/test/layout-advanced/+layout.svelte +0 -0
- package/src/core/sync/write_types/test/layout-advanced/_expected/$types.d.ts +32 -0
- package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/$types.d.ts +42 -0
- package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/sub/$types.d.ts +32 -0
- package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.js +5 -0
- package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.server.js +5 -0
- package/src/core/sync/write_types/test/simple-page-server-and-shared/+page.svelte +0 -0
- package/src/core/sync/write_types/test/simple-page-server-and-shared/_expected/$types.d.ts +44 -0
- package/src/core/sync/write_types/test/simple-page-server-only/+page.server.js +5 -0
- package/src/core/sync/write_types/test/simple-page-server-only/+page.svelte +0 -0
- package/src/core/sync/write_types/test/simple-page-server-only/_expected/$types.d.ts +30 -0
- package/src/core/sync/write_types/test/simple-page-shared-only/+page.js +5 -0
- package/src/core/sync/write_types/test/simple-page-shared-only/+page.svelte +0 -0
- package/src/core/sync/write_types/test/simple-page-shared-only/_expected/$types.d.ts +33 -0
- package/src/core/sync/write_types/test/slugs/+layout.js +1 -0
- package/src/core/sync/write_types/test/slugs/+layout.svelte +1 -0
- package/src/core/sync/write_types/test/slugs/[...rest]/+page.js +3 -0
- package/src/core/sync/write_types/test/slugs/[...rest]/+page.svelte +0 -0
- package/src/core/sync/write_types/test/slugs/[slug]/+page.js +3 -0
- package/src/core/sync/write_types/test/slugs/[slug]/+page.svelte +0 -0
- package/src/core/sync/write_types/test/slugs/_expected/$types.d.ts +32 -0
- package/src/core/sync/write_types/test/slugs/_expected/[...rest]/$types.d.ts +29 -0
- package/src/core/sync/write_types/test/slugs/_expected/[slug]/$types.d.ts +29 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.js +1 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/+layout.svelte +1 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/$types.d.ts +30 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/$types.d.ts +32 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[...rest]/$types.d.ts +36 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[slug]/$types.d.ts +17 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.js +1 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/+layout.svelte +1 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.js +3 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[...rest]/+page.svelte +0 -0
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/nested/[slug]/+page@.svelte +0 -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 +18 -0
- package/src/runtime/client/client.js +1362 -0
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +41 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +46 -0
- package/src/runtime/client/types.d.ts +86 -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 +50 -0
- package/src/runtime/server/index.js +489 -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 +408 -0
- package/src/runtime/server/page/load_data.js +168 -0
- package/src/runtime/server/page/render.js +361 -0
- package/src/runtime/server/page/respond_with_error.js +95 -0
- package/src/runtime/server/page/types.d.ts +44 -0
- package/src/runtime/server/utils.js +116 -0
- package/src/utils/array.js +9 -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/functions.js +16 -0
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +146 -0
- package/src/utils/url.js +142 -0
- package/src/vite/build/build_server.js +348 -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 +543 -0
- package/src/vite/index.js +578 -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 +366 -0
- package/types/index.d.ts +345 -0
- package/types/internal.d.ts +374 -0
- package/types/private.d.ts +209 -0
- package/CHANGELOG.md +0 -437
- 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/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 -545
- 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 -2580
|
@@ -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,380 @@
|
|
|
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
|
+
import { compact } from '../../utils/array.js';
|
|
13
|
+
import { get_path } from '../../utils/routing.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
|
|
17
|
+
* @typedef {import('types').Logger} Logger
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const [, , client_out_dir, results_path, manifest_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 {import('types').Prerendered} prerendered
|
|
62
|
+
*/
|
|
63
|
+
const output_and_exit = (prerendered) => {
|
|
64
|
+
writeFileSync(
|
|
65
|
+
results_path,
|
|
66
|
+
JSON.stringify(prerendered, (_key, value) =>
|
|
67
|
+
value instanceof Map ? Array.from(value.entries()) : value
|
|
68
|
+
)
|
|
69
|
+
);
|
|
70
|
+
process.exit(0);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export async function prerender() {
|
|
74
|
+
/** @type {import('types').Prerendered} */
|
|
75
|
+
const prerendered = {
|
|
76
|
+
pages: new Map(),
|
|
77
|
+
assets: new Map(),
|
|
78
|
+
redirects: new Map(),
|
|
79
|
+
paths: []
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** @type {import('types').ValidatedKitConfig} */
|
|
83
|
+
const config = (await load_config()).kit;
|
|
84
|
+
|
|
85
|
+
if (!config.prerender.enabled) {
|
|
86
|
+
output_and_exit(prerendered);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @type {import('types').Logger} */
|
|
91
|
+
const log = logger({
|
|
92
|
+
verbose: verbose === 'true'
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
installPolyfills();
|
|
96
|
+
const { fetch } = globalThis;
|
|
97
|
+
globalThis.fetch = async (info, init) => {
|
|
98
|
+
/** @type {string} */
|
|
99
|
+
let url;
|
|
100
|
+
|
|
101
|
+
/** @type {RequestInit} */
|
|
102
|
+
let opts = {};
|
|
103
|
+
|
|
104
|
+
if (info instanceof Request) {
|
|
105
|
+
url = info.url;
|
|
106
|
+
|
|
107
|
+
opts = {
|
|
108
|
+
method: info.method,
|
|
109
|
+
headers: info.headers,
|
|
110
|
+
body: info.body,
|
|
111
|
+
mode: info.mode,
|
|
112
|
+
credentials: info.credentials,
|
|
113
|
+
cache: info.cache,
|
|
114
|
+
redirect: info.redirect,
|
|
115
|
+
referrer: info.referrer,
|
|
116
|
+
integrity: info.integrity
|
|
117
|
+
};
|
|
118
|
+
} else {
|
|
119
|
+
url = info.toString();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (url.startsWith(config.prerender.origin + '/')) {
|
|
123
|
+
const request = new Request(url, opts);
|
|
124
|
+
const response = await server.respond(request, {
|
|
125
|
+
getClientAddress,
|
|
126
|
+
prerendering: {
|
|
127
|
+
dependencies: new Map()
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const decoded = new URL(url).pathname;
|
|
132
|
+
|
|
133
|
+
save(
|
|
134
|
+
'dependencies',
|
|
135
|
+
response,
|
|
136
|
+
Buffer.from(await response.clone().arrayBuffer()),
|
|
137
|
+
decoded,
|
|
138
|
+
encodeURI(decoded),
|
|
139
|
+
null,
|
|
140
|
+
'fetched'
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return response;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return fetch(info, init);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const server_root = join(config.outDir, 'output');
|
|
150
|
+
|
|
151
|
+
/** @type {import('types').ServerModule} */
|
|
152
|
+
const { Server, override } = await import(pathToFileURL(`${server_root}/server/index.js`).href);
|
|
153
|
+
|
|
154
|
+
/** @type {import('types').SSRManifest} */
|
|
155
|
+
const manifest = (await import(pathToFileURL(`${server_root}/server/manifest.js`).href)).manifest;
|
|
156
|
+
|
|
157
|
+
override({
|
|
158
|
+
paths: config.paths,
|
|
159
|
+
prerendering: true,
|
|
160
|
+
read: (file) => readFileSync(join(config.files.assets, file))
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const server = new Server(manifest);
|
|
164
|
+
await server.init({ env: JSON.parse(env) });
|
|
165
|
+
|
|
166
|
+
const error = normalise_error_handler(log, config);
|
|
167
|
+
|
|
168
|
+
const q = queue(config.prerender.concurrency);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @param {string} path
|
|
172
|
+
* @param {boolean} is_html
|
|
173
|
+
*/
|
|
174
|
+
function output_filename(path, is_html) {
|
|
175
|
+
const file = path.slice(config.paths.base.length + 1) || 'index.html';
|
|
176
|
+
|
|
177
|
+
if (is_html && !file.endsWith('.html')) {
|
|
178
|
+
return file + (file.endsWith('/') ? 'index.html' : '.html');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return file;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const files = new Set(walk(client_out_dir).map(posixify));
|
|
185
|
+
const seen = new Set();
|
|
186
|
+
const written = new Set();
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @param {string | null} referrer
|
|
190
|
+
* @param {string} decoded
|
|
191
|
+
* @param {string} [encoded]
|
|
192
|
+
*/
|
|
193
|
+
function enqueue(referrer, decoded, encoded) {
|
|
194
|
+
if (seen.has(decoded)) return;
|
|
195
|
+
seen.add(decoded);
|
|
196
|
+
|
|
197
|
+
const file = decoded.slice(config.paths.base.length + 1);
|
|
198
|
+
if (files.has(file)) return;
|
|
199
|
+
|
|
200
|
+
return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* @param {string} decoded
|
|
205
|
+
* @param {string} encoded
|
|
206
|
+
* @param {string?} referrer
|
|
207
|
+
*/
|
|
208
|
+
async function visit(decoded, encoded, referrer) {
|
|
209
|
+
if (!decoded.startsWith(config.paths.base)) {
|
|
210
|
+
error({ status: 404, path: decoded, referrer, referenceType: 'linked' });
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** @type {Map<string, import('types').PrerenderDependency>} */
|
|
215
|
+
const dependencies = new Map();
|
|
216
|
+
|
|
217
|
+
const response = await server.respond(new Request(config.prerender.origin + encoded), {
|
|
218
|
+
getClientAddress,
|
|
219
|
+
prerendering: {
|
|
220
|
+
dependencies
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
225
|
+
|
|
226
|
+
save('pages', response, body, decoded, encoded, referrer, 'linked');
|
|
227
|
+
|
|
228
|
+
for (const [dependency_path, result] of dependencies) {
|
|
229
|
+
// this seems circuitous, but using new URL allows us to not care
|
|
230
|
+
// whether dependency_path is encoded or not
|
|
231
|
+
const encoded_dependency_path = new URL(dependency_path, 'http://localhost').pathname;
|
|
232
|
+
const decoded_dependency_path = decodeURI(encoded_dependency_path);
|
|
233
|
+
|
|
234
|
+
const body = result.body ?? new Uint8Array(await result.response.arrayBuffer());
|
|
235
|
+
save(
|
|
236
|
+
'dependencies',
|
|
237
|
+
result.response,
|
|
238
|
+
body,
|
|
239
|
+
decoded_dependency_path,
|
|
240
|
+
encoded_dependency_path,
|
|
241
|
+
decoded,
|
|
242
|
+
'fetched'
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (config.prerender.crawl && response.headers.get('content-type') === 'text/html') {
|
|
247
|
+
for (const href of crawl(body.toString())) {
|
|
248
|
+
if (href.startsWith('data:') || href.startsWith('#')) continue;
|
|
249
|
+
|
|
250
|
+
const resolved = resolve(encoded, href);
|
|
251
|
+
if (!is_root_relative(resolved)) continue;
|
|
252
|
+
|
|
253
|
+
const { pathname, search } = new URL(resolved, 'http://localhost');
|
|
254
|
+
|
|
255
|
+
if (search) {
|
|
256
|
+
// TODO warn that query strings have no effect on statically-exported pages
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
enqueue(decoded, decodeURI(pathname), pathname);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @param {'pages' | 'dependencies'} category
|
|
266
|
+
* @param {Response} response
|
|
267
|
+
* @param {string | Uint8Array} body
|
|
268
|
+
* @param {string} decoded
|
|
269
|
+
* @param {string} encoded
|
|
270
|
+
* @param {string | null} referrer
|
|
271
|
+
* @param {'linked' | 'fetched'} referenceType
|
|
272
|
+
*/
|
|
273
|
+
function save(category, response, body, decoded, encoded, referrer, referenceType) {
|
|
274
|
+
const response_type = Math.floor(response.status / 100);
|
|
275
|
+
const type = /** @type {string} */ (response.headers.get('content-type'));
|
|
276
|
+
const is_html = response_type === REDIRECT || type === 'text/html';
|
|
277
|
+
|
|
278
|
+
const file = output_filename(decoded, is_html);
|
|
279
|
+
const dest = `${config.outDir}/output/prerendered/${category}/${file}`;
|
|
280
|
+
|
|
281
|
+
if (written.has(file)) return;
|
|
282
|
+
|
|
283
|
+
if (response_type === REDIRECT) {
|
|
284
|
+
const location = response.headers.get('location');
|
|
285
|
+
|
|
286
|
+
if (location) {
|
|
287
|
+
const resolved = resolve(encoded, location);
|
|
288
|
+
if (is_root_relative(resolved)) {
|
|
289
|
+
enqueue(decoded, decodeURI(resolved), resolved);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (!response.headers.get('x-sveltekit-normalize')) {
|
|
293
|
+
mkdirp(dirname(dest));
|
|
294
|
+
|
|
295
|
+
log.warn(`${response.status} ${decoded} -> ${location}`);
|
|
296
|
+
|
|
297
|
+
writeFileSync(
|
|
298
|
+
dest,
|
|
299
|
+
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
written.add(file);
|
|
303
|
+
|
|
304
|
+
if (!prerendered.redirects.has(decoded)) {
|
|
305
|
+
prerendered.redirects.set(decoded, {
|
|
306
|
+
status: response.status,
|
|
307
|
+
location: resolved
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
prerendered.paths.push(decoded);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
log.warn(`location header missing on redirect received from ${decoded}`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (response.status === 200) {
|
|
321
|
+
mkdirp(dirname(dest));
|
|
322
|
+
|
|
323
|
+
log.info(`${response.status} ${decoded}`);
|
|
324
|
+
writeFileSync(dest, body);
|
|
325
|
+
written.add(file);
|
|
326
|
+
|
|
327
|
+
if (is_html) {
|
|
328
|
+
prerendered.pages.set(decoded, {
|
|
329
|
+
file
|
|
330
|
+
});
|
|
331
|
+
} else {
|
|
332
|
+
prerendered.assets.set(decoded, {
|
|
333
|
+
type
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
prerendered.paths.push(decoded);
|
|
338
|
+
} else if (response_type !== OK) {
|
|
339
|
+
error({ status: response.status, path: decoded, referrer, referenceType });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (config.prerender.enabled) {
|
|
344
|
+
for (const entry of config.prerender.entries) {
|
|
345
|
+
if (entry === '*') {
|
|
346
|
+
/** @type {import('types').SSRManifest} */
|
|
347
|
+
const manifest = (await import(pathToFileURL(manifest_path).href)).manifest;
|
|
348
|
+
const { routes } = manifest._;
|
|
349
|
+
const entries = compact(routes.map((route) => route.page && get_path(route.id)));
|
|
350
|
+
|
|
351
|
+
for (const entry of entries) {
|
|
352
|
+
enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these?
|
|
353
|
+
}
|
|
354
|
+
} else {
|
|
355
|
+
enqueue(null, config.paths.base + entry);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
await q.done();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const rendered = await server.respond(new Request(config.prerender.origin + '/[fallback]'), {
|
|
363
|
+
getClientAddress,
|
|
364
|
+
prerendering: {
|
|
365
|
+
fallback: true,
|
|
366
|
+
dependencies: new Map()
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const file = `${config.outDir}/output/prerendered/fallback.html`;
|
|
371
|
+
mkdirp(dirname(file));
|
|
372
|
+
writeFileSync(file, await rendered.text());
|
|
373
|
+
|
|
374
|
+
output_and_exit(prerendered);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** @return {string} */
|
|
378
|
+
function getClientAddress() {
|
|
379
|
+
throw new Error('Cannot read clientAddress during prerendering');
|
|
380
|
+
}
|
|
@@ -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
|
+
}
|