{"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/d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca/packages/astro/src/virtual-modules/i18n.ts#L384-L420","documentation":"Thrown (code `InvalidI18nMiddlewareConfiguration`) when the user-provided i18n middleware options set `prefixDefaultLocale: false` together with `redirectToDefaultLocale: true`. Without a prefix on the default locale, redirecting to it has no distinct URL to redirect to, producing infinite redirect loops. The check guards a combination the type system also forbids (`@ts-expect-error` marks the runtime check).","triggerScenarios":"Calling `middleware({ prefixDefaultLocale: false, redirectToDefaultLocale: true })` imported from `astro:i18n`. This branch only exists inside the `i18n.routing === 'manual'` middleware factory, so it requires manual routing to be enabled.","commonSituations":"Copy-pasting middleware options from a project that prefixed the default locale into one that does not. Disabling `prefixDefaultLocale` to serve the default locale at `/` but forgetting to also disable `redirectToDefaultLocale`. Misunderstanding that the two options are coupled.","solutions":["Set both options to false: `middleware({ prefixDefaultLocale: false, redirectToDefaultLocale: false })` — serve the default locale at root without redirect.","Or set both to true: `middleware({ prefixDefaultLocale: true, redirectToDefaultLocale: true })` — prefix every locale including the default.","Pick one consistent pair; never mix false/true for these two."],"exampleFix":"// before\nimport { middleware } from 'astro:i18n';\nexport const onRequest = middleware({ prefixDefaultLocale: false, redirectToDefaultLocale: true });\n\n// after\nexport const onRequest = middleware({ prefixDefaultLocale: false, redirectToDefaultLocale: false });","handlingStrategy":"validation","validationCode":"function validateMiddlewareOptions(opts) {\n  if (opts.prefixDefaultLocale === false && opts.redirectToDefaultLocale === true) {\n    throw new Error('redirectToDefaultLocale requires prefixDefaultLocale: true');\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never set prefixDefaultLocale:false with redirectToDefaultLocale:true.","Treat the two options as a coupled pair: both true or both false.","Add a config test that asserts your middleware options do not violate the invariant."],"tags":["i18n","middleware","configuration","redirect"],"backgroundTag":null,"analyzedSha":"d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca","analyzedAt":"2026-08-12T13:37:29.035Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}