Skip to content

fix(@angular/ssr): redirect to locale pathname instead of full URL #29518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2025
Merged
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
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