nuxt-async-data-addons
v0.0.1
Published
Collection of common async data addons for Nuxt
Maintainers
Readme
nuxt-async-data-addons
A collection of common async data addons for Nuxt, for use with createUseAsyncData and createUseFetch.
Nuxt 4.6 added defineUseAsyncDataAddon and defineUseFetchAddon, which package reusable behavior for your data fetching composables. This module saves you from writing the common ones yourself.
Setup
npx nuxt module add nuxt-async-data-addonsAddons are auto-imported. Add the ones you need to createUseAsyncData or createUseFetch, optionally with default options for your project, then override those options at the call site when needed:
export const useAppAsyncData = createUseAsyncData({
addons: [minDurationUseAsyncDataAddon({ minDuration: 400 })],
})export const useAppFetch = createUseFetch({
addons: [minDurationUseFetchAddon({ minDuration: 400 })],
})<script setup lang="ts">
const { data: mountains } = await useAppAsyncData(
'mountains',
() => $fetch('/api/mountains'),
)
const { data: rivers } = await useAppFetch('/api/rivers', {
minDuration: 0,
})
</script>Addons
Minimum duration
| Addon | Use with |
| --- | --- |
| minDurationUseAsyncDataAddon | createUseAsyncData |
| minDurationUseFetchAddon | createUseFetch |
Keeps a request pending for at least minDuration milliseconds, so that loading states don't flash when it finishes quickly.
| Option | Type | Description |
| --- | --- | --- |
| minDuration | number | Minimum duration of the request in milliseconds. The addon does nothing when it is unset or 0. |
- It only applies on the client, so it never slows down server-side rendering. Data already in the payload is not re-fetched during hydration, so there is no wait there either.
- A request that fails early is also held for the minimum duration, so errors don't flash either.
- Aborting the request, or starting a new one for the same key, drops the remaining wait.
Contributing
# install dependencies
pnpm install
# generate type stubs and prepare the playgrounds
pnpm dev:prepare
# start the playground on Nuxt 4
pnpm dev
# start the playground on the latest Nuxt 5 nightly
pnpm dev:v5
# lint and type check
pnpm lint
pnpm test:types
# run the tests (the e2e tests need a browser: pnpm playwright-core install chromium)
pnpm testEach addon has its own file in src/runtime/addons, where every export is auto-imported, its own page in playground/app/pages, and its own checks in test/e2e/addons, registered in describeAddons.ts. Each addon is a function that takes default options. Where it applies to both composables, a file exports one per composable, such as minDurationUseAsyncDataAddon and minDurationUseFetchAddon. playground-v5 extends playground as a layer, so the same pages run on both Nuxt versions.
Commits follow Conventional Commits, which uppt uses to prepare releases. Commits and pull requests about a specific addon use its name as the scope, such as fix(minDuration): ....
