diff --git a/packages/nuxt/src/app/entry.ts b/packages/nuxt/src/app/entry.ts index 22523aaa1d..4424ae4a4d 100644 --- a/packages/nuxt/src/app/entry.ts +++ b/packages/nuxt/src/app/entry.ts @@ -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 } diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 78b2b59d6d..5414fea108 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -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() 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) => { diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 071dfa61ef..61183c6e1c 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -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)