mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxt): use runWithContext
within callWithNuxt
(#20775)
This commit is contained in:
parent
f2c3c2fab4
commit
93ba5e7cd2
@ -1,4 +1,4 @@
|
|||||||
import { getCurrentInstance, inject, onUnmounted } from 'vue'
|
import { getCurrentInstance, hasInjectionContext, inject, onUnmounted } from 'vue'
|
||||||
import type { Ref } 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 type { NavigationFailure, NavigationGuard, RouteLocationNormalized, RouteLocationPathRaw, RouteLocationRaw, Router, useRoute as _useRoute, useRouter as _useRouter } from '#vue-router'
|
||||||
import { sanitizeStatusCode } from 'h3'
|
import { sanitizeStatusCode } from 'h3'
|
||||||
@ -19,7 +19,7 @@ export const useRoute: typeof _useRoute = () => {
|
|||||||
if (process.dev && isProcessingMiddleware()) {
|
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.')
|
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 inject('_route', useNuxtApp()._route)
|
||||||
}
|
}
|
||||||
return useNuxtApp()._route
|
return useNuxtApp()._route
|
||||||
|
@ -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>) {
|
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()
|
const fn: () => ReturnType<T> = () => args ? setup(...args as Parameters<T>) : setup()
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
return nuxtAppCtx.callAsync(nuxt as NuxtApp, fn)
|
return nuxt.vueApp.runWithContext(() => nuxtAppCtx.callAsync(nuxt as NuxtApp, fn))
|
||||||
} else {
|
} else {
|
||||||
// In client side we could assume nuxt app is singleton
|
// In client side we could assume nuxt app is singleton
|
||||||
nuxtAppCtx.set(nuxt as NuxtApp)
|
nuxtAppCtx.set(nuxt as NuxtApp)
|
||||||
return fn()
|
return nuxt.vueApp.runWithContext(fn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e
|
|||||||
|
|
||||||
it('default server bundle size', async () => {
|
it('default server bundle size', async () => {
|
||||||
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
|
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)
|
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
||||||
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')
|
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')
|
||||||
|
Loading…
Reference in New Issue
Block a user