fix(@angular/ssr): destroy platform when response stream is cancelled · angular/angular-cli@6a34467 · GitHub
Skip to content

Commit 6a34467

Browse files
committed
fix(@angular/ssr): destroy platform when response stream is cancelled
Ensures PlatformRef is properly destroyed when an active response stream is cancelled or encounters an error during rendering to prevent memory leaks.
1 parent ecf8c08 commit 6a34467

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎packages/angular/ssr/src/app.ts‎

Lines changed: 12 additions & 4 deletions

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ import { addTrailingSlash, joinUrlParts, stripIndexHtmlFromURL, stripTrailingSla
3434
* - A function that returns a `Promise<ApplicationRef>`, which resolves with the root application reference.
3535
*/
3636
export type AngularBootstrap =
37-
| Type<unknown>
38-
| ((context: BootstrapContext) => Promise<ApplicationRef>);
37+
Type<unknown> | ((context: BootstrapContext) => Promise<ApplicationRef>);
3938

4039
/**
4140
* Renders an Angular application or module to an HTML string.
@@ -60,7 +59,12 @@ export async function renderAngular(
6059
serverContext: string,
6160
): Promise<
6261
| { hasNavigationError: true }
63-
| { hasNavigationError: boolean; redirectTo?: string; content: () => Promise<string> }
62+
| {
63+
hasNavigationError: boolean;
64+
redirectTo?: string;
65+
content: () => Promise<string>;
66+
destroy: () => void;
67+
}
6468
> {
6569
// A request to `http://www.example.com/page/index.html` will render the Angular route corresponding to `http://www.example.com/page`.
6670
const urlToRender = stripIndexHtmlFromURL(url);
@@ -134,6 +138,7 @@ export async function renderAngular(
134138
}
135139

136140
return {
141+
destroy: () => void asyncDestroyPlatform(platformRef),
137142
hasNavigationError,
138143
redirectTo,
139144
content: () =>
@@ -177,6 +182,10 @@ export function isNgModule(value: AngularBootstrap): value is Type<unknown> {
177182
* @param platformRef - The platform reference to be destroyed.
178183
*/
179184
function asyncDestroyPlatform(platformRef: PlatformRef): Promise<void> {
185+
if (platformRef.destroyed) {
186+
return Promise.resolve();
187+
}
188+
180189
return new Promise((resolve) => {
181190
setTimeout(() => {
182191
if (!platformRef.destroyed) {

‎packages/angular/ssr/test/app_spec.ts‎

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)