diff --git a/docs/content/3.api/3.utils/clear-nuxt-data.md b/docs/content/3.api/3.utils/clear-nuxt-data.md index bdcf1462e36..c00036bbeb5 100644 --- a/docs/content/3.api/3.utils/clear-nuxt-data.md +++ b/docs/content/3.api/3.utils/clear-nuxt-data.md @@ -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 diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 629f5c63be9..1765e35753a 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -254,9 +254,14 @@ export function refreshNuxtData (keys?: string | string[]): Promise { 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) {