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

feat(nuxt): filter support for clearNuxtData #7323

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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