Nuxt
nuxt/async-data-key
A composable that wraps useAsyncData passes an explicit, unique key built from its inputs.
umiarkowanastatycznefe-nuxt
Dlaczego
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.
Poprawka
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.
Przykłady
// 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}`))
}Źródła
Fakty
- Identyfikator
nuxt/async-data-key- Waga
- umiarkowana
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
vuejsx- Tylko w projektach z
nuxt- Uczy jej skill
fe-nuxtNuxt apps- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain nuxt/async-data-key