Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
feat(nuxt): filter support for clearNuxtData (#7323)
Browse files Browse the repository at this point in the history
  • Loading branch information
cawa-93 committed Sep 7, 2022
1 parent eab4706 commit 6b3a1a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/content/3.api/3.utils/clear-nuxt-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This method is useful if you want to invalidate the data fetching for another pa
## Type

```ts
clearNuxtData (keys?: string | string[]): void
clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void
```

## Parameters
Expand Down
9 changes: 7 additions & 2 deletions packages/nuxt/src/app/composables/asyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,14 @@ export function refreshNuxtData (keys?: string | string[]): Promise<void> {
return useNuxtApp().callHook('app:data:refresh', _keys)
}

export function clearNuxtData (keys?: string | string[]): void {
export function clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void {
const nuxtApp = useNuxtApp()
const _keys = keys ? Array.from(keys).filter(key => key && typeof key === 'string') : Object.keys(nuxtApp.payload.data)
const _allKeys = Object.keys(nuxtApp.payload.data)
const _keys: string[] = !keys
? _allKeys
: typeof keys === 'function'
? _allKeys.filter(keys)
: Array.isArray(keys) ? keys : [keys]

for (const key of _keys) {
if (key in nuxtApp.payload.data) {
Expand Down

0 comments on commit 6b3a1a4

Please sign in to comment.