Skip to content

Commit d079bbc

Browse files
committed
fixup! fix(@angular/ssr): destroy platform when request is aborted during rendering
1 parent 6024ec1 commit d079bbc

2 files changed

Lines changed: 52 additions & 28 deletions

File tree

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,17 @@ export async function renderAngular(
103103
let hasNavigationError = true;
104104
let renderTimeoutId: ReturnType<typeof setTimeout> | undefined;
105105
let destroyPromise: Promise<void> | undefined;
106+
let rejectContent: ((reason?: unknown) => void) | undefined;
106107

107108
const destroy = (): Promise<void> => {
109+
rejectContent?.(
110+
new DOMException(
111+
`Request for: ${url} was aborted.${signal?.reason ? `\n${signal.reason}` : ''}`,
112+
'AbortError',
113+
),
114+
);
115+
rejectContent = undefined;
116+
108117
if (!destroyPromise) {
109118
clearTimeout(renderTimeoutId);
110119
signal?.removeEventListener('abort', abortHandler);
@@ -178,6 +187,8 @@ export async function renderAngular(
178187
redirectTo,
179188
content: () =>
180189
new Promise<string>((resolve, reject) => {
190+
rejectContent = reject;
191+
181192
// Defer rendering to the next event loop iteration to avoid blocking, as most operations in `renderInternal` are synchronous.
182193
renderTimeoutId = setTimeout(() => {
183194
if (platformRef.destroyed || applicationRef.destroyed || signal?.aborted) {
@@ -189,7 +200,10 @@ export async function renderAngular(
189200
renderInternal(platformRef, applicationRef)
190201
.then(resolve)
191202
.catch(reject)
192-
.finally(() => void destroy());
203+
.finally(() => {
204+
rejectContent = undefined;
205+
void destroy();
206+
});
193207
}, 0);
194208
}),
195209
};

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

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ import { setAngularAppTestingManifest } from './testing-utils';
2929

3030
describe('AngularServerApp', () => {
3131
let app: AngularServerApp;
32+
let slowRouteHook: (() => void) | undefined;
3233

3334
@Component({
3435
selector: 'app-home',
3536
template: `Home works`,
3637
})
3738
class HomeComponent {
3839
constructor() {
39-
if (inject(ActivatedRoute).snapshot.data['destroyApp']) {
40+
const data = inject(ActivatedRoute).snapshot.data;
41+
if (data['destroyApp']) {
4042
inject(PlatformRef).destroy();
4143
}
44+
if (data['slow']) {
45+
slowRouteHook?.();
46+
}
4247
}
4348
}
4449

@@ -86,6 +91,7 @@ describe('AngularServerApp', () => {
8691
{ path: 'page-with-headers', component: HomeComponent },
8792
{ path: 'page-with-status', component: HomeComponent },
8893
{ path: 'page-destroy-app', component: HomeComponent, data: { destroyApp: true } },
94+
{ path: 'page-slow', component: HomeComponent, data: { slow: true } },
8995
{ path: 'redirect', redirectTo: 'home' },
9096
{ path: 'redirect-via-navigate', component: RedirectComponent },
9197
{
@@ -335,34 +341,22 @@ describe('AngularServerApp', () => {
335341
let appDestroyed = false;
336342
let destroySpy: jasmine.Spy | undefined;
337343

338-
@Component({
339-
selector: 'app-slow',
340-
template: 'Slow page',
341-
})
342-
class SlowComponent {
343-
constructor() {
344-
destroySpy = spyOn(inject(PlatformRef), 'destroy').and.callThrough();
345-
const pendingTasks = inject(PendingTasks);
346-
const removeTask = pendingTasks.add();
347-
inject(DestroyRef).onDestroy(() => {
348-
appDestroyed = true;
349-
removeTask();
350-
});
351-
resolveTaskReady();
352-
}
353-
}
344+
slowRouteHook = () => {
345+
destroySpy = spyOn(inject(PlatformRef), 'destroy').and.callThrough();
346+
const pendingTasks = inject(PendingTasks);
347+
const removeTask = pendingTasks.add();
348+
inject(DestroyRef).onDestroy(() => {
349+
appDestroyed = true;
350+
removeTask();
351+
});
352+
resolveTaskReady();
353+
};
354354

355355
try {
356-
setAngularAppTestingManifest(
357-
[{ path: 'slow', component: SlowComponent }],
358-
[{ path: 'slow', renderMode: RenderMode.Server }],
359-
);
360-
361-
const customApp = new AngularServerApp();
362356
const controller = new AbortController();
363-
const request = new Request('http://localhost/slow', { signal: controller.signal });
357+
const request = new Request('http://localhost/page-slow', { signal: controller.signal });
364358

365-
const handlePromise = customApp.handle(request);
359+
const handlePromise = app.handle(request);
366360

367361
// Wait until the component is instantiated and holding a pending task
368362
await taskReady;
@@ -373,15 +367,15 @@ describe('AngularServerApp', () => {
373367

374368
await expectAsync(handlePromise).toBeRejectedWithError(
375369
DOMException,
376-
/Request for: http:\/\/localhost\/slow was aborted/,
370+
/Request for: http:\/\/localhost\/page-slow was aborted/,
377371
);
378372

379373
// Wait for the macrotask queue to clear since platform destruction is scheduled via setTimeout(0)
380374
await setTimeout(0);
381375
expect(destroySpy).toHaveBeenCalledTimes(1);
382376
expect(appDestroyed).toBeTrue();
383377
} finally {
384-
setupManifest();
378+
slowRouteHook = undefined;
385379
}
386380
});
387381

@@ -417,6 +411,22 @@ describe('AngularServerApp', () => {
417411
expect(removeEventListenerSpy).toHaveBeenCalledWith('abort', jasmine.any(Function));
418412
});
419413

414+
it('should reject reading response body if request is aborted after handle() resolves but before rendering completes', async () => {
415+
const controller = new AbortController();
416+
const response = await app.handle(
417+
new Request('http://localhost/home', { signal: controller.signal }),
418+
);
419+
expect(response).not.toBeNull();
420+
421+
controller.abort();
422+
423+
await expectAsync(response?.text()).toBeRejectedWithError(
424+
DOMException,
425+
/Request for: http:\/\/localhost\/home was aborted/,
426+
);
427+
await setTimeout(0);
428+
});
429+
420430
it('should return configured headers for pages with specific header settings', async () => {
421431
const response = await app.handle(new Request('http://localhost/page-with-headers'));
422432
const headers = response?.headers.entries() ?? [];

0 commit comments

Comments
 (0)