diff options
author | 2024-07-01 14:42:07 +0100 | |
---|---|---|
committer | 2024-07-01 14:42:07 +0100 | |
commit | 93993b77cf4915b4c0d245df9ecbf2265f5893e7 (patch) | |
tree | 2cab9bc313d81ab4fcd81e51efa67fea30677d89 | |
parent | 3a223b4811708cc93ebb27706118c1723e1fc013 (diff) | |
download | astro-93993b77cf4915b4c0d245df9ecbf2265f5893e7.tar.gz astro-93993b77cf4915b4c0d245df9ecbf2265f5893e7.tar.zst astro-93993b77cf4915b4c0d245df9ecbf2265f5893e7.zip |
fix(i18n): update strategy when defining manually astro i18n middleware (#11362)
5 files changed, 40 insertions, 3 deletions
diff --git a/.changeset/brave-mayflies-share.md b/.changeset/brave-mayflies-share.md new file mode 100644 index 000000000..d755fb778 --- /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 5af618be0..28ddfcf51 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 a0a07f1f0..f7365ea62 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 d9f61aa02..9c7c9b12d 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") +--- <html> <head> <title>Astro</title> </head> <body> -Hello +Hello <p>{customUrl}</p> </body> </html> 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 1e609f748..4c3f24489 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/'); + }); }); |