Skip to content
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

refactor!: rename public runtime config properties #3096

Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ Reasons for change

## Locale `iso` renamed to `language`

The `iso` property on a locale object has been renamed to `language` to be consistent with the usage of Language Tags on the web (e.g. `navigator.language` and `Accept-Language`). The original `iso` property name referred to ISO standards which describe valid Language Tags, see the [related issue](https://github.com/nuxt-modules/i18n/issues/2449) for more details.
The `iso` property on a locale object has been renamed to `language` to be consistent with the usage of Language Tags on the web (e.g. `navigator.language` and `Accept-Language`). The original `iso` property name referred to ISO standards which describe valid Language Tags, see the [related issue](https://github.com/nuxt-modules/i18n/issues/2449) for more details.

## Runtime config properties

Some properties have changed or swapped names to better fit their functionality, the runtime config properties configured by this module are mostly used for internal purposes and should not be relied upon but it's worth noting these changes.

| v8 | v9 | Notes |
| --- | --- | --- |
| `locales` | `domainLocales` | This also changes the environment variable key to `NUXT_PUBLIC_I18N_DOMAIN_LOCALES_{code}_DOMAIN`, see [`runtimeConfig`](/docs/options/runtime-config#domainLocales) |
| `configLocales` | `locales` | |
4 changes: 2 additions & 2 deletions docs/content/docs/5.v9/3.options/9.runtime-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ This runtime config option is the same as the [`baseUrl`](/docs/options/routing#
Note that the `baseUrl` module option allows you to set the function, but the runtime config does not due to limitations.
::

### `locales`
### `domainLocales`

- property: `locales[code].domain`
- key: `NUXT_PUBLIC_I18N_LOCALES_{code}_DOMAIN`
- key: `NUXT_PUBLIC_I18N_DOMAIN_LOCALES_{code}_DOMAIN`

This runtime config option allows overriding the domain set in the [`locales`](/docs/options/routing#locales) module option.
2 changes: 1 addition & 1 deletion specs/different_domains/different_domains.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ await setup({
runtimeConfig: {
public: {
i18n: {
locales: {
domainLocales: {
kr: {
domain: 'kr.staging.nuxt-app.localhost'
}
Expand Down
24 changes: 10 additions & 14 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,7 @@ export default defineNuxtModule<NuxtI18nOptions>({
skipSettingLocaleOnNavigate: options.skipSettingLocaleOnNavigate,
differentDomains: options.differentDomains,
trailingSlash: options.trailingSlash,
configLocales: options.locales,
locales: options.locales.reduce(
(obj, locale) => {
if (typeof locale === 'string') {
obj[locale] = { domain: undefined }
} else {
obj[locale.code] = { domain: locale.domain }
}
return obj
},
{} as Record<string, { domain: string | undefined }>
),
locales: options.locales,
detectBrowserLanguage: options.detectBrowserLanguage ?? DEFAULT_OPTIONS.detectBrowserLanguage,
experimental: options.experimental,
multiDomainLocales: options.multiDomainLocales
Expand Down Expand Up @@ -245,7 +234,7 @@ export default defineNuxtModule<NuxtI18nOptions>({
}

// @ts-expect-error type error
nuxt.options.runtimeConfig.public.i18n.configLocales = simplifyLocaleOptions(nuxt, defu({}, options))
nuxt.options.runtimeConfig.public.i18n.locales = simplifyLocaleOptions(nuxt, defu({}, options))

addTemplate({
filename: NUXT_I18N_TEMPLATE_OPTIONS_KEY,
Expand Down Expand Up @@ -387,6 +376,13 @@ export interface ModulePublicRuntimeConfig {
rootRedirect: NuxtI18nOptions['rootRedirect']
multiDomainLocales?: NuxtI18nOptions['multiDomainLocales']

/**
* Overwritten at build time, used to pass generated options to runtime
*
* @internal
*/
domainLocales: { [key: Locale]: { domain: string | undefined } }

/**
* Overwritten at build time, used to pass generated options to runtime
*
Expand All @@ -398,7 +394,7 @@ export interface ModulePublicRuntimeConfig {
*
* @internal
*/
configLocales: NonNullable<Required<NuxtI18nOptions<unknown>>['locales']>
locales: NonNullable<Required<NuxtI18nOptions<unknown>>['locales']>
/**
* Overwritten at build time, used to pass generated options to runtime
*
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,9 @@ export function getDomainFromLocale(localeCode: Locale): string | undefined {
const nuxtApp = useNuxtApp()
const host = getHost()
// lookup the `differentDomain` origin associated with given locale.
const config = runtimeConfig.public.i18n as { locales?: Record<Locale, { domain?: string; domains?: string[] }> }
const config = runtimeConfig.public.i18n
const lang = normalizedLocales.find(locale => locale.code === localeCode)
const domain =
config?.locales?.[localeCode]?.domain ||
lang?.domain ||
config?.locales?.[localeCode]?.domains?.find((v: string) => v === host) ||
lang?.domains?.find((v: string) => v === host)
const domain = config?.domainLocales?.[localeCode]?.domain || lang?.domain || lang?.domains?.find(v => v === host)

if (domain) {
if (hasProtocol(domain, { strict: true })) {
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ export default defineNuxtPlugin({
const { vueApp: app } = nuxt
const nuxtContext = nuxt as unknown as NuxtApp
const host = getHost()
const { configLocales, defaultLocale, multiDomainLocales, strategy } = nuxtContext.$config.public.i18n
const { locales, defaultLocale, multiDomainLocales, strategy } = nuxtContext.$config.public.i18n

const hasDefaultForDomains = configLocales.some(
const hasDefaultForDomains = locales.some(
(l): l is LocaleObject => typeof l !== 'string' && Array.isArray(l.defaultForDomains)
)

let defaultLocaleDomain: string
if (defaultLocale) {
defaultLocaleDomain = defaultLocale
} else if (hasDefaultForDomains) {
const findDefaultLocale = configLocales.find((l): l is LocaleObject =>
const findDefaultLocale = locales.find((l): l is LocaleObject =>
typeof l === 'string' || !Array.isArray(l.defaultForDomains) ? false : l.defaultForDomains.includes(host ?? '')
)

Expand Down Expand Up @@ -175,7 +175,7 @@ export default defineNuxtPlugin({
extendI18n(i18n, {
extendComposer(composer) {
const route = useRoute()
const _locales = ref<string[] | LocaleObject[]>(runtimeI18n.configLocales)
const _locales = ref<string[] | LocaleObject[]>(runtimeI18n.locales)
const _localeCodes = ref<string[]>(localeCodes)
const _baseUrl = ref<string>('')

Expand Down
7 changes: 5 additions & 2 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export async function navigate(
{ status = 302, enableNavigate = false }: { status?: number; enableNavigate?: boolean } = {}
) {
const { nuxtApp, i18n, locale, route } = args
const { rootRedirect, differentDomains, multiDomainLocales, skipSettingLocaleOnNavigate, configLocales, strategy } =
const { rootRedirect, differentDomains, multiDomainLocales, skipSettingLocaleOnNavigate, locales, strategy } =
nuxtApp.$config.public.i18n
const logger = /*#__PURE__*/ createLogger('navigate')
let { redirectPath } = args
Expand All @@ -304,6 +304,9 @@ export async function navigate(
redirectPath = '/' + rootRedirect.path
status = rootRedirect.statusCode
}

// TODO: resolve type errors for nuxt context extensions
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
redirectPath = nuxtApp.$localePath(redirectPath, locale)
__DEBUG__ && logger.log('rootRedirect mode', { redirectPath, status })
return _navigate(redirectPath, status)
Expand All @@ -321,7 +324,7 @@ export async function navigate(

if (multiDomainLocales && strategy === 'prefix_except_default') {
const host = getHost()
const currentDomain = configLocales.find(locale => {
const currentDomain = locales.find(locale => {
if (typeof locale !== 'string') {
return locale.defaultForDomains?.find(domain => domain === host)
}
Expand Down