Nuxe provides two composables for fetching data:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dvlkit/nuxe/llms.txt
Use this file to discover all available pages before exploring further.
useAsyncData for wrapping any arbitrary async logic, and useFetch as a convenience layer that combines useAsyncData with the built-in $fetch HTTP client. Both composables run on the server during SSR, serialize the result into the page payload, and rehydrate it on the client — so the network request is never made twice.
useAsyncData
useAsyncData accepts a unique cache key, an async handler function, and an optional options object. It returns a set of reactive refs you can use directly in your template.
Signature
UseAsyncDataReturn<T> contains:
The resolved value from the handler.
null until the handler completes (or until default is applied).true while the handler is in-flight.Populated if the handler throws or all retries are exhausted.
Lifecycle status of the async operation. See Status values below.
Manually re-run the handler and update
data, pending, error, and status.Basic example
Options
Factory function that returns the initial (fallback) value for
data before the handler resolves. Useful for avoiding null checks in templates.Set to
false to skip running the handler on the server. The fetch will then happen on the client after hydration.When
true, the handler runs without blocking navigation. pending starts as true and the data arrives asynchronously. Useful for non-critical data that shouldn’t delay page rendering.Number of times to retry the handler after a failure before populating
error.Milliseconds to wait between retry attempts.
One or more reactive sources. Whenever any of them change,
useAsyncData automatically re-runs the handler and refreshes data.Status values
Thestatus ref follows a simple lifecycle:
| Value | Meaning |
|---|---|
'idle' | Handler has not been triggered yet. |
'pending' | Handler is currently running. |
'success' | Handler completed and data is populated. |
'error' | Handler failed; error contains the thrown value. |
Hydration
When a page is server-rendered,useAsyncData stores its result in the server payload under the provided key. On the client, the matching hydrated value is read back immediately — the handler is not re-executed. This means every key must be unique across the application. If the same key is reused with a different handler, the client will receive stale data.
useFetch
useFetch is a thin wrapper around useAsyncData and $fetch. It derives its cache key from the URL (or options.key), handles reactive options automatically, and exposes an extra statusCode ref for the HTTP response status.
Signature
UseFetchReturn<T> extends UseAsyncDataReturn<T> with:
The HTTP status code of the most recent response.
null if no response has been received yet or a network-level error occurred.Basic example
Reactive query with watch
Options likequery, params, body, and headers accept plain values, refs, or getters. When the value of any of these changes, useFetch automatically triggers a refresh — no manual watch required for most cases. For ref-based inputs you can be explicit with watch:
Error handling with onError
Additional options
useFetch accepts all UseAsyncDataOptions fields plus:
Override the cache key. Required when
url is a function.HTTP method (
'GET', 'POST', etc.). Accepts a ref or getter.Base URL prepended to relative request URLs. On the server, Nuxe falls back to
runtimeConfig.app.baseURL, then NUXE_BASE_URL, then http://localhost:3000 when this is omitted.Query string parameters appended to the URL. Accepts a plain object or a reactive ref — changes trigger an automatic refresh.
Alias for
query.Request body for
POST/PUT/PATCH requests. Accepts a reactive ref.Additional request headers. Accepts a reactive ref.
Called after every successful response. Receives the full fetch context including the raw
Response object.Called when the request fails (network error or non-2xx response).
error.status and error.statusText are populated for HTTP errors.When
url is a function (e.g. a getter that computes the URL dynamically), options.key is required. Nuxe cannot derive a stable cache key from a function reference, so omitting key throws an error at runtime: [nuxe] useFetch: options.key is required when url is a function.$fetch and createFetch
$fetch is the low-level HTTP client available in both components and server routes. It is re-exported by Nuxe with one enhancement: on the server, relative URLs are automatically prefixed with the application’s base URL (read from runtimeConfig.app.baseURL, then NUXE_BASE_URL, then http://localhost:3000) so server-to-server calls to your own API routes work without any configuration. It is built on ofetch.
$fetch directly when you need fire-and-forget mutations or when you want full control over the request without the reactive overhead of useFetch.
createFetch creates a new pre-configured $fetch instance. Use it to share a base URL, default headers, or interceptors across many requests without repeating options on every call.