Skip to content

Commit 0e00d61

Browse files
committed
fixup! fix(@angular/ssr): destroy platform when request is aborted during rendering
1 parent 7afdd87 commit 0e00d61

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

  • packages/angular/ssr/src/utils

‎packages/angular/ssr/src/utils/ng.ts‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export type AngularBootstrap =
5151
* @returns A promise resolving to an object containing:
5252
* - `hasNavigationError`: Indicates if a navigation error occurred or if rendering was aborted/destroyed before stability.
5353
* - `redirectTo`: (Optional) The redirect URL if a navigation redirect occurred.
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.
54+
* - `content`: (Optional) A function returning a promise that resolves to the rendered HTML string.
55+
* - `destroy`: (Optional) A function to destroy the underlying Angular platform.
5656
*/
5757
export async function renderAngular(
5858
html: string,
@@ -102,15 +102,20 @@ export async function renderAngular(
102102
let redirectTo: string | undefined;
103103
let hasNavigationError = true;
104104
let renderTimeoutId: ReturnType<typeof setTimeout> | undefined;
105+
let destroyPromise: Promise<void> | undefined;
105106

106-
const destroy = () => {
107-
clearTimeout(renderTimeoutId);
108-
signal?.removeEventListener('abort', abortHandler);
109-
void asyncDestroyPlatform(platformRef);
107+
const destroy = (): Promise<void> => {
108+
if (!destroyPromise) {
109+
clearTimeout(renderTimeoutId);
110+
signal?.removeEventListener('abort', abortHandler);
111+
destroyPromise = asyncDestroyPlatform(platformRef);
112+
}
113+
114+
return destroyPromise;
110115
};
111116

112117
const abortHandler = () => {
113-
destroy();
118+
void destroy();
114119
};
115120

116121
try {
@@ -168,34 +173,33 @@ export async function renderAngular(
168173
}
169174

170175
return {
171-
destroy,
176+
destroy: () => void destroy(),
172177
hasNavigationError,
173178
redirectTo,
174179
content: () =>
175180
new Promise<string>((resolve, reject) => {
176181
// Defer rendering to the next event loop iteration to avoid blocking, as most operations in `renderInternal` are synchronous.
177182
renderTimeoutId = setTimeout(() => {
178183
if (platformRef.destroyed || applicationRef.destroyed || signal?.aborted) {
179-
destroy();
184+
void destroy();
180185

181186
return;
182187
}
183188

184189
renderInternal(platformRef, applicationRef)
185190
.then(resolve)
186191
.catch(reject)
187-
.finally(destroy);
192+
.finally(() => void destroy());
188193
}, 0);
189194
}),
190195
};
191196
} catch (error) {
192-
signal?.removeEventListener('abort', abortHandler);
193-
await asyncDestroyPlatform(platformRef);
197+
await destroy();
194198

195199
throw error;
196200
} finally {
197201
if (hasNavigationError || redirectTo) {
198-
destroy();
202+
void destroy();
199203
}
200204
}
201205
}

0 commit comments

Comments
 (0)