Skip to content
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

fix(i18n): fallback SSR #10755

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/old-pugs-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a case where the i18n fallback faild to correctly redirect to the index when the fallback is enabled in SSR
ematipico marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 6 additions & 1 deletion packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export function redirectToFallback({
locales,
defaultLocale,
strategy,
base,
}: MiddlewarePayload) {
return function (context: APIContext, response: Response): Response {
if (response.status >= 300 && fallback) {
Expand Down Expand Up @@ -370,7 +371,11 @@ export function redirectToFallback({
// If a locale falls back to the default locale, we want to **remove** the locale because
// the default locale doesn't have a prefix
if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') {
newPathname = context.url.pathname.replace(`/${urlLocale}`, ``);
if (context.url.pathname.includes(`${base}`)) {
newPathname = context.url.pathname.replace(`/${urlLocale}`, ``);
} else {
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/`);
}
} else {
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
}
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { SSRManifestI18n } from '../core/app/types.js';
import { ROUTE_TYPE_HEADER } from '../core/constants.js';
import {
type MiddlewarePayload,
getPathByLocale,
normalizeTheLocale,
notFound,
redirectToDefaultLocale,
Expand Down
33 changes: 33 additions & 0 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1871,3 +1871,36 @@ describe('i18n routing does not break assets and endpoints', () => {
});
});
});

describe('SSR fallback from missing locale index to default locale index', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;

before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing-prefix-other-locales/',
output: 'server',
adapter: testAdapter(),
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr'],
routing: {
prefixDefaultLocale: false,
},
fallback: {
fr: 'en',
},
},
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});

it('should correctly redirect', async () => {
let request = new Request('http://example.com/fr');
let response = await app.render(request);
assert.equal(response.status, 302);
assert.equal(response.headers.get('location'), '/');
});
});
Loading