From afc9d9bfc8c3f4c5a271dc655017dee6f6e1bfce Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Tue, 30 Aug 2022 12:48:26 +0200 Subject: [PATCH] perf(nuxt): tree-shake asyncData client logic from server (#7056) Co-authored-by: Ohb00 <43827372+OhB00@users.noreply.github.com> --- .../nuxt/src/app/composables/asyncData.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index a7596859e4..34ad7d9d89 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -98,20 +98,6 @@ export function useAsyncData< // Setup nuxt instance payload const nuxt = useNuxtApp() - // Setup hook callbacks once per instance - const instance = getCurrentInstance() - if (instance && !instance._nuxtOnBeforeMountCbs) { - instance._nuxtOnBeforeMountCbs = [] - const cbs = instance._nuxtOnBeforeMountCbs - if (instance && process.client) { - onBeforeMount(() => { - cbs.forEach((cb) => { cb() }) - cbs.splice(0, cbs.length) - }) - onUnmounted(() => cbs.splice(0, cbs.length)) - } - } - const useInitialCache = () => (nuxt.isHydrating || options.initialCache) && nuxt.payload.data[key] !== undefined // Create or use a shared asyncData entity @@ -181,6 +167,20 @@ export function useAsyncData< // Client side if (process.client) { + // Setup hook callbacks once per instance + const instance = getCurrentInstance() + if (instance && !instance._nuxtOnBeforeMountCbs) { + instance._nuxtOnBeforeMountCbs = [] + const cbs = instance._nuxtOnBeforeMountCbs + if (instance) { + onBeforeMount(() => { + cbs.forEach((cb) => { cb() }) + cbs.splice(0, cbs.length) + }) + onUnmounted(() => cbs.splice(0, cbs.length)) + } + } + if (fetchOnServer && nuxt.isHydrating && key in nuxt.payload.data) { // 1. Hydration (server: true): no fetch asyncData.pending.value = false