Skip to content

Commit 22da66e

Browse files
committed
fix(@angular/ssr): preserve response headers during redirect
Previously, when a redirect was triggered, any custom headers (such as cookies or cache control) configured on the RESPONSE_INIT object by components or guards were lost because the redirect response was created with the default headers only. Closes angular#33473
1 parent 010cef6 commit 22da66e

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

packages/angular/ssr/src/app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,11 @@ export class AngularServerApp {
353353
}
354354

355355
if (result.redirectTo) {
356-
return createRedirectResponse(result.redirectTo, responseInit.status, headers);
356+
return createRedirectResponse(
357+
result.redirectTo,
358+
responseInit.status,
359+
Object.fromEntries(responseInit.headers),
360+
);
357361
}
358362

359363
if (renderMode === RenderMode.Prerender) {

packages/angular/ssr/test/app_spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ describe('AngularServerApp', () => {
4242
const responseInit = inject(RESPONSE_INIT);
4343
if (responseInit) {
4444
responseInit.status = 308;
45+
if (responseInit.headers && typeof (responseInit.headers as Headers).set === 'function') {
46+
(responseInit.headers as Headers).set('X-Redirect-Header', 'custom-value');
47+
}
4548
}
4649

4750
void inject(Router).navigate([], {
@@ -326,6 +329,7 @@ describe('AngularServerApp', () => {
326329
const response = await app.handle(new Request('http://localhost/redirect-via-navigate'));
327330
expect(response?.headers.get('location')).toBe('/redirect-via-navigate?filter=test');
328331
expect(response?.status).toBe(308);
332+
expect(response?.headers.get('X-Redirect-Header')).toBe('custom-value');
329333
});
330334

331335
it('returns a 302 status and redirects to the correct location when `urlTree` is updated in a guard', async () => {

0 commit comments

Comments
 (0)