{"record":{"id":"78269821839f8fa4","repo":"withastro/astro","slug":"invalidi18nmiddlewareconfiguration","errorCode":"InvalidI18nMiddlewareConfiguration","errorMessage":"The option `redirectToDefaultLocale` can be enabled only when `prefixDefaultLocale` is also set to `true`; otherwise, redirects might cause infinite loops. Enable the option `prefixDefaultLocale` to continue to use `redirectToDefaultLocale`, or ensure both are set to `false`.","messagePattern":"The option `redirectToDefaultLocale` can be enabled only when `prefixDefaultLocale` is also set to `true`; otherwise, redirects might cause infinite loops\\. Enable the option `prefixDefaultLocale` to continue to use `redirectToDefaultLocale`, or ensure both are set to `false`\\.","errorType":"exception","errorClass":"AstroError","httpStatus":null,"severity":"error","filePath":"packages/astro/src/virtual-modules/i18n.ts","lineNumber":402,"sourceCode":" * });\n *\n * export const onRequest = sequence(customLogic, middleware({\n * \tprefixDefaultLocale: true,\n * \tredirectToDefaultLocale: false\n * }))\n *\n * ```\n */\nexport let middleware: (customOptions: I18nMiddlewareOptions) => MiddlewareHandler;\n\nif (i18n?.routing === 'manual') {\n\tmiddleware = (customOptions) => {\n\t\tif (\n\t\t\tcustomOptions.prefixDefaultLocale === false &&\n\t\t\t// @ts-expect-error types do not allow this but we also check at runtime\n\t\t\tcustomOptions.redirectToDefaultLocale === true\n\t\t) {\n\t\t\tthrow new AstroError(InvalidI18nMiddlewareConfiguration);\n\t\t}\n\t\tstrategy = toRoutingStrategy(customOptions, {});\n\t\tfallbackType = toFallbackType(customOptions);\n\t\tconst manifest: SSRManifest['i18n'] = {\n\t\t\t...i18n,\n\t\t\tstrategy,\n\t\t\tdomainLookupTable: {},\n\t\t\tfallbackType,\n\t\t\tfallback: i18n.fallback,\n\t\t\tdomains: i18n.domains,\n\t\t};\n\t\treturn I18nInternals.createMiddleware(manifest, base, trailingSlash, format);\n\t};\n} else {\n\tmiddleware = noop('middleware');\n}\n\n/**","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/withastro/astro/blob/52e6c34790cc8ac4e69e6135ace06049867e5c4a/packages/astro/src/virtual-modules/i18n.ts#L384-L420","documentation":"The i18n middleware factory (middleware() from astro:i18n) recreates Astro's i18n middleware with custom options. One combination is rejected up front: redirectToDefaultLocale: true together with prefixDefaultLocale: false. Without a prefixed default locale, redirecting / to the default locale would target the same URL and loop forever, so Astro throws InvalidI18nMiddlewareConfiguration instead. TypeScript blocks this combination at compile time; the runtime check catches untyped usage.","triggerScenarios":"Calling middleware({ prefixDefaultLocale: false, redirectToDefaultLocale: true }) in src/middleware.ts — exactly this option combination; JavaScript projects without type checking reaching the runtime guard.","commonSituations":"Moving routing options from astro.config into middleware form and flipping prefixDefaultLocale incorrectly; hand-written JS middleware bypassing the published types.","solutions":["Set prefixDefaultLocale: true when you want redirectToDefaultLocale: true","Or set both to false (no prefix, no redirect) and handle the root URL yourself","Write middleware in TypeScript (or add // @ts-check) so the invalid combination is caught at compile time"],"exampleFix":"// before — src/middleware.ts\nexport const onRequest = middleware({ prefixDefaultLocale: false, redirectToDefaultLocale: true });\n\n// after\nexport const onRequest = middleware({ prefixDefaultLocale: true, redirectToDefaultLocale: true });","handlingStrategy":"validation","validationCode":"// src/middleware.ts — validate the option combination before passing it\nconst opts = { prefixDefaultLocale: true, redirectToDefaultLocale: true };\nif (!opts.prefixDefaultLocale && (opts as any).redirectToDefaultLocale) {\n\tthrow new Error('redirectToDefaultLocale requires prefixDefaultLocale: true');\n}\nexport const onRequest = middleware(opts);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write middleware in TypeScript — the published I18nMiddlewareOptions union forbids this combination at compile time","Remember the rule: redirecting to the default locale only makes sense when the default locale has a prefix (/en)","Set both options to false when you do not want prefixes or redirects"],"tags":["i18n","routing","middleware","config","infinite-redirect"],"backgroundTag":"i18n-routing-misconfigured","analyzedSha":"52e6c34790cc8ac4e69e6135ace06049867e5c4a","analyzedAt":"2026-08-18T18:48:03.901Z","contentChangedAt":"2026-08-18T18:48:03.901Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}