Skip to content

Commit 24123a5

Browse files
committed
fix(i18n): fallback SSR
1 parent cdd0c7c commit 24123a5

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-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/middleware.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ export function createI18nMiddleware(
179179
// If a locale falls back to the default locale, we want to **remove** the locale because
180180
// the default locale doesn't have a prefix
181181
if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') {
182-
newPathname = url.pathname.replace(`/${urlLocale}`, ``);
182+
if (url.pathname.includes(`${base}`)) {
183+
newPathname = url.pathname.replace(`/${urlLocale}`, ``);
184+
} else {
185+
newPathname = url.pathname.replace(`/${urlLocale}`, `/`);
186+
}
183187
} else {
184188
newPathname = url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
185189
}

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

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

0 commit comments

Comments
 (0)