Nuxt
nuxt/async-data-key
A composable that wraps useAsyncData passes an explicit, unique key built from its inputs.
moderatestaticfe-nuxt
Why
Without a key, Nuxt builds one from the file name and line of the useAsyncData call. Inside a composable that line is the same for every caller, so useProduct(1) and useProduct(2) share one cache entry: the second product page shows the first product's data until a refresh, and the payload holds the wrong value.
Fix
Pass the key first and make it depend on the inputs: useAsyncData(() => product-${id.value}, () => $fetch(/api/products/${id.value})), or a string key when the composable has no inputs. Keep options (transform, pick, default) the same for every call that shares a key.
Examples
// bad
export function useProduct(id) {
return useAsyncData(() => $fetch(`/api/products/${id.value}`))
}
// good
export function useProduct(id) {
return useAsyncData(() => `product-${id.value}`, () => $fetch(`/api/products/${id.value}`))
}Sources
Facts
- Id
nuxt/async-data-key- Severity
- moderate
- Check
- static checked in the source by
audit - Applies to
vuejsx- Only in projects with
nuxt- Taught by the skill
fe-nuxtNuxt apps- Since version
- 0.1.0
Read it in the terminal
npx github:dawidolko/frontend-skills explain nuxt/async-data-key