diff options
-rw-r--r-- | .changeset/happy-beans-protect.md | 5 | ||||
-rw-r--r-- | packages/astro/src/i18n/middleware.ts | 4 | ||||
-rw-r--r-- | packages/astro/test/i18n-routing.test.js | 29 |
3 files changed, 36 insertions, 2 deletions
diff --git a/.changeset/happy-beans-protect.md b/.changeset/happy-beans-protect.md new file mode 100644 index 000000000..cf6f8a8bc --- /dev/null +++ b/.changeset/happy-beans-protect.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix i18n fallback routing with routing strategy of always-prefix diff --git a/packages/astro/src/i18n/middleware.ts b/packages/astro/src/i18n/middleware.ts index 854a39b77..81523c25a 100644 --- a/packages/astro/src/i18n/middleware.ts +++ b/packages/astro/src/i18n/middleware.ts @@ -81,8 +81,8 @@ export function createI18nMiddleware( const fallbackLocale = fallback[urlLocale]; let newPathname: string; // 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 (fallbackLocale === defaultLocale) { + // the default locale doesn't have a prefix, but _only_ if prefix-always is false + if (fallbackLocale === defaultLocale && i18n.routingStrategy !== 'prefix-always') { newPathname = url.pathname.replace(`/${urlLocale}`, ``); } else { newPathname = url.pathname.replace(`/${urlLocale}`, `/${fallbackLocale}`); diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 52ebeda23..7fd5946e6 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -961,6 +961,35 @@ describe('[SSR] i18n routing', () => { let response = await app.render(request); expect(response.status).to.equal(404); }); + + describe('with routing strategy [prefix-always]', () => { + before(async () => { + fixture = await loadFixture({ + root: './fixtures/i18n-routing-fallback/', + output: 'server', + adapter: testAdapter(), + experimental: { + i18n: { + defaultLocale: 'en', + locales: ['en', 'pt', 'it'], + fallback: { + it: 'en', + }, + routingStrategy: 'prefix-always', + }, + }, + }); + await fixture.build(); + app = await fixture.loadTestAdapterApp(); + }); + + it('should redirect to the english locale, which is the first fallback', async () => { + let request = new Request('http://example.com/new-site/it/start'); + let response = await app.render(request); + expect(response.status).to.equal(302); + expect(response.headers.get('location')).to.equal('/new-site/en/start'); + }); + }); }); describe('preferred locale', () => { |