diff --git a/packages/kit/src/context.ts b/packages/kit/src/context.ts index d1307d40f2..804104d27e 100644 --- a/packages/kit/src/context.ts +++ b/packages/kit/src/context.ts @@ -5,7 +5,8 @@ import { logger } from './logger' /** Direct access to the Nuxt context with asyncLocalStorage - see https://github.com/unjs/unctx. */ export const getNuxtCtx = () => getContext(asyncNameStorage.getStore()!) -export const globalNuxtCtx = getContext('nuxt-global') +/** Global nuxt ctx - see https://github.com/unjs/unctx. */ +export const nuxtCtx = getContext('nuxt-global') // TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6 @@ -21,7 +22,7 @@ export const globalNuxtCtx = getContext('nuxt-global') export function useNuxt (): Nuxt { const instance = getNuxtCtx().tryUse() if (!instance) { - const fallbackInstance = globalNuxtCtx.tryUse() + const fallbackInstance = nuxtCtx.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 @@ -48,7 +49,7 @@ export function tryUseNuxt (): Nuxt | null { 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 globalNuxtCtx.tryUse() + return nuxtCtx.tryUse() } return nuxt } diff --git a/packages/kit/src/index.ts b/packages/kit/src/index.ts index 3c020090cf..1ca6230dea 100644 --- a/packages/kit/src/index.ts +++ b/packages/kit/src/index.ts @@ -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 { getNuxtCtx as nuxtCtx, tryUseNuxt, useNuxt, globalNuxtCtx } from './context' +export { getNuxtCtx, tryUseNuxt, useNuxt, nuxtCtx } from './context' export { isIgnored, resolveIgnorePatterns } from './ignore' export { addLayout } from './layout' export { addRouteMiddleware, extendPages, extendRouteRules } from './pages' diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index a387524285..39499f2479 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -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, globalNuxtCtx, installModule, loadNuxtConfig, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit' +import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, asyncNameStorage, nuxtCtx, installModule, loadNuxtConfig, getNuxtCtx, 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,17 +175,17 @@ async function initNuxt (nuxt: Nuxt) { } } }) - if (!globalNuxtCtx.tryUse()) { + if (!nuxtCtx.tryUse()) { // backward compatibility with 3.x - globalNuxtCtx.set(nuxt) + nuxtCtx.set(nuxt) nuxt.hook('close', () => { - globalNuxtCtx.unset() + nuxtCtx.unset() }) } // Set nuxt instance for useNuxt - nuxtCtx().set(nuxt) + getNuxtCtx().set(nuxt) nuxt.hook('close', () => { - nuxtCtx().unset() + getNuxtCtx().unset() }) const coreTypePackages = nuxt.options.typescript.hoist || []