forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-worker.ts
64 lines (51 loc) · 1.87 KB
/
render-worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { workerData } from 'node:worker_threads';
import type { OutputMode } from '../../builders/application/schema';
import type { ESMInMemoryFileLoaderWorkerData } from './esm-in-memory-loader/loader-hooks';
import { patchFetchToLoadInMemoryAssets } from './fetch-patch';
import { DEFAULT_URL, launchServer } from './launch-server';
import { loadEsmModuleFromMemory } from './load-esm-from-memory';
export interface RenderWorkerData extends ESMInMemoryFileLoaderWorkerData {
assetFiles: Record</** Destination */ string, /** Source */ string>;
outputMode: OutputMode | undefined;
hasSsrEntry: boolean;
}
export interface RenderOptions {
url: string;
}
/**
* This is passed as workerData when setting up the worker via the `piscina` package.
*/
const { outputMode, hasSsrEntry } = workerData as {
outputMode: OutputMode | undefined;
hasSsrEntry: boolean;
};
let serverURL = DEFAULT_URL;
/**
* Renders each route in routes and writes them to <outputPath>/<route>/index.html.
*/
async function renderPage({ url }: RenderOptions): Promise<string | null> {
const { ɵgetOrCreateAngularServerApp: getOrCreateAngularServerApp } =
await loadEsmModuleFromMemory('./main.server.mjs');
const angularServerApp = getOrCreateAngularServerApp({
allowStaticRouteRender: true,
});
const response = await angularServerApp.handle(
new Request(new URL(url, serverURL), { signal: AbortSignal.timeout(30_000) }),
);
return response ? response.text() : null;
}
async function initialize() {
if (outputMode !== undefined && hasSsrEntry) {
serverURL = await launchServer();
}
patchFetchToLoadInMemoryAssets(serverURL);
return renderPage;
}
export default initialize();