fix(nuxt): unset context after app is created (#19753)

This commit is contained in:
Daniel Roe 2023-03-17 19:01:16 +00:00 committed by GitHub
parent 5a1cfa8b66
commit 4ca2dccd38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View File

@ -34,7 +34,7 @@ if (process.server) {
await applyPlugins(nuxt, plugins)
await nuxt.hooks.callHook('app:created', vueApp)
} catch (err) {
await nuxt.callHook('app:error', err)
await nuxt.hooks.callHook('app:error', err)
nuxt.payload.error = (nuxt.payload.error || err) as any
}

View File

@ -2,7 +2,7 @@
import { getCurrentInstance, reactive } from 'vue'
import type { App, onErrorCaptured, VNode, Ref } from 'vue'
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import type { Hookable } from 'hookable'
import type { Hookable, HookCallback } from 'hookable'
import { createHooks } from 'hookable'
import { getContext } from 'unctx'
import type { SSRContext } from 'vue-bundle-renderer/runtime'
@ -187,6 +187,18 @@ export function createNuxtApp (options: CreateOptions) {
nuxtApp.hooks = createHooks<RuntimeNuxtHooks>()
nuxtApp.hook = nuxtApp.hooks.hook
if (process.server) {
async function contextCaller (hooks: HookCallback[], args: any[]) {
for (const hook of hooks) {
await nuxtAppCtx.call(nuxtApp, () => hook(...args))
}
}
// Patch callHook to preserve NuxtApp context on server
// TODO: Refactor after https://github.com/unjs/hookable/issues/74
nuxtApp.hooks.callHook = (name: any, ...args: any[]) => nuxtApp.hooks.callHookWith(contextCaller, name, ...args)
}
nuxtApp.callHook = nuxtApp.hooks.callHook
nuxtApp.provide = (name: string, value: any) => {

View File

@ -40,7 +40,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => {
it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect(stats.server.totalBytes).toBeLessThan(93600)
expect(stats.server.totalBytes).toBeLessThan(93800)
const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(modules.totalBytes).toBeLessThan(2693900)