Skip to content

Commit ccc5ad1

Browse files
authored
fix(i18n): manual routing with rewrite (#12718)
1 parent f1f3bc0 commit ccc5ad1

File tree

7 files changed

+27
-5
lines changed

7 files changed

+27
-5
lines changed

.changeset/fuzzy-windows-cover.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes an issue where Astro couldn't correctly handle i18n fallback when using the i18n middleware

packages/astro/src/i18n/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,14 @@ export function redirectToDefaultLocale({
298298
}
299299

300300
// NOTE: public function exported to the users via `astro:i18n` module
301-
export function notFound({ base, locales }: MiddlewarePayload) {
301+
export function notFound({ base, locales, fallback }: MiddlewarePayload) {
302302
return function (context: APIContext, response?: Response): Response | undefined {
303-
if (response?.headers.get(REROUTE_DIRECTIVE_HEADER) === 'no') return response;
303+
if (
304+
response?.headers.get(REROUTE_DIRECTIVE_HEADER) === 'no' &&
305+
typeof fallback === 'undefined'
306+
) {
307+
return response;
308+
}
304309

305310
const url = context.url;
306311
// We return a 404 if:

packages/astro/src/i18n/middleware.ts

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ export function createI18nMiddleware(
8383
}
8484

8585
const { currentLocale } = context;
86-
8786
switch (i18n.strategy) {
8887
// NOTE: theoretically, we should never hit this code path
8988
case 'manual': {

packages/astro/src/virtual-modules/i18n.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ if (i18n?.routing === 'manual') {
378378
fallbackType = toFallbackType(customOptions);
379379
const manifest: SSRManifest['i18n'] = {
380380
...i18n,
381-
fallback: undefined,
382381
strategy,
383382
domainLookupTable: {},
384383
fallbackType,
384+
fallback: i18n.fallback,
385385
};
386386
return I18nInternals.createMiddleware(manifest, base, trailingSlash, format);
387387
};

packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/astro.config.mjs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export default defineConfig({
99
codes: ["es", "es-ar"]
1010
}
1111
],
12-
routing: "manual"
12+
routing: "manual",
13+
fallback: {
14+
it: 'en'
15+
}
1316
}
1417
})

packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/middleware.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ export const onRequest = sequence(
1818
customLogic,
1919
middleware({
2020
prefixDefaultLocale: true,
21+
fallbackType: "rewrite"
2122
})
2223
);

packages/astro/test/i18n-routing-manual-with-default-middleware.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,13 @@ describe('SSR manual routing', () => {
117117
const $ = cheerio.load(html);
118118
assert.equal($('p').text(), '/en/blog/title/');
119119
});
120+
121+
it('should use the fallback', async () => {
122+
let request = new Request('http://example.com/it/start');
123+
let response = await app.render(request);
124+
assert.equal(response.status, 200);
125+
const html = await response.text();
126+
const $ = cheerio.load(html);
127+
assert.equal($('p').text(), '/en/blog/title/');
128+
});
120129
});

0 commit comments

Comments
 (0)