Skip to content

Commit ca6f08c

Browse files
authored
fix(@angular/ssr): apply forwarded prefix and vary header in accept-language redirects
1 parent 1ebdb3c commit ca6f08c

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

packages/angular/ssr/src/app-engine.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { AngularServerApp, getOrCreateAngularServerApp } from './app';
1010
import { Hooks } from './hooks';
1111
import { getPotentialLocaleIdFromUrl, getPreferredLocale } from './i18n';
1212
import { EntryPointExports, getAngularAppEngineManifest } from './manifest';
13+
import { createRedirectResponse } from './utils/redirect';
1314
import { joinUrlParts } from './utils/url';
1415
import { cloneRequestAndPatchHeaders, validateRequest } from './utils/validation';
1516

@@ -146,7 +147,7 @@ export class AngularAppEngine {
146147

147148
if (this.supportedLocales.length > 1) {
148149
// Redirect to the preferred language if i18n is enabled.
149-
return this.redirectBasedOnAcceptLanguage(request);
150+
return this.redirectBasedOnAcceptLanguage(securedRequest);
150151
}
151152

152153
return null;
@@ -179,13 +180,14 @@ export class AngularAppEngine {
179180
if (preferredLocale) {
180181
const subPath = supportedLocales[preferredLocale];
181182
if (subPath !== undefined) {
182-
return new Response(null, {
183-
status: 302, // Use a 302 redirect as language preference may change.
184-
headers: {
185-
'Location': joinUrlParts(pathname, subPath),
186-
'Vary': 'Accept-Language',
187-
},
188-
});
183+
const prefix = request.headers.get('X-Forwarded-Prefix') ?? '';
184+
185+
return createRedirectResponse(
186+
joinUrlParts(prefix, pathname, subPath),
187+
302,
188+
// Use a 302 redirect as language preference may change.
189+
{ 'Vary': 'Accept-Language' },
190+
);
189191
}
190192
}
191193

packages/angular/ssr/test/app-engine_spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,21 @@ describe('AngularAppEngine', () => {
160160
const response = await appEngine.handle(request);
161161
expect(response?.status).toBe(302);
162162
expect(response?.headers.get('Location')).toBe('/it');
163-
expect(response?.headers.get('Vary')).toBe('Accept-Language');
163+
expect(response?.headers.get('Vary')).toBe('X-Forwarded-Prefix, Accept-Language');
164+
});
165+
166+
it('should include forwarded prefix in locale redirect location when present', async () => {
167+
const request = new Request('https://example.com', {
168+
headers: {
169+
'Accept-Language': 'it',
170+
'X-Forwarded-Prefix': '/app',
171+
},
172+
});
173+
174+
const response = await appEngine.handle(request);
175+
expect(response?.status).toBe(302);
176+
expect(response?.headers.get('Location')).toBe('/app/it');
177+
expect(response?.headers.get('Vary')).toBe('X-Forwarded-Prefix, Accept-Language');
164178
});
165179

166180
it('should return null for requests to file-like resources in a locale', async () => {

0 commit comments

Comments
 (0)