Performance: Nuxt data fetching (useFetch, useAsyncData)
MEDIUM IMPACT
This concept affects page load speed and interaction responsiveness by controlling when and how data is fetched and rendered in Nuxt apps.
import { useAsyncData } from '#app' export default { setup() { const { data } = useAsyncData('data', () => $fetch('/api/data')) return { data } } }
export default { async mounted() { this.data = await fetch('/api/data').then(res => res.json()) } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client fetch in mounted() | Low initial DOM nodes | Multiple reflows on data arrival | High paint cost due to delayed content | [X] Bad |
| Server fetch with useAsyncData | DOM ready with data | Single reflow during hydration | Low paint cost, fast LCP | [OK] Good |
| Repeated useFetch calls in watcher | Multiple DOM updates | Multiple reflows | High paint cost, INP delay | [X] Bad |
| useFetch with refresh() | Single DOM update per refresh | Single reflow | Moderate paint cost, better INP | [!] OK |