Skip to content

Commit

Permalink
fix(i18n): manual routing with rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Dec 11, 2024
1 parent dff04a4 commit 00100f2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 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
10 changes: 8 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 All @@ -310,6 +315,7 @@ export function notFound({ base, locales }: MiddlewarePayload) {
if (!(isRoot || pathHasLocale(url.pathname, locales))) {
if (response) {
response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
console.log('fallbackhere');
return new Response(response.body, {
status: 404,
headers: response.headers,
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 @@ -78,7 +78,7 @@ describe('SSG manual routing', () => {
});

// // SSR
describe('SSR manual routing', () => {
describe.only('SSR manual routing', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;
Expand Down Expand Up @@ -117,4 +117,13 @@ describe('SSR manual routing', () => {
const $ = cheerio.load(html);
assert.equal($('p').text(), '/en/blog/title/');
});

it.only('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 00100f2

Please sign in to comment.