feat(nuxt): add clearNuxtData (#5227)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
Alex Kozack 2022-09-07 14:20:09 +03:00 committed by GitHub
parent 1c07914608
commit b2f573f685
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 2 deletions

View File

@ -156,7 +156,7 @@ This method is useful if you want to refresh all the data fetching for a current
::ReadMore{link="/api/utils/refresh-nuxt-data"} ::ReadMore{link="/api/utils/refresh-nuxt-data"}
:: ::
### Example #### Example
```vue ```vue
<template> <template>
@ -173,6 +173,15 @@ const refresh = () => refreshNuxtData('count')
</script> </script>
``` ```
### `clearNuxtData`
Delete cached data, error status and pending promises of `useAsyncData` and `useFetch`.
This method is useful if you want to invalidate the data fetching for another page.
::ReadMore{link="/api/utils/clear-nuxt-data"}
::
## Options API support ## Options API support
Nuxt 3 provides a way to perform `asyncData` fetching within the Options API. You must wrap your component definition within `defineNuxtComponent` for this to work. Nuxt 3 provides a way to perform `asyncData` fetching within the Options API. You must wrap your component definition within `defineNuxtComponent` for this to work.

View File

@ -0,0 +1,18 @@
# `clearNuxtData`
::StabilityEdge
::
Delete cached data, error status and pending promises of `useAsyncData` and `useFetch`.
This method is useful if you want to invalidate the data fetching for another page.
## Type
```ts
clearNuxtData (keys?: string | string[]): void
```
## Parameters
* `keys`: On or an array of keys that are used in `useAsyncData` to delete their cached data. If no keys are provided, **every data** will be invalidated.

View File

@ -254,6 +254,28 @@ export function refreshNuxtData (keys?: string | string[]): Promise<void> {
return useNuxtApp().callHook('app:data:refresh', _keys) return useNuxtApp().callHook('app:data:refresh', _keys)
} }
export function clearNuxtData (keys?: string | string[]): void {
const nuxtApp = useNuxtApp()
const _keys = keys ? Array.from(keys).filter(key => key && typeof key === 'string') : Object.keys(nuxtApp.payload.data)
for (const key of _keys) {
if (key in nuxtApp.payload.data) {
nuxtApp.payload.data[key] = undefined
}
if (key in nuxtApp.payload._errors) {
nuxtApp.payload._errors[key] = undefined
}
if (nuxtApp._asyncData[key]) {
nuxtApp._asyncData[key]!.data.value = undefined
nuxtApp._asyncData[key]!.error.value = undefined
nuxtApp._asyncData[key]!.pending.value = false
}
if (key in nuxtApp._asyncDataPromises) {
nuxtApp._asyncDataPromises[key] = undefined
}
}
}
function pick (obj: Record<string, any>, keys: string[]) { function pick (obj: Record<string, any>, keys: string[]) {
const newObj = {} const newObj = {}
for (const key of keys) { for (const key of keys) {

View File

@ -69,7 +69,7 @@ interface _NuxtApp {
data: Ref<any> data: Ref<any>
pending: Ref<boolean> pending: Ref<boolean>
error: Ref<any> error: Ref<any>
}>, } | undefined>,
ssrContext?: NuxtSSRContext ssrContext?: NuxtSSRContext
payload: { payload: {