Skip to content

feat: resolve default locale from runtime config in single-host multiDomainLocales setups#4003

Open
sequensucks wants to merge 3 commits into
nuxt-modules:mainfrom
sequensucks:feat/single-host-runtime-default-locale
Open

feat: resolve default locale from runtime config in single-host multiDomainLocales setups#4003
sequensucks wants to merge 3 commits into
nuxt-modules:mainfrom
sequensucks:feat/single-host-runtime-default-locale

Conversation

@sequensucks

@sequensucks sequensucks commented Jun 12, 2026

Copy link
Copy Markdown

🔗 Linked issue

📚 Description

multiDomainLocales currently resolves the per-request default locale exclusively through build-time domain lists (domain / domains / defaultForDomains on 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.defaultLocale is already runtime-overridable (NUXT_PUBLIC_I18N_DEFAULT_LOCALE), and the route table already adapts (setupMultiDomainLocales rebuilds the unprefixed routes from the runtime value). But the host detector resolves nothing when no domains are configured, so detection falls through to the route detector, which on / picks the first ___default route in baked order — the runtime default is ignored.

This PR adds a single-host runtime mode to matchDomainLocale:

  1. When no locale declares domain/domains, every locale is considered served by the current host (there is only one host per process).
  2. With multiple matches and no path prefix, after the existing defaultForDomains/domainDefault checks, fall back to the runtime defaultLocale.
  3. The same runtime fallback is applied in afterSwitchLocalePath, so switchLocalePath strips 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: true and build-time defaultLocale: '', one build serves any number of environments:

process env NUXT_PUBLIC_I18N_DEFAULT_LOCALE / /de /es
es es de (200) 404 (canonical unprefixed)
de de 404 es (200)

Validated 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

  • Backward compatible — new code paths are gated on "no domains configured"
  • Validated against a real multi-locale storefront build

Summary by CodeRabbit

  • Improvements
    • Enhanced locale detection to use a configurable default-locale fallback when host- or prefix-based resolution is ambiguous.
    • Updated domain matching to better support single-host (domainless) runtime behavior, including correct handling of prefixed vs unprefixed paths.
    • Improved fallback order when multiple locale candidates are available, including domain-default and default-locale precedence.
  • Tests
    • Added Vitest coverage for domain and single-host locale resolution, including default-locale fallback scenarios and mixed-domain disabling behavior.

…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.
@sequensucks sequensucks requested a review from BobbieGoede as a code owner June 12, 2026 09:44
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1775c0b2-71b5-4e52-a8f8-5c5c1ad6e31d

📥 Commits

Reviewing files that changed from the base of the PR and between dce91a1 and d704a18.

📒 Files selected for processing (2)
  • src/runtime/shared/detection.ts
  • src/runtime/utils.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/runtime/utils.ts
  • src/runtime/shared/detection.ts

Walkthrough

This PR adds an optional runtime defaultLocale to domain-based locale resolution and propagates it through host-based detection and the multi-domain prefix_except_default default selection. matchDomainLocale now supports single-host runtime mode when no locales declare domains, falling back to the runtime default when multiple matches remain. getHostLocale and the host detector forward runtimeI18n.defaultLocale, utils extend composable context behavior to fall back to runtime default when domain resolution is absent, and tests cover both domainful and domainless scenarios.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: resolving default locale from runtime config in single-host multiDomainLocales setups, which aligns with the core modification across detection.ts, domain.ts, utils.ts, and new test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant