|
1 |
| -import { readFileSync, writeFileSync } from 'node:fs'; |
2 |
| -import { dirname, join } from 'node:path'; |
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
3 | 3 | import { pathToFileURL } from 'node:url';
|
4 |
| -import { mkdirp } from '../../utils/filesystem.js'; |
5 | 4 | import { installPolyfills } from '../../exports/node/polyfills.js';
|
6 | 5 | import { load_config } from '../config/index.js';
|
| 6 | +import { forked } from '../../utils/fork.js'; |
7 | 7 |
|
8 |
| -const [, , dest, manifest_path, env] = process.argv; |
| 8 | +export default forked(import.meta.url, generate_fallback); |
9 | 9 |
|
10 |
| -/** @type {import('types').ValidatedKitConfig} */ |
11 |
| -const config = (await load_config()).kit; |
| 10 | +/** |
| 11 | + * @param {{ |
| 12 | + * manifest_path: string; |
| 13 | + * env: Record<string, string> |
| 14 | + * }} opts |
| 15 | + */ |
| 16 | +async function generate_fallback({ manifest_path, env }) { |
| 17 | + /** @type {import('types').ValidatedKitConfig} */ |
| 18 | + const config = (await load_config()).kit; |
12 | 19 |
|
13 |
| -installPolyfills(); |
| 20 | + installPolyfills(); |
14 | 21 |
|
15 |
| -const server_root = join(config.outDir, 'output'); |
| 22 | + const server_root = join(config.outDir, 'output'); |
16 | 23 |
|
17 |
| -/** @type {import('types').ServerInternalModule} */ |
18 |
| -const { set_building } = await import(pathToFileURL(`${server_root}/server/internal.js`).href); |
| 24 | + /** @type {import('types').ServerInternalModule} */ |
| 25 | + const { set_building } = await import(pathToFileURL(`${server_root}/server/internal.js`).href); |
19 | 26 |
|
20 |
| -/** @type {import('types').ServerModule} */ |
21 |
| -const { Server } = await import(pathToFileURL(`${server_root}/server/index.js`).href); |
| 27 | + /** @type {import('types').ServerModule} */ |
| 28 | + const { Server } = await import(pathToFileURL(`${server_root}/server/index.js`).href); |
22 | 29 |
|
23 |
| -/** @type {import('types').SSRManifest} */ |
24 |
| -const manifest = (await import(pathToFileURL(manifest_path).href)).manifest; |
| 30 | + /** @type {import('types').SSRManifest} */ |
| 31 | + const manifest = (await import(pathToFileURL(manifest_path).href)).manifest; |
25 | 32 |
|
26 |
| -set_building(true); |
| 33 | + set_building(true); |
27 | 34 |
|
28 |
| -const server = new Server(manifest); |
29 |
| -await server.init({ env: JSON.parse(env) }); |
| 35 | + const server = new Server(manifest); |
| 36 | + await server.init({ env }); |
30 | 37 |
|
31 |
| -const rendered = await server.respond(new Request(config.prerender.origin + '/[fallback]'), { |
32 |
| - getClientAddress: () => { |
33 |
| - throw new Error('Cannot read clientAddress during prerendering'); |
34 |
| - }, |
35 |
| - prerendering: { |
36 |
| - fallback: true, |
37 |
| - dependencies: new Map() |
38 |
| - }, |
39 |
| - read: (file) => readFileSync(join(config.files.assets, file)) |
40 |
| -}); |
| 38 | + const response = await server.respond(new Request(config.prerender.origin + '/[fallback]'), { |
| 39 | + getClientAddress: () => { |
| 40 | + throw new Error('Cannot read clientAddress during prerendering'); |
| 41 | + }, |
| 42 | + prerendering: { |
| 43 | + fallback: true, |
| 44 | + dependencies: new Map() |
| 45 | + }, |
| 46 | + read: (file) => readFileSync(join(config.files.assets, file)) |
| 47 | + }); |
41 | 48 |
|
42 |
| -mkdirp(dirname(dest)); |
43 |
| -writeFileSync(dest, await rendered.text()); |
| 49 | + if (response.ok) { |
| 50 | + return await response.text(); |
| 51 | + } |
| 52 | + |
| 53 | + throw new Error(`Could not create a fallback page — failed with status ${response.status}`); |
| 54 | +} |
0 commit comments