feat(nuxt): use runWithContext within callWithNuxt (#20775)

This commit is contained in:
Daniel Roe 2023-05-11 12:39:08 +01:00 committed by GitHub
parent f2c3c2fab4
commit 93ba5e7cd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 5 deletions

View File

@ -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

View File

@ -407,11 +407,11 @@ export function isNuxtPlugin (plugin: unknown) {
export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp | _NuxtApp, setup: T, args?: Parameters<T>) {
const fn: () => ReturnType<T> = () => args ? setup(...args as Parameters<T>) : 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)
}
}

View File

@ -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"')