Skip to content

Commit

Permalink
fix(i18n): manual routing with rewrite (#12718)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Dec 11, 2024
1 parent f1f3bc0 commit ccc5ad1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-windows-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where Astro couldn't correctly handle i18n fallback when using the i18n middleware
9 changes: 7 additions & 2 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export function createI18nMiddleware(
}

const { currentLocale } = context;

switch (i18n.strategy) {
// NOTE: theoretically, we should never hit this code path
case 'manual': {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/virtual-modules/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default defineConfig({
codes: ["es", "es-ar"]
}
],
routing: "manual"
routing: "manual",
fallback: {
it: 'en'
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export const onRequest = sequence(
customLogic,
middleware({
prefixDefaultLocale: true,
fallbackType: "rewrite"
})
);
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
});
});

0 comments on commit ccc5ad1

Please sign in to comment.