diff --git a/packages/kit/src/context.ts b/packages/kit/src/context.ts index 08eb4aebee..d1307d40f2 100644 --- a/packages/kit/src/context.ts +++ b/packages/kit/src/context.ts @@ -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(asyncNameStorage.getStore()!) -export const fallbackNuxtCtx = getContext('nuxt-fallback') +/** 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') // TODO: Use use/tryUse from unctx. https://github.com/unjs/unctx/issues/6 @@ -19,9 +19,9 @@ export const fallbackNuxtCtx = getContext('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 } diff --git a/packages/kit/src/index.ts b/packages/kit/src/index.ts index b5e500202a..3c020090cf 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 { 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' diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 9d26cb7160..a387524285 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, 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