Data Fetching: useFetch and useAsyncData
useFetch(), useAsyncData(), server-side vs client-side fetch, refresh(), pending state.
Data Fetching: useFetch and useAsyncData is a free Vue Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Fetching Data in Nuxt
Nuxt provides SSR-aware data fetching composables. They run on the server during render and pass results to the client, avoiding a duplicate request and a flash of empty content.
useFetch Basics
useFetch wraps a URL fetch. It returns reactive data, pending, and error values you can use directly in the template.
<script setup>
const { data, pending, error } = await useFetch('/api/posts')
</script>Showing Loading and Errors
Use pending for a loading state and error for failures. data holds the parsed JSON.
<template>
<p v-if="pending">Loading...</p>
<p v-else-if="error">Failed to load</p>
<ul v-else>
<li v-for="p in data" :key="p.id">{{ p.title }}</li>
</ul>
</template>useAsyncData for Custom Logic
When you need more than a plain URL — combining calls or transforming results — use useAsyncData. It takes a unique key and a fetcher function.
const { data } = await useAsyncData('posts', () => {
return $fetch('/api/posts')
})Why the Key Matters
The key deduplicates and caches the request. Two components using the same key share one result and one network call, preventing redundant fetches.
useFetch Is Sugar
Under the hood, useFetch(url) is essentially useAsyncData with an auto-generated key and a $fetch(url) fetcher. Use useFetch for simple URLs and useAsyncData for custom fetchers.
Client-Only With server:false
Set server: false to skip the fetch during SSR and run it only on the client — useful for user-specific data that should not block the server render.
const { data } = await useFetch('/api/me', {
server: false
})Lazy, Non-Blocking Fetches
By default the await blocks navigation until data arrives. The lazy option (or useLazyFetch) lets the page render immediately and fill in data when it resolves.
const { data, pending } = useFetch('/api/posts', {
lazy: true
})Refetching With refresh
The composables return a refresh function to re-run the fetch on demand — after a mutation or a button click.
const { data, refresh } = await useFetch('/api/posts')
function reload() { refresh() }Reactive Query Params
Pass a query or reactive options; when a watched ref changes, the fetch re-runs automatically. Bind watch to re-fetch on dependency changes.
const page = ref(1)
const { data } = await useFetch('/api/posts', {
query: { page },
watch: [page]
})Transforming the Response
The transform option reshapes the response before it reaches data — handy to pick only the fields you need and shrink the payload sent to the client.
const { data } = await useFetch('/api/user', {
transform: (u) => ({ name: u.name, id: u.id })
})Quick Check
Test your knowledge of Nuxt data fetching.
Recap
You learned Nuxt data fetching:
- useFetch returns reactive data, pending, and error.
- useAsyncData takes a key plus a fetcher for custom logic.
- server:false runs client-only; lazy makes the fetch non-blocking.
- refresh() refetches; watch re-runs on dependency change.
Frequently asked questions
Is the “Data Fetching: useFetch and useAsyncData” lesson free?
Yes — the full text of “Data Fetching: useFetch and useAsyncData” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “Data Fetching: useFetch and useAsyncData”?
useFetch(), useAsyncData(), server-side vs client-side fetch, refresh(), pending state. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Data Fetching: useFetch and useAsyncData” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nuxt 3 Project Structure and File-Based Routing
- Data Fetching: useFetch and useAsyncData
- Nuxt Server Routes and API Endpoints
- SSR vs SSG vs SPA Mode