perf(nuxt): tree-shake asyncData client logic from server (#7056)

Co-authored-by: Ohb00 <43827372+OhB00@users.noreply.github.com>
This commit is contained in:
pooya parsa 2022-08-30 12:48:26 +02:00 committed by GitHub
parent 5a17458af5
commit afc9d9bfc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -98,20 +98,6 @@ export function useAsyncData<
// Setup nuxt instance payload // Setup nuxt instance payload
const nuxt = useNuxtApp() 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 const useInitialCache = () => (nuxt.isHydrating || options.initialCache) && nuxt.payload.data[key] !== undefined
// Create or use a shared asyncData entity // Create or use a shared asyncData entity
@ -181,6 +167,20 @@ export function useAsyncData<
// Client side // Client side
if (process.client) { 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) { if (fetchOnServer && nuxt.isHydrating && key in nuxt.payload.data) {
// 1. Hydration (server: true): no fetch // 1. Hydration (server: true): no fetch
asyncData.pending.value = false asyncData.pending.value = false