Skip to content

Commit

Permalink
fix(i18n): fallback SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 11, 2024
1 parent cdd0c7c commit 24123a5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
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
6 changes: 5 additions & 1 deletion packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ export function createI18nMiddleware(
// 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 = url.pathname.replace(`/${urlLocale}`, ``);
if (url.pathname.includes(`${base}`)) {
newPathname = url.pathname.replace(`/${urlLocale}`, ``);
} else {
newPathname = url.pathname.replace(`/${urlLocale}`, `/`);
}
} else {
newPathname = url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
}
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 @@ -1872,3 +1872,36 @@ describe('i18n routing does not break assets and endpoints', () => {
});
});
});

describe.only('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.only('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'), '/');
});
});

0 comments on commit 24123a5

Please sign in to comment.