mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
fix(i18n): manual routing with rewrite (#12718)
This commit is contained in:
parent
f1f3bc0432
commit
ccc5ad1676
7 changed files with 27 additions and 5 deletions
5
.changeset/fuzzy-windows-cover.md
Normal file
5
.changeset/fuzzy-windows-cover.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes an issue where Astro couldn't correctly handle i18n fallback when using the i18n middleware
|
|
@ -298,9 +298,14 @@ export function redirectToDefaultLocale({
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: public function exported to the users via `astro:i18n` module
|
// 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 {
|
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;
|
const url = context.url;
|
||||||
// We return a 404 if:
|
// We return a 404 if:
|
||||||
|
|
|
@ -83,7 +83,6 @@ export function createI18nMiddleware(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { currentLocale } = context;
|
const { currentLocale } = context;
|
||||||
|
|
||||||
switch (i18n.strategy) {
|
switch (i18n.strategy) {
|
||||||
// NOTE: theoretically, we should never hit this code path
|
// NOTE: theoretically, we should never hit this code path
|
||||||
case 'manual': {
|
case 'manual': {
|
||||||
|
|
|
@ -378,10 +378,10 @@ if (i18n?.routing === 'manual') {
|
||||||
fallbackType = toFallbackType(customOptions);
|
fallbackType = toFallbackType(customOptions);
|
||||||
const manifest: SSRManifest['i18n'] = {
|
const manifest: SSRManifest['i18n'] = {
|
||||||
...i18n,
|
...i18n,
|
||||||
fallback: undefined,
|
|
||||||
strategy,
|
strategy,
|
||||||
domainLookupTable: {},
|
domainLookupTable: {},
|
||||||
fallbackType,
|
fallbackType,
|
||||||
|
fallback: i18n.fallback,
|
||||||
};
|
};
|
||||||
return I18nInternals.createMiddleware(manifest, base, trailingSlash, format);
|
return I18nInternals.createMiddleware(manifest, base, trailingSlash, format);
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,6 +9,9 @@ export default defineConfig({
|
||||||
codes: ["es", "es-ar"]
|
codes: ["es", "es-ar"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
routing: "manual"
|
routing: "manual",
|
||||||
|
fallback: {
|
||||||
|
it: 'en'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -18,5 +18,6 @@ export const onRequest = sequence(
|
||||||
customLogic,
|
customLogic,
|
||||||
middleware({
|
middleware({
|
||||||
prefixDefaultLocale: true,
|
prefixDefaultLocale: true,
|
||||||
|
fallbackType: "rewrite"
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
|
@ -117,4 +117,13 @@ describe('SSR manual routing', () => {
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
assert.equal($('p').text(), '/en/blog/title/');
|
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/');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue