mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
perf(nuxt): unsubscribe from watch when scope is disposed (#26554)
This commit is contained in:
parent
2f8915127d
commit
cbb4a1cb61
@ -1,4 +1,4 @@
|
|||||||
import { computed, getCurrentInstance, onBeforeMount, onServerPrefetch, onUnmounted, ref, shallowRef, toRef, unref, watch } from 'vue'
|
import { computed, getCurrentInstance, getCurrentScope, onBeforeMount, onScopeDispose, onServerPrefetch, onUnmounted, ref, shallowRef, toRef, unref, watch } from 'vue'
|
||||||
import type { Ref, WatchSource } from 'vue'
|
import type { Ref, WatchSource } from 'vue'
|
||||||
import type { NuxtApp } from '../nuxt'
|
import type { NuxtApp } from '../nuxt'
|
||||||
import { useNuxtApp } from '../nuxt'
|
import { useNuxtApp } from '../nuxt'
|
||||||
@ -223,8 +223,8 @@ export function useAsyncData<
|
|||||||
|
|
||||||
const promise = nuxtApp.runWithContext(_handler)
|
const promise = nuxtApp.runWithContext(_handler)
|
||||||
|
|
||||||
nuxtApp.ssrContext!._sharedPrerenderCache!.set(key, promise)
|
nuxtApp.ssrContext!._sharedPrerenderCache!.set(key, promise)
|
||||||
return promise
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to get default values
|
// Used to get default values
|
||||||
@ -370,16 +370,20 @@ export function useAsyncData<
|
|||||||
// 4. Navigation (lazy: false) - or plugin usage: await fetch
|
// 4. Navigation (lazy: false) - or plugin usage: await fetch
|
||||||
initialFetch()
|
initialFetch()
|
||||||
}
|
}
|
||||||
|
const hasScope = getCurrentScope()
|
||||||
if (options.watch) {
|
if (options.watch) {
|
||||||
watch(options.watch, () => asyncData.refresh())
|
const unsub = watch(options.watch, () => asyncData.refresh())
|
||||||
|
if (hasScope) {
|
||||||
|
onScopeDispose(unsub)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const off = nuxtApp.hook('app:data:refresh', async (keys) => {
|
const off = nuxtApp.hook('app:data:refresh', async (keys) => {
|
||||||
if (!keys || keys.includes(key)) {
|
if (!keys || keys.includes(key)) {
|
||||||
await asyncData.refresh()
|
await asyncData.refresh()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (instance) {
|
if (hasScope) {
|
||||||
onUnmounted(off)
|
onScopeDispose(off)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user