Nuxt

nuxt/no-bare-fetch-in-setup

Load a page's initial data with useFetch or useAsyncData, not a bare $fetch at the top of script setup.

umiarkowanastatycznefe-nuxt

Dlaczego

Code at the top of <script setup> runs on the server and again in the browser during hydration. A bare $fetch there requests the same data twice, the second time while the user waits, and the browser's result can differ from the server's HTML, which gives a hydration mismatch.

Poprawka

Wrap the initial load: const { data, status, error } = await useFetch('/api/products'), or useAsyncData('products', () => $fetch('/api/products')) for custom logic. The server's result travels in the page payload and the browser reuses it. Keep $fetch for requests that a user action starts.

Przykłady

// bad (top of script setup)
const products = await $fetch('/api/products')
// good
const { data: products, status } = await useFetch('/api/products')

Źródła

Fakty

Identyfikator
nuxt/no-bare-fetch-in-setup
Waga
umiarkowana
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
vue
Tylko w projektach z
nuxt
Uczy jej skill
fe-nuxt Nuxt apps
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain nuxt/no-bare-fetch-in-setup