feat: resolve default locale from runtime config in single-host multiDomainLocales setups#4003
Conversation
…DomainLocales setups multiDomainLocales resolves the per-request default locale only through build-time domain lists (domain/domains/defaultForDomains compiled into the bundle). Deployments that ship ONE build to hosts unknown at build time (dynamic preview environments, one storefront build serving many country domains) cannot use it: with no domains configured the host detector resolves nothing, detection falls through to the route detector, and on / that picks the first ___default route in baked order — the runtime defaultLocale (NUXT_PUBLIC_I18N_DEFAULT_LOCALE) is ignored even though setupMultiDomainLocales already rebuilds the route table from it. Add a single-host runtime mode to matchDomainLocale: when no locale declares domain/domains, every locale matches the current host, and after the existing defaultForDomains/domainDefault checks the unprefixed default falls back to the runtime defaultLocale. Apply the same fallback in afterSwitchLocalePath so switchLocalePath strips the prefix for the runtime default locale. Backward compatible: the new branches only engage when no domains are configured anywhere — a configuration that previously resolved nothing.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughThis PR adds an optional runtime Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
JSDoc for the extended resolution precedence and eight unit tests: existing domain matching, path-locale precedence, domainDefault fallback, and the new single-host mode (path locale, runtime default fallback, no-default undefined, disabled when any domain is declared).
🔗 Linked issue
—
📚 Description
multiDomainLocalescurrently resolves the per-request default locale exclusively through build-time domain lists (domain/domains/defaultForDomainson locale entries, compiled into#build/i18n-options). That works when every domain is known at build time, but breaks a common deployment model:one build artifact / container image, deployed to many hosts that are unknown at build time — dynamic preview/dev environments, or one storefront build serving
example.de/example.es/example.ua, each with its own default locale supplied via env.runtimeConfig.public.i18n.defaultLocaleis already runtime-overridable (NUXT_PUBLIC_I18N_DEFAULT_LOCALE), and the route table already adapts (setupMultiDomainLocalesrebuilds the unprefixed routes from the runtime value). But the host detector resolves nothing when nodomains are configured, so detection falls through to the route detector, which on/picks the first___defaultroute in baked order — the runtime default is ignored.This PR adds a single-host runtime mode to
matchDomainLocale:domain/domains, every locale is considered served by the current host (there is only one host per process).defaultForDomains/domainDefaultchecks, fall back to the runtimedefaultLocale.afterSwitchLocalePath, soswitchLocalePathstrips the prefix for the runtime default locale (otherwise it links to the removed prefixed route of the default).Backward compatible: deployments that configure domains keep the exact same resolution — the new branches only engage when no domains are configured at all, a configuration that previously could not resolve any host locale.
With strategy
prefix_except_default,multiDomainLocales: trueand build-timedefaultLocale: '', one build serves any number of environments:NUXT_PUBLIC_I18N_DEFAULT_LOCALE//de/esesdeValidated on a production storefront (Nuxt 4, four locales): one artifact, parallel processes with different runtime defaults — correct locale at
/, prefixed routes intact, internal links unprefixed for the default with no prefixed leaks.📝 Checklist
Summary by CodeRabbit