diff --git a/packages/nuxt/src/app/composables/router.ts b/packages/nuxt/src/app/composables/router.ts index b7d490da8a..5c58ff8646 100644 --- a/packages/nuxt/src/app/composables/router.ts +++ b/packages/nuxt/src/app/composables/router.ts @@ -1,4 +1,4 @@ -import { getCurrentInstance, inject, onUnmounted } from 'vue' +import { getCurrentInstance, hasInjectionContext, inject, onUnmounted } from 'vue' import type { Ref } from 'vue' import type { NavigationFailure, NavigationGuard, RouteLocationNormalized, RouteLocationPathRaw, RouteLocationRaw, Router, useRoute as _useRoute, useRouter as _useRouter } from '#vue-router' import { sanitizeStatusCode } from 'h3' @@ -19,7 +19,7 @@ export const useRoute: typeof _useRoute = () => { if (process.dev && isProcessingMiddleware()) { console.warn('[nuxt] Calling `useRoute` within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes.') } - if (getCurrentInstance()) { + if (hasInjectionContext()) { return inject('_route', useNuxtApp()._route) } return useNuxtApp()._route diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 15f51d81f0..2148e78c7e 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -407,11 +407,11 @@ export function isNuxtPlugin (plugin: unknown) { export function callWithNuxt any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters) { const fn: () => ReturnType = () => args ? setup(...args as Parameters) : setup() if (process.server) { - return nuxtAppCtx.callAsync(nuxt as NuxtApp, fn) + return nuxt.vueApp.runWithContext(() => nuxtAppCtx.callAsync(nuxt as NuxtApp, fn)) } else { // In client side we could assume nuxt app is singleton nuxtAppCtx.set(nuxt as NuxtApp) - return fn() + return nuxt.vueApp.runWithContext(fn) } } diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 667958f84f..2b2e2c2b68 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e it('default server bundle size', async () => { stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir) - expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"61.7k"') + expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"61.8k"') const modules = await analyzeSizes('node_modules/**/*', serverDir) expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')