Skip to content

fix: locale detection should respect runtime-configured domains (#2931) #3697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions specs/different_domains/different_domains.spec.ts
Original file line number Diff line number Diff line change
@@ -151,6 +151,16 @@ test.each([
expect(dom.querySelector('#welcome-text').textContent).toEqual(header)
})

test('(#2931) detect using runtimeConfig domain', async () => {
const res = await undiciRequest('/', {
headers: {
host: 'kr.staging.nuxt-app.localhost'
}
})
const dom = getDom(await res.body.text())
expect(dom.querySelector('#welcome-text').textContent).toEqual('환영하다')
})

test('(#2374) detect with x-forwarded-host on server', async () => {
const html = await $fetch('/', {
headers: {
13 changes: 13 additions & 0 deletions specs/fixtures/different_domains/i18n/i18n.config.ts
Original file line number Diff line number Diff line change
@@ -40,6 +40,19 @@ export default {
article: 'Dette er bloggartikkelsiden'
}
}
},
kr: {
welcome: '환영하다',
home: '홈페이지',
profile: '프로필',
about: '회사 소개',
posts: '게시물',
dynamic: '동적',
pages: {
blog: {
article: '여기는 블로그 게시물 페이지입니다'
}
}
}
},
fallbackLocale: 'en'
19 changes: 16 additions & 3 deletions src/runtime/shared/detection.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ import { getLocaleFromRoute, getLocaleFromRoutePath } from '#i18n-kit/routing'
import { findBrowserLocale } from '#i18n-kit/browser'
import { parseAcceptLanguage } from '@intlify/utils'
import { matchDomainLocale } from './domain'
import { useRuntimeI18n } from '../shared/utils'
import type { I18nPublicRuntimeConfig } from '../../types'
import { parse } from 'cookie-es'

import type { H3Event } from 'h3'
@@ -28,23 +30,34 @@ const getNavigatorLocale = (event: H3Event | undefined) => {
return findBrowserLocale(normalizedLocales, navigator.languages)
}

const getHostLocale = (event: H3Event | undefined, path: string) => {
const getHostLocale = (
event: H3Event | undefined,
path: string,
domainLocales: I18nPublicRuntimeConfig['domainLocales']
) => {
const host = import.meta.client
? new URL(window.location.href).host
: getRequestURL(event!, { xForwardedHost: true }).host
return matchDomainLocale(normalizedLocales, host, getLocaleFromRoutePath(path))

const locales = normalizedLocales.map(l => ({
...l,
domain: domainLocales[l.code]?.domain ?? l.domain
}))
return matchDomainLocale(locales, host, getLocaleFromRoutePath(path))
}
Comment on lines +33 to 47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Per-request cloning of normalizedLocales is wasteful

getHostLocale maps over normalizedLocales on every invocation to attach runtime domains.
For heavily trafficked apps this runs once per request, causing unnecessary allocations.

-  const locales = normalizedLocales.map(l => ({
-    ...l,
-    domain: domainLocales[l.code]?.domain ?? l.domain
-  }))
+  // cache by reference – memoised on first call
+  const locales =
+    cachedRuntimeDomains ??
+    (cachedRuntimeDomains = normalizedLocales.map(l => ({
+      ...l,
+      domain: domainLocales[l.code]?.domain ?? l.domain
+    })))

You can stick a simple let cachedRuntimeDomains: LocaleObject[] | undefined outside the function to memoise until domainLocales changes (which is only on hot-reload).

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/runtime/shared/detection.ts around lines 33 to 47, the function
getHostLocale creates a new mapped array from normalizedLocales on every call,
causing unnecessary allocations per request. To fix this, declare a variable
cachedRuntimeDomains outside the function to store the mapped locales with
runtime domains, and update this cache only when domainLocales changes (such as
on hot-reload). Inside getHostLocale, return the cached array instead of
remapping normalizedLocales each time.


export const useDetectors = (event: H3Event | undefined, config: { cookieKey: string }) => {
if (import.meta.server && !event) {
throw new Error('H3Event is required for server-side locale detection')
}

const runtimeI18n = useRuntimeI18n()

return {
cookie: () => getCookieLocale(event, config.cookieKey),
header: () => (import.meta.server ? getHeaderLocale(event) : undefined),
navigator: () => (import.meta.client ? getNavigatorLocale(event) : undefined),
host: (path: string) => getHostLocale(event, path),
host: (path: string) => getHostLocale(event, path, runtimeI18n.domainLocales),
route: (path: string | CompatRoute) => getRouteLocale(event, path)
}
}