Skip to content

Commit fda945c

Browse files
committed
fixup! fix(@angular/ssr): guard aborted requests across rendering and serialization
1 parent 66d97cf commit fda945c

2 files changed

Lines changed: 141 additions & 41 deletions

File tree

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,21 @@ export async function renderAngular(
103103
let hasNavigationError = true;
104104
let renderTimeoutId: ReturnType<typeof setTimeout> | undefined;
105105
let destroyPromise: Promise<void> | undefined;
106+
let contentPromise: Promise<string> | undefined;
106107
let rejectContent: ((reason?: unknown) => void) | undefined;
107108

108-
const destroy = (): Promise<void> => {
109-
clearTimeout(renderTimeoutId);
110-
rejectContent?.(
111-
new DOMException(
112-
`Request for: ${url} was aborted.${signal?.reason ? `\n${signal.reason}` : ''}`,
113-
'AbortError',
114-
),
109+
const createAbortError = (): DOMException =>
110+
new DOMException(
111+
`Request for: ${url} was aborted.${signal?.reason ? `\n${signal.reason}` : ''}`,
112+
'AbortError',
115113
);
116-
rejectContent = undefined;
117114

115+
const destroy = (): Promise<void> => {
118116
if (!destroyPromise) {
117+
clearTimeout(renderTimeoutId);
118+
rejectContent?.(createAbortError());
119+
rejectContent = undefined;
120+
contentPromise = undefined;
119121
signal?.removeEventListener('abort', abortHandler);
120122
destroyPromise = asyncDestroyPlatform(platformRef);
121123
}
@@ -183,20 +185,20 @@ export async function renderAngular(
183185
destroy,
184186
hasNavigationError,
185187
redirectTo,
186-
content: () =>
187-
new Promise<string>((resolve, reject) => {
188-
rejectContent = reject;
189-
190-
if (
191-
destroyPromise ||
192-
platformRef.destroyed ||
193-
applicationRef.destroyed ||
194-
signal?.aborted
195-
) {
196-
void destroy();
188+
content: () => {
189+
if (
190+
destroyPromise ||
191+
platformRef.destroyed ||
192+
applicationRef.destroyed ||
193+
signal?.aborted
194+
) {
195+
void destroy();
196+
197+
return Promise.reject(createAbortError());
198+
}
197199

198-
return;
199-
}
200+
return (contentPromise ??= new Promise<string>((resolve, reject) => {
201+
rejectContent = reject;
200202

201203
// Defer rendering to the next event loop iteration to avoid blocking, as most operations in `renderInternal` are synchronous.
202204
renderTimeoutId = setTimeout(() => {
@@ -206,7 +208,9 @@ export async function renderAngular(
206208
applicationRef.destroyed ||
207209
signal?.aborted
208210
) {
211+
rejectContent = undefined;
209212
void destroy();
213+
reject(createAbortError());
210214

211215
return;
212216
}
@@ -224,7 +228,8 @@ export async function renderAngular(
224228
},
225229
);
226230
}, 0);
227-
}),
231+
}));
232+
},
228233
};
229234
} finally {
230235
if (hasNavigationError || redirectTo) {

‎packages/angular/ssr/test/utils/ng_spec.ts‎

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
import '@angular/compiler';
1212
/* eslint-enable import/no-unassigned-import */
1313

14-
import { Component, PlatformRef, inject, provideZonelessChangeDetection } from '@angular/core';
14+
import {
15+
ApplicationRef,
16+
Component,
17+
PlatformRef,
18+
inject,
19+
provideZonelessChangeDetection,
20+
} from '@angular/core';
1521
import { bootstrapApplication } from '@angular/platform-browser';
1622
import { setTimeout } from 'node:timers/promises';
1723
import { renderAngular } from '../../src/utils/ng';
@@ -77,28 +83,26 @@ describe('renderAngular', () => {
7783
controller.signal,
7884
);
7985

80-
expect(result.hasNavigationError).toBeFalse();
86+
expect(result).toEqual(
87+
jasmine.objectContaining({
88+
hasNavigationError: false,
89+
content: jasmine.any(Function),
90+
}),
91+
);
8192
if (!('content' in result)) {
82-
fail('Expected renderAngular result to expose content()');
83-
8493
return;
8594
}
8695

8796
controller.abort('client disconnected');
8897

89-
let caughtError: unknown;
90-
try {
91-
await result.content();
92-
fail('Expected content() to reject with AbortError');
93-
} catch (error) {
94-
caughtError = error;
95-
}
96-
97-
expect(caughtError).toBeInstanceOf(DOMException);
98-
expect((caughtError as DOMException).name).toBe('AbortError');
99-
expect((caughtError as DOMException).message).toContain(
98+
const contentPromise = result.content();
99+
await expectAsync(contentPromise).toBeRejectedWithError(
100+
DOMException,
100101
'Request for: http://localhost/ was aborted.\nclient disconnected',
101102
);
103+
await expectAsync(contentPromise).toBeRejectedWith(
104+
jasmine.objectContaining({ name: 'AbortError' }),
105+
);
102106

103107
await setTimeout(0);
104108
expect(destroySpy).toHaveBeenCalledTimes(1);
@@ -130,9 +134,13 @@ describe('renderAngular', () => {
130134
'ssr',
131135
);
132136

137+
expect(result).toEqual(
138+
jasmine.objectContaining({
139+
content: jasmine.any(Function),
140+
destroy: jasmine.any(Function),
141+
}),
142+
);
133143
if (!('content' in result)) {
134-
fail('Expected renderAngular result to expose content()');
135-
136144
return;
137145
}
138146

@@ -171,9 +179,8 @@ describe('renderAngular', () => {
171179
'ssr',
172180
);
173181

182+
expect(result).toEqual(jasmine.objectContaining({ destroy: jasmine.any(Function) }));
174183
if (!('destroy' in result)) {
175-
fail('Expected renderAngular result to expose destroy()');
176-
177184
return;
178185
}
179186

@@ -185,4 +192,92 @@ describe('renderAngular', () => {
185192
await result.destroy();
186193
expect(destroySpy).toHaveBeenCalledTimes(1);
187194
});
195+
196+
it('should reuse the in-flight promise when content() is called concurrently', async () => {
197+
let destroySpy: jasmine.Spy | undefined;
198+
199+
@Component({
200+
selector: 'app-root',
201+
template: 'Concurrent content works',
202+
})
203+
class AppComponent {
204+
constructor() {
205+
destroySpy = spyOn(inject(PlatformRef), 'destroy').and.callThrough();
206+
}
207+
}
208+
209+
const result = await renderAngular(
210+
BASE_HTML,
211+
(context) =>
212+
bootstrapApplication(
213+
AppComponent,
214+
{ providers: [provideZonelessChangeDetection()] },
215+
context,
216+
),
217+
new URL('http://localhost/'),
218+
[],
219+
'ssr',
220+
);
221+
222+
expect(result).toEqual(jasmine.objectContaining({ content: jasmine.any(Function) }));
223+
if (!('content' in result)) {
224+
return;
225+
}
226+
227+
const p1 = result.content();
228+
const p2 = result.content();
229+
expect(p1).toBe(p2);
230+
231+
const [html1, html2] = await Promise.all([p1, p2]);
232+
expect(html1).toContain('Concurrent content works');
233+
expect(html2).toBe(html1);
234+
235+
await setTimeout(0);
236+
expect(destroySpy).toHaveBeenCalledTimes(1);
237+
});
238+
239+
it('should reject content() with AbortError if applicationRef is destroyed before rendering runs', async () => {
240+
let appRef: ApplicationRef | undefined;
241+
let destroySpy: jasmine.Spy | undefined;
242+
243+
@Component({
244+
selector: 'app-root',
245+
template: 'External app destroy works',
246+
})
247+
class AppComponent {
248+
constructor() {
249+
appRef = inject(ApplicationRef);
250+
destroySpy = spyOn(inject(PlatformRef), 'destroy').and.callThrough();
251+
}
252+
}
253+
254+
const result = await renderAngular(
255+
BASE_HTML,
256+
(context) =>
257+
bootstrapApplication(
258+
AppComponent,
259+
{ providers: [provideZonelessChangeDetection()] },
260+
context,
261+
),
262+
new URL('http://localhost/'),
263+
[],
264+
'ssr',
265+
);
266+
267+
expect(result).toEqual(jasmine.objectContaining({ content: jasmine.any(Function) }));
268+
if (!('content' in result)) {
269+
return;
270+
}
271+
272+
const contentPromise = result.content();
273+
appRef?.destroy();
274+
275+
await expectAsync(contentPromise).toBeRejectedWithError(
276+
DOMException,
277+
/Request for: http:\/\/localhost\/ was aborted/,
278+
);
279+
280+
await setTimeout(0);
281+
expect(destroySpy).toHaveBeenCalledTimes(1);
282+
});
188283
});

0 commit comments

Comments
 (0)