@@ -47,17 +47,20 @@ export type AngularBootstrap =
4747 * @param url - The application URL, used for route-based rendering in SSR.
4848 * @param platformProviders - An array of platform providers for the rendering process.
4949 * @param serverContext - A string representing the server context, providing additional metadata for SSR.
50+ * @param signal - An optional `AbortSignal` to abort the rendering process and destroy the platform.
5051 * @returns A promise resolving to an object containing:
51- * - `hasNavigationError`: Indicates if a navigation error occurred.
52+ * - `hasNavigationError`: Indicates if a navigation error occurred or if rendering was aborted/destroyed before stability .
5253 * - `redirectTo`: (Optional) The redirect URL if a navigation redirect occurred.
53- * - `content`: A function returning a promise that resolves to the rendered HTML string.
54+ * - `content`: (Present when `hasNavigationError` is `false`) A function returning a promise that resolves to the rendered HTML string.
55+ * - `destroy`: (Present when `hasNavigationError` is `false`) A function to destroy the underlying Angular platform.
5456 */
5557export async function renderAngular (
5658 html : string ,
5759 bootstrap : AngularBootstrap ,
5860 url : URL ,
5961 platformProviders : StaticProvider [ ] ,
6062 serverContext : string ,
63+ signal ?: AbortSignal ,
6164) : Promise <
6265 | { hasNavigationError : true }
6366 | {
@@ -67,6 +70,10 @@ export async function renderAngular(
6770 destroy : ( ) => void ;
6871 }
6972> {
73+ if ( signal ?. aborted ) {
74+ return { hasNavigationError : true } ;
75+ }
76+
7077 // A request to `http://www.example.com/page/index.html` will render the Angular route corresponding to `http://www.example.com/page`.
7178 const urlToRender = stripIndexHtmlFromURL ( url ) ;
7279 const platformRef = platformServer ( [
@@ -94,14 +101,34 @@ export async function renderAngular(
94101
95102 let redirectTo : string | undefined ;
96103 let hasNavigationError = true ;
104+ let renderTimeoutId : ReturnType < typeof setTimeout > | undefined ;
105+
106+ const destroy = ( ) => {
107+ clearTimeout ( renderTimeoutId ) ;
108+ signal ?. removeEventListener ( 'abort' , abortHandler ) ;
109+ void asyncDestroyPlatform ( platformRef ) ;
110+ } ;
111+
112+ const abortHandler = ( ) => {
113+ destroy ( ) ;
114+ } ;
97115
98116 try {
117+ signal ?. addEventListener ( 'abort' , abortHandler , { once : true } ) ;
118+
99119 let applicationRef : ApplicationRef ;
100120 if ( isNgModule ( bootstrap ) ) {
101121 const moduleRef = await platformRef . bootstrapModule ( bootstrap ) ;
122+ if ( platformRef . destroyed || signal ?. aborted ) {
123+ return { hasNavigationError : true } ;
124+ }
125+
102126 applicationRef = moduleRef . injector . get ( ApplicationRef ) ;
103127 } else {
104128 applicationRef = await bootstrap ( { platformRef } ) ;
129+ if ( platformRef . destroyed || signal ?. aborted ) {
130+ return { hasNavigationError : true } ;
131+ }
105132 }
106133
107134 // Block until application is stable.
@@ -110,7 +137,7 @@ export async function renderAngular(
110137 // This code protect against app destruction during bootstrapping which is a
111138 // valid case. We should not assume the `applicationRef` is not in destroyed state.
112139 // Calling `envInjector.get` would throw `NG0205: Injector has already been destroyed`.
113- if ( applicationRef . destroyed ) {
140+ if ( applicationRef . destroyed || signal ?. aborted ) {
114141 return { hasNavigationError : true } ;
115142 }
116143
@@ -141,27 +168,34 @@ export async function renderAngular(
141168 }
142169
143170 return {
144- destroy : ( ) => void asyncDestroyPlatform ( platformRef ) ,
171+ destroy,
145172 hasNavigationError,
146173 redirectTo,
147174 content : ( ) =>
148175 new Promise < string > ( ( resolve , reject ) => {
149176 // Defer rendering to the next event loop iteration to avoid blocking, as most operations in `renderInternal` are synchronous.
150- setTimeout ( ( ) => {
177+ renderTimeoutId = setTimeout ( ( ) => {
178+ if ( platformRef . destroyed || applicationRef . destroyed || signal ?. aborted ) {
179+ destroy ( ) ;
180+
181+ return ;
182+ }
183+
151184 renderInternal ( platformRef , applicationRef )
152185 . then ( resolve )
153186 . catch ( reject )
154- . finally ( ( ) => void asyncDestroyPlatform ( platformRef ) ) ;
187+ . finally ( destroy ) ;
155188 } , 0 ) ;
156189 } ) ,
157190 } ;
158191 } catch ( error ) {
192+ signal ?. removeEventListener ( 'abort' , abortHandler ) ;
159193 await asyncDestroyPlatform ( platformRef ) ;
160194
161195 throw error ;
162196 } finally {
163197 if ( hasNavigationError || redirectTo ) {
164- void asyncDestroyPlatform ( platformRef ) ;
198+ destroy ( ) ;
165199 }
166200 }
167201}
0 commit comments