Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/angular/ssr/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export class AngularServerApp {
}

if (result.redirectTo) {
return createRedirectResponse(result.redirectTo, responseInit.status, headers);
return createRedirectResponse(result.redirectTo, responseInit.status, responseInit.headers);
}

if (renderMode === RenderMode.Prerender) {
Expand Down
8 changes: 5 additions & 3 deletions packages/angular/ssr/src/utils/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
/**
* An set of HTTP status codes that are considered valid for redirect responses.
*/
export const VALID_REDIRECT_RESPONSE_CODES: Set<number> = new Set([301, 302, 303, 307, 308]);
export const VALID_REDIRECT_RESPONSE_CODES: ReadonlySet<number> = new Set([
301, 302, 303, 307, 308,
]);

/**
* Checks if the given HTTP status code is a valid redirect response code.
Expand All @@ -33,7 +35,7 @@ export function isValidRedirectResponseCode(code: number): boolean {
export function createRedirectResponse(
location: string,
status = 302,
headers?: Record<string, string>,
headers?: Record<string, string> | Headers,
): Response {
if (ngDevMode && !isValidRedirectResponseCode(status)) {
throw new Error(
Expand All @@ -42,7 +44,7 @@ export function createRedirectResponse(
);
}

const resHeaders = new Headers(headers);
const resHeaders = headers instanceof Headers ? headers : new Headers(headers);
Comment thread
alan-agius4 marked this conversation as resolved.
if (ngDevMode && resHeaders.has('location')) {
// eslint-disable-next-line no-console
console.warn(
Expand Down
5 changes: 5 additions & 0 deletions packages/angular/ssr/test/app_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ describe('AngularServerApp', () => {
const responseInit = inject(RESPONSE_INIT);
if (responseInit) {
responseInit.status = 308;
const headers = responseInit.headers;
if (headers) {
(headers as Headers).set('X-Redirect-Header', 'custom-value');
}
}

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

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