0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

fix(i18n): manual routing with rewrite ()

This commit is contained in:
Emanuele Stoppa 2024-12-11 15:23:50 +00:00 committed by GitHub
parent f1f3bc0432
commit ccc5ad1676
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 27 additions and 5 deletions
.changeset
packages/astro
src
i18n
virtual-modules
test
fixtures/i18n-routing-manual-with-default-middleware
i18n-routing-manual-with-default-middleware.test.js

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes an issue where Astro couldn't correctly handle i18n fallback when using the i18n middleware

View file

@ -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:

View file

@ -83,7 +83,6 @@ export function createI18nMiddleware(
}
const { currentLocale } = context;
switch (i18n.strategy) {
// NOTE: theoretically, we should never hit this code path
case 'manual': {

View file

@ -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);
};

View file

@ -9,6 +9,9 @@ export default defineConfig({
codes: ["es", "es-ar"]
}
],
routing: "manual"
routing: "manual",
fallback: {
it: 'en'
}
}
})

View file

@ -18,5 +18,6 @@ export const onRequest = sequence(
customLogic,
middleware({
prefixDefaultLocale: true,
fallbackType: "rewrite"
})
);

View file

@ -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/');
});
});