diff --git a/.changeset/fuzzy-windows-cover.md b/.changeset/fuzzy-windows-cover.md new file mode 100644 index 000000000000..7f3b766fc0af --- /dev/null +++ b/.changeset/fuzzy-windows-cover.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where Astro couldn't correctly handle i18n fallback when using the i18n middleware diff --git a/packages/astro/src/i18n/index.ts b/packages/astro/src/i18n/index.ts index 455e2e141487..e1e3750ea40b 100644 --- a/packages/astro/src/i18n/index.ts +++ b/packages/astro/src/i18n/index.ts @@ -298,9 +298,14 @@ export function redirectToDefaultLocale({ } // NOTE: public function exported to the users via `astro:i18n` module -export function notFound({ base, locales }: MiddlewarePayload) { +export function notFound({ base, locales, fallback }: MiddlewarePayload) { return function (context: APIContext, response?: Response): Response | undefined { - if (response?.headers.get(REROUTE_DIRECTIVE_HEADER) === 'no') return response; + if ( + response?.headers.get(REROUTE_DIRECTIVE_HEADER) === 'no' && + typeof fallback === 'undefined' + ) { + return response; + } const url = context.url; // We return a 404 if: diff --git a/packages/astro/src/i18n/middleware.ts b/packages/astro/src/i18n/middleware.ts index eefb8a9dd56e..c2805b1a3266 100644 --- a/packages/astro/src/i18n/middleware.ts +++ b/packages/astro/src/i18n/middleware.ts @@ -83,7 +83,6 @@ export function createI18nMiddleware( } const { currentLocale } = context; - switch (i18n.strategy) { // NOTE: theoretically, we should never hit this code path case 'manual': { diff --git a/packages/astro/src/virtual-modules/i18n.ts b/packages/astro/src/virtual-modules/i18n.ts index 8f85ae5f6146..d2e193fd7c04 100644 --- a/packages/astro/src/virtual-modules/i18n.ts +++ b/packages/astro/src/virtual-modules/i18n.ts @@ -378,10 +378,10 @@ if (i18n?.routing === 'manual') { fallbackType = toFallbackType(customOptions); const manifest: SSRManifest['i18n'] = { ...i18n, - fallback: undefined, strategy, domainLookupTable: {}, fallbackType, + fallback: i18n.fallback, }; return I18nInternals.createMiddleware(manifest, base, trailingSlash, format); }; diff --git a/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/astro.config.mjs b/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/astro.config.mjs index 0638988f063b..8006c260f80c 100644 --- a/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/astro.config.mjs +++ b/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/astro.config.mjs @@ -9,6 +9,9 @@ export default defineConfig({ codes: ["es", "es-ar"] } ], - routing: "manual" + routing: "manual", + fallback: { + it: 'en' + } } }) diff --git a/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/middleware.js b/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/middleware.js index 60d179ec7111..afc3c6c607f9 100644 --- a/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/middleware.js +++ b/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/middleware.js @@ -18,5 +18,6 @@ export const onRequest = sequence( customLogic, middleware({ prefixDefaultLocale: true, + fallbackType: "rewrite" }) ); diff --git a/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js b/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js index af900a43b589..0b24c6aa7aa5 100644 --- a/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js +++ b/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js @@ -117,4 +117,13 @@ describe('SSR manual routing', () => { const $ = cheerio.load(html); assert.equal($('p').text(), '/en/blog/title/'); }); + + it('should use the fallback', async () => { + let request = new Request('http://example.com/it/start'); + let response = await app.render(request); + assert.equal(response.status, 200); + const html = await response.text(); + const $ = cheerio.load(html); + assert.equal($('p').text(), '/en/blog/title/'); + }); });