Skip to content

Commit

Permalink
fix(@angular/ssr): redirect to locale pathname instead of full URL
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 committed Jan 29, 2025
1 parent 05f3680 commit 46581db
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions packages/angular/ssr/src/app-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export class AngularAppEngine {
const { basePath, supportedLocales } = this.manifest;

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

Expand All @@ -112,12 +112,10 @@ export class AngularAppEngine {
if (preferredLocale) {
const subPath = supportedLocales[preferredLocale];
if (subPath !== undefined) {
url.pathname = joinUrlParts(url.pathname, subPath);

return new Response(null, {
status: 302, // Use a 302 redirect as language preference may change.
headers: {
'Location': url.toString(),
'Location': joinUrlParts(pathname, subPath),
'Vary': 'Accept-Language',
},
});
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/ssr/test/app-engine_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ describe('AngularAppEngine', () => {
});

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

Expand Down

0 comments on commit 46581db

Please sign in to comment.