Skip to content

Commit

Permalink
fix: onUnmounted warnings triggered by composable assignment (#2750)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Feb 2, 2024
1 parent 5db7351 commit 2145ce5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
14 changes: 2 additions & 12 deletions src/runtime/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRequestHeaders, useCookie as useNuxtCookie } from '#imports'
import { ref, computed, watch, onUnmounted } from 'vue'
import { parseAcceptLanguage } from '../internal'
import { parseAcceptLanguage, wrapComposable } from '../internal'
import { localeCodes, normalizedLocales, nuxtI18nOptions } from '#build/i18n.options.mjs'
import { getActiveHead } from 'unhead'
import { getNormalizedLocales, initCommonComposableOptions } from '../utils'
Expand All @@ -23,21 +23,11 @@ import type { Ref } from 'vue'
import type { Locale } from 'vue-i18n'
import type { RouteLocation, RouteLocationNormalizedLoaded, RouteLocationRaw, Router } from 'vue-router'
import type { I18nHeadMetaInfo, I18nHeadOptions, SeoAttributesOptions } from '#build/i18n.options.mjs'
import type { CommonComposableOptions, HeadParam } from '../utils'
import type { HeadParam } from '../utils'

export * from 'vue-i18n'
export * from './shared'

type TailParameters<T> = T extends (first: CommonComposableOptions, ...rest: infer R) => unknown ? R : never

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function wrapComposable<F extends (common: CommonComposableOptions, ...args: any[]) => any>(
fn: F,
common = initCommonComposableOptions()
) {
return (...args: TailParameters<F>) => fn(common, ...args)
}

/**
* Returns a function to set i18n params.
*
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
normalizedLocales
} from '#build/i18n.options.mjs'
import { findBrowserLocale, getLocalesRegex, getI18nTarget } from './routing/utils'
import { initCommonComposableOptions, type CommonComposableOptions } from './utils'

import type { Locale } from 'vue-i18n'
import type { DetectBrowserLanguageOptions, LocaleObject } from '#build/i18n.options.mjs'
Expand All @@ -46,6 +47,15 @@ export function defineGetter<K extends string | number | symbol, V>(obj: Record<
Object.defineProperty(obj, key, { get: () => val })
}

type TailParameters<T> = T extends (first: CommonComposableOptions, ...rest: infer R) => unknown ? R : never
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function wrapComposable<F extends (common: CommonComposableOptions, ...args: any[]) => any>(
fn: F,
common = initCommonComposableOptions()
) {
return (...args: TailParameters<F>) => fn(common, ...args)
}

/**
* Parses locales provided from browser through `accept-language` header.
*
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/routing/extends/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { DEFAULT_BASE_URL } from '#build/i18n.options.mjs'
import { resolveBaseUrl, isVueI18n, getComposer, inBrowser } from '../utils'
import {
getRouteBaseName,
localeHead,
localeLocation,
localePath,
localeRoute,
resolveRoute,
switchLocalePath
} from '../compatibles'
import { useLocaleHead, wrapComposable } from '../../composables'
import { wrapComposable } from '../../internal'
import { initCommonComposableOptions } from '../../utils'

import type { NuxtApp } from 'nuxt/app'
Expand Down Expand Up @@ -143,13 +144,13 @@ export function extendI18n<Context = unknown, TI18n extends I18n = I18n>(
// extend vue component instance
vue.mixin({
methods: {
resolveRoute,
getRouteBaseName,
resolveRoute: wrapComposable(resolveRoute, common),
localePath: wrapComposable(localePath, common),
localeRoute: wrapComposable(localeRoute, common),
localeLocation: wrapComposable(localeLocation, common),
switchLocalePath: wrapComposable(switchLocalePath, common),
localeHead: useLocaleHead
localeHead: wrapComposable(localeHead, common)
}
})
}
Expand Down
15 changes: 9 additions & 6 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type LocaleObject
} from '#build/i18n.options.mjs'
import {
wrapComposable,
detectBrowserLanguage,
getLocaleCookie,
callVueI18nInterfaces,
Expand All @@ -26,10 +27,12 @@ import {
DefaultDetectBrowserLanguageFromResult
} from './internal'
import { loadLocale, makeFallbackLocaleCodes } from './messages'
import { useLocaleRoute, useSwitchLocalePath, useLocalePath, useLocaleHead } from '#i18n'
import {
switchLocalePath,
localeHead,
localePath,
localeRoute,
getRouteBaseName,
switchLocalePath,
DefaultPrefixable,
DefaultSwitchLocalePathIntercepter
} from './routing/compatibles'
Expand Down Expand Up @@ -402,10 +405,10 @@ export function injectNuxtHelpers(nuxt: NuxtApp, i18n: I18n | VueI18n | Composer
*/
defineGetter(nuxt, '$i18n', getI18nTarget(i18n))
defineGetter(nuxt, '$getRouteBaseName', getRouteBaseName)
defineGetter(nuxt, '$localePath', useLocalePath())
defineGetter(nuxt, '$localeRoute', useLocaleRoute())
defineGetter(nuxt, '$switchLocalePath', useSwitchLocalePath())
defineGetter(nuxt, '$localeHead', useLocaleHead())
defineGetter(nuxt, '$localePath', wrapComposable(localePath))
defineGetter(nuxt, '$localeRoute', wrapComposable(localeRoute))
defineGetter(nuxt, '$switchLocalePath', wrapComposable(switchLocalePath))
defineGetter(nuxt, '$localeHead', wrapComposable(localeHead))
}

// override prefix for route path, support domain
Expand Down

0 comments on commit 2145ce5

Please sign in to comment.