( platformRef: PlatformRef, applicationRef: ApplicationRef, )
| 195 | * @returns A promise that resolves to the rendered string. |
| 196 | */ |
| 197 | export async function renderInternal( |
| 198 | platformRef: PlatformRef, |
| 199 | applicationRef: ApplicationRef, |
| 200 | ): Promise<string> { |
| 201 | if (platformRef.destroyed || applicationRef.destroyed) { |
| 202 | return ''; |
| 203 | } |
| 204 | |
| 205 | const platformState = platformRef.injector.get(PlatformState); |
| 206 | prepareForHydration(platformState, applicationRef); |
| 207 | appendServerContextInfo(applicationRef); |
| 208 | |
| 209 | // Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string. |
| 210 | const environmentInjector = applicationRef.injector; |
| 211 | const errorHandler = environmentInjector.get(INTERNAL_APPLICATION_ERROR_HANDLER); |
| 212 | const callbacks = environmentInjector.get(BEFORE_APP_SERIALIZED, null); |
| 213 | if (callbacks) { |
| 214 | const asyncCallbacks: Promise<void>[] = []; |
| 215 | for (const callback of callbacks) { |
| 216 | try { |
| 217 | const callbackResult = callback(); |
| 218 | if (callbackResult) { |
| 219 | asyncCallbacks.push(callbackResult); |
| 220 | } |
| 221 | } catch (e) { |
| 222 | // Delegate to the application's ErrorHandler so custom handlers |
| 223 | // (e.g. Sentry) are notified, rather than writing directly to console. |
| 224 | if (!environmentInjector.destroyed) { |
| 225 | errorHandler(e); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (asyncCallbacks.length) { |
| 231 | if (platformRef.destroyed || applicationRef.destroyed) { |
| 232 | void Promise.allSettled(asyncCallbacks); |
| 233 | return ''; |
| 234 | } |
| 235 | |
| 236 | const settled = await new Promise<PromiseSettledResult<void>[] | null>((resolve) => { |
| 237 | const unregisterDestroy = applicationRef.onDestroy(() => { |
| 238 | resolve(null); |
| 239 | }); |
| 240 | Promise.allSettled(asyncCallbacks).then((results) => { |
| 241 | unregisterDestroy(); |
| 242 | resolve(results); |
| 243 | }); |
| 244 | }); |
| 245 | |
| 246 | if (settled === null) { |
| 247 | return ''; |
| 248 | } |
| 249 | |
| 250 | for (const result of settled) { |
| 251 | if (result.status === 'rejected' && !environmentInjector.destroyed) { |
| 252 | errorHandler(result.reason); |
| 253 | } |
| 254 | } |
no test coverage detected