Skip to content

Commit c0bb10a

Browse files
committed
fix(@angular/ssr): support custom headers in redirect responses
Updates createRedirectResponse to accept an optional Record<string, string> of headers, allowing custom headers to be merged into the redirect response. The Location and Vary: X-Forwarded-Prefix headers are automatically set to ensure correct routing and proxy behavior. AngularServerApp now passes relevant headers from the matched route or response context when creating a redirect.
1 parent cdbf7a1 commit c0bb10a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/angular/ssr/src/app.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class AngularServerApp {
190190
return null;
191191
}
192192

193-
const { redirectTo, status, renderMode } = matchedRoute;
193+
const { redirectTo, status, renderMode, headers } = matchedRoute;
194194

195195
if (redirectTo !== undefined) {
196196
return createRedirectResponse(
@@ -199,6 +199,7 @@ export class AngularServerApp {
199199
buildPathWithParams(redirectTo, url.pathname),
200200
),
201201
status,
202+
headers,
202203
);
203204
}
204205

@@ -352,7 +353,7 @@ export class AngularServerApp {
352353
}
353354

354355
if (result.redirectTo) {
355-
return createRedirectResponse(result.redirectTo, responseInit.status);
356+
return createRedirectResponse(result.redirectTo, responseInit.status, headers);
356357
}
357358

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

packages/angular/ssr/src/utils/redirect.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ export function isValidRedirectResponseCode(code: number): boolean {
2727
* @param location - The URL to which the response should redirect.
2828
* @param status - The HTTP status code for the redirection. Defaults to 302 (Found).
2929
* See: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect_static#status
30+
* @param headers - Additional headers to include in the response.
3031
* @returns A `Response` object representing the HTTP redirect.
3132
*/
32-
export function createRedirectResponse(location: string, status = 302): Response {
33+
export function createRedirectResponse(
34+
location: string,
35+
status = 302,
36+
headers?: Record<string, string>,
37+
): Response {
3338
if (ngDevMode && !isValidRedirectResponseCode(status)) {
3439
throw new Error(
3540
`Invalid redirect status code: ${status}. ` +
@@ -40,8 +45,8 @@ export function createRedirectResponse(location: string, status = 302): Response
4045
return new Response(null, {
4146
status,
4247
headers: {
48+
...headers,
4349
'Location': location,
44-
'Cache-Control': 'no-store',
4550
'Vary': 'X-Forwarded-Prefix',
4651
},
4752
});

0 commit comments

Comments
 (0)