Skip to content

Commit ecb2ff4

Browse files
committed
fix(i18n): fallback SSR
1 parent 66bc104 commit ecb2ff4

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

.changeset/old-pugs-jog.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Fixes a case where the i18n fallback faild to correctly redirect to the index when the fallback is enabled in SSR

packages/astro/src/i18n/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ export function redirectToFallback({
343343
locales,
344344
defaultLocale,
345345
strategy,
346+
base,
346347
}: MiddlewarePayload) {
347348
return function (context: APIContext, response: Response): Response {
348349
if (response.status >= 300 && fallback) {
@@ -370,7 +371,11 @@ export function redirectToFallback({
370371
// If a locale falls back to the default locale, we want to **remove** the locale because
371372
// the default locale doesn't have a prefix
372373
if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') {
373-
newPathname = context.url.pathname.replace(`/${urlLocale}`, ``);
374+
if (context.url.pathname.includes(`${base}`)) {
375+
newPathname = context.url.pathname.replace(`/${urlLocale}`, ``);
376+
} else {
377+
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/`);
378+
}
374379
} else {
375380
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
376381
}

packages/astro/test/i18n-routing.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -1871,3 +1871,36 @@ describe('i18n routing does not break assets and endpoints', () => {
18711871
});
18721872
});
18731873
});
1874+
1875+
describe.only('SSR fallback from missing locale index to default locale index', () => {
1876+
/** @type {import('./test-utils').Fixture} */
1877+
let fixture;
1878+
let app;
1879+
1880+
before(async () => {
1881+
fixture = await loadFixture({
1882+
root: './fixtures/i18n-routing-prefix-other-locales/',
1883+
output: 'server',
1884+
adapter: testAdapter(),
1885+
i18n: {
1886+
defaultLocale: 'en',
1887+
locales: ['en', 'fr'],
1888+
routing: {
1889+
prefixDefaultLocale: false,
1890+
},
1891+
fallback: {
1892+
fr: 'en',
1893+
},
1894+
},
1895+
});
1896+
await fixture.build();
1897+
app = await fixture.loadTestAdapterApp();
1898+
});
1899+
1900+
it.only('should correctly redirect', async () => {
1901+
let request = new Request('http://example.com/fr');
1902+
let response = await app.render(request);
1903+
assert.equal(response.status, 302);
1904+
assert.equal(response.headers.get('location'), '/');
1905+
});
1906+
});

0 commit comments

Comments
 (0)