( html: string, bootstrap: AngularBootstrap, url: URL, platformProviders: StaticProvider[], serverContext: string, )
| 53 | * - `content`: A function returning a promise that resolves to the rendered HTML string. |
| 54 | */ |
| 55 | export async function renderAngular( |
| 56 | html: string, |
| 57 | bootstrap: AngularBootstrap, |
| 58 | url: URL, |
| 59 | platformProviders: StaticProvider[], |
| 60 | serverContext: string, |
| 61 | ): Promise< |
| 62 | | { hasNavigationError: true } |
| 63 | | { hasNavigationError: boolean; redirectTo?: string; content: () => Promise<string> } |
| 64 | > { |
| 65 | // A request to `http://www.example.com/page/index.html` will render the Angular route corresponding to `http://www.example.com/page`. |
| 66 | const urlToRender = stripIndexHtmlFromURL(url); |
| 67 | const platformRef = platformServer([ |
| 68 | { |
| 69 | provide: INITIAL_CONFIG, |
| 70 | useValue: { |
| 71 | url: urlToRender.href, |
| 72 | document: html, |
| 73 | }, |
| 74 | }, |
| 75 | { |
| 76 | provide: SERVER_CONTEXT, |
| 77 | useValue: serverContext, |
| 78 | }, |
| 79 | { |
| 80 | // An Angular Console Provider that does not print a set of predefined logs. |
| 81 | provide: ɵConsole, |
| 82 | // Using `useClass` would necessitate decorating `Console` with `@Injectable`, |
| 83 | // which would require switching from `ts_library` to `ng_module`. This change |
| 84 | // would also necessitate various patches of `@angular/bazel` to support ESM. |
| 85 | useFactory: () => new Console(), |
| 86 | }, |
| 87 | ...platformProviders, |
| 88 | ]); |
| 89 | |
| 90 | let redirectTo: string | undefined; |
| 91 | let hasNavigationError = true; |
| 92 | |
| 93 | try { |
| 94 | let applicationRef: ApplicationRef; |
| 95 | if (isNgModule(bootstrap)) { |
| 96 | const moduleRef = await platformRef.bootstrapModule(bootstrap); |
| 97 | applicationRef = moduleRef.injector.get(ApplicationRef); |
| 98 | } else { |
| 99 | applicationRef = await bootstrap({ platformRef }); |
| 100 | } |
| 101 | |
| 102 | // Block until application is stable. |
| 103 | await applicationRef.whenStable(); |
| 104 | |
| 105 | // This code protect against app destruction during bootstrapping which is a |
| 106 | // valid case. We should not assume the `applicationRef` is not in destroyed state. |
| 107 | // Calling `envInjector.get` would throw `NG0205: Injector has already been destroyed`. |
| 108 | if (applicationRef.destroyed) { |
| 109 | return { hasNavigationError: true }; |
| 110 | } |
| 111 | |
| 112 | // TODO(alanagius): Find a way to avoid rendering here especially for redirects as any output will be discarded. |
no test coverage detected