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

Commit 6b3a1a4

Browse files
authored
feat(nuxt): filter support for clearNuxtData (#7323)
1 parent eab4706 commit 6b3a1a4

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

docs/content/3.api/3.utils/clear-nuxt-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This method is useful if you want to invalidate the data fetching for another pa
1010
## Type
1111

1212
```ts
13-
clearNuxtData (keys?: string | string[]): void
13+
clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void
1414
```
1515

1616
## Parameters

packages/nuxt/src/app/composables/asyncData.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,14 @@ export function refreshNuxtData (keys?: string | string[]): Promise<void> {
254254
return useNuxtApp().callHook('app:data:refresh', _keys)
255255
}
256256

257-
export function clearNuxtData (keys?: string | string[]): void {
257+
export function clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void {
258258
const nuxtApp = useNuxtApp()
259-
const _keys = keys ? Array.from(keys).filter(key => key && typeof key === 'string') : Object.keys(nuxtApp.payload.data)
259+
const _allKeys = Object.keys(nuxtApp.payload.data)
260+
const _keys: string[] = !keys
261+
? _allKeys
262+
: typeof keys === 'function'
263+
? _allKeys.filter(keys)
264+
: Array.isArray(keys) ? keys : [keys]
260265

261266
for (const key of _keys) {
262267
if (key in nuxtApp.payload.data) {

0 commit comments

Comments
 (0)