|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import type { Type } from '@angular/core'; |
| 10 | +import type * as platformServer from '@angular/platform-server'; |
| 11 | +import assert from 'assert'; |
9 | 12 | import * as fs from 'fs'; |
10 | 13 | import * as path from 'path'; |
11 | 14 | import { loadEsmModule } from '../utils/utils'; |
@@ -41,28 +44,39 @@ export async function render({ |
41 | 44 | const outputFolderPath = path.join(outputPath, route); |
42 | 45 | const outputIndexPath = path.join(outputFolderPath, 'index.html'); |
43 | 46 |
|
44 | | - const { renderModule, AppServerModule } = await import(serverBundlePath); |
| 47 | + const { AppServerModule, renderModule, ɵSERVER_CONTEXT } = (await import(serverBundlePath)) as { |
| 48 | + renderModule: typeof platformServer.renderModule | undefined; |
| 49 | + ɵSERVER_CONTEXT: typeof platformServer.ɵSERVER_CONTEXT | undefined; |
| 50 | + AppServerModule: Type<unknown> | undefined; |
| 51 | + }; |
| 52 | + |
| 53 | + assert(renderModule, `renderModule was not exported from: ${serverBundlePath}.`); |
| 54 | + assert(AppServerModule, `AppServerModule was not exported from: ${serverBundlePath}.`); |
| 55 | + assert(ɵSERVER_CONTEXT, `ɵSERVER_CONTEXT was not exported from: ${serverBundlePath}.`); |
45 | 56 |
|
46 | 57 | const indexBaseName = fs.existsSync(path.join(outputPath, 'index.original.html')) |
47 | 58 | ? 'index.original.html' |
48 | 59 | : indexFile; |
49 | 60 | const browserIndexInputPath = path.join(outputPath, indexBaseName); |
50 | | - let indexHtml = await fs.promises.readFile(browserIndexInputPath, 'utf8'); |
51 | | - indexHtml = indexHtml.replace( |
52 | | - '</html>', |
53 | | - '<!-- This page was prerendered with Angular Universal -->\n</html>', |
54 | | - ); |
| 61 | + let document = await fs.promises.readFile(browserIndexInputPath, 'utf8'); |
| 62 | + |
55 | 63 | if (inlineCriticalCss) { |
56 | 64 | // Workaround for https://github.com/GoogleChromeLabs/critters/issues/64 |
57 | | - indexHtml = indexHtml.replace( |
| 65 | + document = document.replace( |
58 | 66 | / media="print" onload="this\.media='all'"><noscript><link .+?><\/noscript>/g, |
59 | 67 | '>', |
60 | 68 | ); |
61 | 69 | } |
62 | 70 |
|
63 | 71 | let html = await renderModule(AppServerModule, { |
64 | | - document: indexHtml, |
| 72 | + document, |
65 | 73 | url: route, |
| 74 | + extraProviders: [ |
| 75 | + { |
| 76 | + provide: ɵSERVER_CONTEXT, |
| 77 | + useValue: 'ssg', |
| 78 | + }, |
| 79 | + ], |
66 | 80 | }); |
67 | 81 |
|
68 | 82 | if (inlineCriticalCss) { |
|
0 commit comments