Skip to content

Commit 747557a

Browse files
committed
fix(@angular/ssr): redirect to locale pathname instead of full URL
When redirecting to the preferred locale, the previous implementation used the full URL for the 302 redirect to i18n subpaths based on the user's preferred locale. This update ensures that the redirect now uses the locale-specific pathname instead of the full URL. Closes #29514 (cherry picked from commit 46581db)
1 parent 7532aca commit 747557a

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ export class AngularAppEngine {
9797
const { basePath, supportedLocales } = this.manifest;
9898

9999
// If the request is not for the base path, it's not our responsibility to handle it.
100-
const url = new URL(request.url);
101-
if (url.pathname !== basePath) {
100+
const { pathname } = new URL(request.url);
101+
if (pathname !== basePath) {
102102
return null;
103103
}
104104

@@ -112,12 +112,10 @@ export class AngularAppEngine {
112112
if (preferredLocale) {
113113
const subPath = supportedLocales[preferredLocale];
114114
if (subPath !== undefined) {
115-
url.pathname = joinUrlParts(url.pathname, subPath);
116-
117115
return new Response(null, {
118116
status: 302, // Use a 302 redirect as language preference may change.
119117
headers: {
120-
'Location': url.toString(),
118+
'Location': joinUrlParts(pathname, subPath),
121119
'Vary': 'Accept-Language',
122120
},
123121
});

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ describe('AngularAppEngine', () => {
157157
});
158158

159159
it('should redirect to the highest priority locale when the URL is "/"', async () => {
160-
const request = new Request('https://example.com/', {
160+
const request = new Request('https://example.com', {
161161
headers: { 'Accept-Language': 'fr-CH, fr;q=0.9, it;q=0.8, en;q=0.7, *;q=0.5' },
162162
});
163163
const response = await appEngine.handle(request);
164164
expect(response?.status).toBe(302);
165-
expect(response?.headers.get('Location')).toBe('https://example.com/it');
165+
expect(response?.headers.get('Location')).toBe('/it');
166166
expect(response?.headers.get('Vary')).toBe('Accept-Language');
167167
});
168168

0 commit comments

Comments
 (0)