mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-22 11:22:43 +00:00
refactor: rename fallback to global
This commit is contained in:
parent
97af928232
commit
982af4e1a2
@ -3,9 +3,9 @@ import type { Nuxt } from '@nuxt/schema'
|
||||
import { asyncNameStorage } from './utils'
|
||||
import { logger } from './logger'
|
||||
|
||||
/** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */
|
||||
export const nuxtCtx = () => getContext<Nuxt>(asyncNameStorage.getStore()!)
|
||||
export const fallbackNuxtCtx = getContext<Nuxt>('nuxt-fallback')
|
||||
/** Direct access to the Nuxt context with asyncLocalStorage - see https://github.com/unjs/unctx. */
|
||||
export const getNuxtCtx = () => getContext<Nuxt>(asyncNameStorage.getStore()!)
|
||||
export const globalNuxtCtx = getContext<Nuxt>('nuxt-global')
|
||||
|
||||
// TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6
|
||||
|
||||
@ -19,9 +19,9 @@ export const fallbackNuxtCtx = getContext<Nuxt>('nuxt-fallback')
|
||||
* ```
|
||||
*/
|
||||
export function useNuxt (): Nuxt {
|
||||
const instance = nuxtCtx().tryUse()
|
||||
const instance = getNuxtCtx().tryUse()
|
||||
if (!instance) {
|
||||
const fallbackInstance = fallbackNuxtCtx.tryUse()
|
||||
const fallbackInstance = globalNuxtCtx.tryUse()
|
||||
if (fallbackInstance) {
|
||||
logger.warn('Using fallback global Nuxt instance. You may be using a @nuxt/kit composable outside of a Nuxt context, this behavior is deprecated and will be removed in v4.')
|
||||
return fallbackInstance
|
||||
@ -45,10 +45,10 @@ export function useNuxt (): Nuxt {
|
||||
* ```
|
||||
*/
|
||||
export function tryUseNuxt (): Nuxt | null {
|
||||
const nuxt = nuxtCtx().tryUse()
|
||||
const nuxt = getNuxtCtx().tryUse()
|
||||
if (!nuxt) {
|
||||
logger.warn('Using fallback global Nuxt instance. You may be using a @nuxt/kit composable outside of a Nuxt context, this behavior is deprecated and will be removed in v4.')
|
||||
return fallbackNuxtCtx.tryUse()
|
||||
return globalNuxtCtx.tryUse()
|
||||
}
|
||||
return nuxt
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ export type { ExtendConfigOptions, ExtendViteConfigOptions, ExtendWebpackConfigO
|
||||
export { assertNuxtCompatibility, checkNuxtCompatibility, getNuxtVersion, hasNuxtCompatibility, isNuxtMajorVersion, normalizeSemanticVersion, isNuxt2, isNuxt3 } from './compatibility'
|
||||
export { addComponent, addComponentsDir } from './components'
|
||||
export type { AddComponentOptions } from './components'
|
||||
export { nuxtCtx, tryUseNuxt, useNuxt, fallbackNuxtCtx } from './context'
|
||||
export { getNuxtCtx as nuxtCtx, tryUseNuxt, useNuxt, globalNuxtCtx } from './context'
|
||||
export { isIgnored, resolveIgnorePatterns } from './ignore'
|
||||
export { addLayout } from './layout'
|
||||
export { addRouteMiddleware, extendPages, extendRouteRules } from './pages'
|
||||
|
@ -4,7 +4,7 @@ import { join, normalize, relative, resolve } from 'pathe'
|
||||
import { createDebugger, createHooks } from 'hookable'
|
||||
import ignore from 'ignore'
|
||||
import type { LoadNuxtOptions } from '@nuxt/kit'
|
||||
import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, asyncNameStorage, fallbackNuxtCtx, installModule, loadNuxtConfig, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
|
||||
import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, asyncNameStorage, globalNuxtCtx, installModule, loadNuxtConfig, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
|
||||
import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions } from 'nuxt/schema'
|
||||
import type { PackageJson } from 'pkg-types'
|
||||
import { readPackageJSON } from 'pkg-types'
|
||||
@ -175,11 +175,11 @@ async function initNuxt (nuxt: Nuxt) {
|
||||
}
|
||||
}
|
||||
})
|
||||
if (!fallbackNuxtCtx.tryUse()) {
|
||||
if (!globalNuxtCtx.tryUse()) {
|
||||
// backward compatibility with 3.x
|
||||
fallbackNuxtCtx.set(nuxt)
|
||||
globalNuxtCtx.set(nuxt)
|
||||
nuxt.hook('close', () => {
|
||||
fallbackNuxtCtx.unset()
|
||||
globalNuxtCtx.unset()
|
||||
})
|
||||
}
|
||||
// Set nuxt instance for useNuxt
|
||||
|
Loading…
Reference in New Issue
Block a user