diff options
author | 2025-04-04 08:47:33 +0100 | |
---|---|---|
committer | 2025-04-04 08:47:33 +0100 | |
commit | 9cd0fd432634ed664a820ac78c6a3033684c7a83 (patch) | |
tree | 4073956a3ae1dba6d72072603fa9d8f28ca934c1 | |
parent | a7aff41681f9235719c03f97650db288f9f5f71a (diff) | |
download | astro-9cd0fd432634ed664a820ac78c6a3033684c7a83.tar.gz astro-9cd0fd432634ed664a820ac78c6a3033684c7a83.tar.zst astro-9cd0fd432634ed664a820ac78c6a3033684c7a83.zip |
fix(i18n): incorrect validation (#13552)
* fix(i18n): incorrect validation
* Update packages/astro/src/core/config/schemas/refined.ts
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* Update .changeset/old-animals-shop.md
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
* update tests
* update fixture
* track default value too
* remove validation
---------
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
Co-authored-by: delucis <357379+delucis@users.noreply.github.com>
6 files changed, 31 insertions, 17 deletions
diff --git a/.changeset/old-animals-shop.md b/.changeset/old-animals-shop.md new file mode 100644 index 000000000..684336b29 --- /dev/null +++ b/.changeset/old-animals-shop.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where Astro validated the i18n configuration incorrectly, causing false positives in downstream libraries. diff --git a/packages/astro/src/core/config/schemas/base.ts b/packages/astro/src/core/config/schemas/base.ts index 194f23a47..1776785b9 100644 --- a/packages/astro/src/core/config/schemas/base.ts +++ b/packages/astro/src/core/config/schemas/base.ts @@ -402,6 +402,7 @@ export const AstroConfigSchema = z.object({ .or( z.object({ prefixDefaultLocale: z.boolean().optional().default(false), + // TODO: Astro 6.0 change to false redirectToDefaultLocale: z.boolean().optional().default(true), fallbackType: z.enum(['redirect', 'rewrite']).optional().default('redirect'), }), diff --git a/packages/astro/src/core/config/schemas/refined.ts b/packages/astro/src/core/config/schemas/refined.ts index efb8bd233..25e058cf4 100644 --- a/packages/astro/src/core/config/schemas/refined.ts +++ b/packages/astro/src/core/config/schemas/refined.ts @@ -42,19 +42,21 @@ export const AstroConfigRefinedSchema = z.custom<AstroConfig>().superRefine((con } } - if ( - config.i18n && - typeof config.i18n.routing !== 'string' && - !config.i18n.routing.redirectToDefaultLocale && - !config.i18n.routing.prefixDefaultLocale - ) { - ctx.addIssue({ - code: z.ZodIssueCode.custom, - message: - 'The option `i18n.routing.redirectToDefaultLocale` is only useful when the `i18n.routing.prefixDefaultLocale` is set to `true`. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `true`.', - path: ['i18n', 'routing', 'redirectToDefaultLocale'], - }); - } + // TODO: Astro 6.0 + // Uncomment this validation check, and change the default value of redirectToDefaultLocale to false + // if ( + // config.i18n && + // typeof config.i18n.routing !== 'string' && + // config.i18n.routing.prefixDefaultLocale === false && + // config.i18n.routing.redirectToDefaultLocale === true + // ) { + // ctx.addIssue({ + // code: z.ZodIssueCode.custom, + // message: + // 'The option `i18n.routing.redirectToDefaultLocale` can be used only when `i18n.routing.prefixDefaultLocale` is set to `true`, otherwise redirects might cause infinite loops. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `false`.', + // path: ['i18n', 'routing', 'redirectToDefaultLocale'], + // }); + // } if (config.outDir.toString().startsWith(config.publicDir.toString())) { ctx.addIssue({ diff --git a/packages/astro/src/integrations/hooks.ts b/packages/astro/src/integrations/hooks.ts index 172579297..68b782760 100644 --- a/packages/astro/src/integrations/hooks.ts +++ b/packages/astro/src/integrations/hooks.ts @@ -346,6 +346,12 @@ export async function runHookConfigSetup({ if (astroJSXRenderer) { updatedSettings.renderers.push(astroJSXRenderer); } + + // TODO: Astro 6.0 + // Remove this hack to avoid breaking changes, and change the default value of redirectToDefaultLocale + if (updatedConfig.i18n && typeof updatedConfig.i18n.routing !== "string") { + updatedConfig.i18n.routing.redirectToDefaultLocale ??= updatedConfig.i18n.routing.prefixDefaultLocale || false; + } updatedSettings.config = updatedConfig; return updatedSettings; diff --git a/packages/astro/test/fixtures/i18n-routing-prefix-always/astro.config.mjs b/packages/astro/test/fixtures/i18n-routing-prefix-always/astro.config.mjs index cc445b396..6ab4d1305 100644 --- a/packages/astro/test/fixtures/i18n-routing-prefix-always/astro.config.mjs +++ b/packages/astro/test/fixtures/i18n-routing-prefix-always/astro.config.mjs @@ -10,7 +10,7 @@ export default defineConfig({ } ], routing: { - prefixDefaultLocale: true + prefixDefaultLocale: true, } }, base: "/new-site" diff --git a/packages/astro/test/units/config/config-validate.test.js b/packages/astro/test/units/config/config-validate.test.js index 1874eaf45..0bb275d42 100644 --- a/packages/astro/test/units/config/config-validate.test.js +++ b/packages/astro/test/units/config/config-validate.test.js @@ -195,21 +195,21 @@ describe('Config Validation', () => { ); }); - it('errors if `i18n.prefixDefaultLocale` is `false` and `i18n.redirectToDefaultLocale` is `true`', async () => { + it('errors if `i18n.prefixDefaultLocale` is `false` and `i18n.redirectToDefaultLocale` is `true`', { todo: "Enable in Astro 6.0", skip: "Removed validation"}, async () => { const configError = await validateConfig({ i18n: { defaultLocale: 'en', locales: ['es', 'en'], routing: { prefixDefaultLocale: false, - redirectToDefaultLocale: false, + redirectToDefaultLocale: true, }, }, }).catch((err) => err); assert.equal(configError instanceof z.ZodError, true); assert.equal( configError.errors[0].message, - 'The option `i18n.routing.redirectToDefaultLocale` is only useful when the `i18n.routing.prefixDefaultLocale` is set to `true`. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `true`.', + 'The option `i18n.routing.redirectToDefaultLocale` can be used only when `i18n.routing.prefixDefaultLocale` is set to `true`, otherwise redirects might cause infinite loops. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `false`.' ); }); |