From 93993b77cf4915b4c0d245df9ecbf2265f5893e7 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Mon, 1 Jul 2024 14:42:07 +0100 Subject: [PATCH] fix(i18n): update strategy when defining manually astro i18n middleware (#11362) --- .changeset/brave-mayflies-share.md | 5 ++++ packages/astro/src/i18n/index.ts | 2 ++ packages/astro/src/virtual-modules/i18n.ts | 6 +++-- .../src/pages/en/start.astro | 7 +++++- ...ing-manual-with-default-middleware.test.js | 23 +++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 .changeset/brave-mayflies-share.md diff --git a/.changeset/brave-mayflies-share.md b/.changeset/brave-mayflies-share.md new file mode 100644 index 000000000000..d755fb778ce4 --- /dev/null +++ b/.changeset/brave-mayflies-share.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where creating manually the i18n middleware could break the logic of the functions of the virtual module `astro:i18n` diff --git a/packages/astro/src/i18n/index.ts b/packages/astro/src/i18n/index.ts index 5af618be0d1f..28ddfcf515db 100644 --- a/packages/astro/src/i18n/index.ts +++ b/packages/astro/src/i18n/index.ts @@ -24,6 +24,7 @@ export function requestIs404Or500(request: Request, base = '') { return url.pathname.startsWith(`${base}/404`) || url.pathname.startsWith(`${base}/500`); } + // Checks if the pathname has any locale export function pathHasLocale(path: string, locales: Locales): boolean { const segments = path.split('/'); @@ -70,6 +71,7 @@ type GetLocaleAbsoluteUrl = GetLocaleRelativeUrl & { site: AstroConfig['site']; isBuild: boolean; }; + /** * The base URL */ diff --git a/packages/astro/src/virtual-modules/i18n.ts b/packages/astro/src/virtual-modules/i18n.ts index a0a07f1f0e7e..f7365ea62465 100644 --- a/packages/astro/src/virtual-modules/i18n.ts +++ b/packages/astro/src/virtual-modules/i18n.ts @@ -11,6 +11,7 @@ import * as I18nInternals from '../i18n/index.js'; import type { RedirectToFallback } from '../i18n/index.js'; import { toRoutingStrategy } from '../i18n/utils.js'; import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js'; + export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js'; const { trailingSlash, format, site, i18n, isBuild } = @@ -19,7 +20,7 @@ const { trailingSlash, format, site, i18n, isBuild } = const { defaultLocale, locales, domains, fallback, routing } = i18n!; const base = import.meta.env.BASE_URL; -const strategy = toRoutingStrategy(routing, domains); +let strategy = toRoutingStrategy(routing, domains); export type GetLocaleOptions = I18nInternals.GetLocaleOptions; @@ -372,10 +373,11 @@ export let middleware: (customOptions: NewAstroRoutingConfigWithoutManual) => Mi if (i18n?.routing === 'manual') { middleware = (customOptions: NewAstroRoutingConfigWithoutManual) => { + strategy = toRoutingStrategy(customOptions, {}); const manifest: SSRManifest['i18n'] = { ...i18n, fallback: undefined, - strategy: toRoutingStrategy(customOptions, {}), + strategy, domainLookupTable: {}, }; return I18nInternals.createMiddleware(manifest, base, trailingSlash, format); diff --git a/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/pages/en/start.astro b/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/pages/en/start.astro index d9f61aa025c1..9c7c9b12d659 100644 --- a/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/pages/en/start.astro +++ b/packages/astro/test/fixtures/i18n-routing-manual-with-default-middleware/src/pages/en/start.astro @@ -1,8 +1,13 @@ +--- +import {getRelativeLocaleUrl} from "astro:i18n"; + +const customUrl = getRelativeLocaleUrl("en", "/blog/title") +--- Astro -Hello +Hello

{customUrl}

diff --git a/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js b/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js index 1e609f7487b1..4c3f24489c12 100644 --- a/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js +++ b/packages/astro/test/i18n-routing-manual-with-default-middleware.test.js @@ -35,6 +35,14 @@ describe('Dev server manual routing', () => { const text = await response.text(); assert.equal(text.includes('ABOUT ME'), true); }); + + it('should correctly print the relative locale url', async () => { + const response = await fixture.fetch('/en/start'); + assert.equal(response.status, 200); + const html = await response.text(); + const $ = cheerio.load(html); + assert.equal($('p').text(), '/en/blog/title/'); + }); }); // // // SSG @@ -61,6 +69,12 @@ describe('SSG manual routing', () => { let $ = cheerio.load(html); assert.equal($('body').text().includes('ABOUT ME'), true); }); + + it('should correctly print the relative locale url', async () => { + const html = await fixture.readFile('/en/start/index.html'); + const $ = cheerio.load(html); + assert.equal($('p').text(), '/en/blog/title/'); + }); }); // // SSR @@ -94,4 +108,13 @@ describe('SSR manual routing', () => { const text = await response.text(); assert.equal(text.includes('ABOUT ME'), true); }); + + it('should correctly print the relative locale url', async () => { + let request = new Request('http://example.com/en/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/'); + }); });