@pinia/colada-plugin-auto-refetch
v0.2.4
Published
Automatically refetch queries when they become stale in Pinia Colada
Maintainers
Readme
Automatically refetch queries when they become stale in Pinia Colada.
Installation
npm install @pinia/colada-plugin-auto-refetchUsage
import { PiniaColadaAutoRefetch } from '@pinia/colada-plugin-auto-refetch'
// Pass the plugin to Pinia Colada options
app.use(PiniaColada, {
// ...
plugins: [
PiniaColadaAutoRefetch({ autoRefetch: true }), // enable globally
],
})You can customize the refetch behavior individually for each query with the autoRefetch option:
// true: use existing staleTime
useQuery({
key: ['todos'],
query: getTodos,
staleTime: 10000,
autoRefetch: true, // refetch every 10 seconds
})
// Number: custom interval in milliseconds
useQuery({
key: ['posts'],
query: getPosts,
staleTime: 5000,
autoRefetch: 10000, // refetch every 10 seconds
})
// Function: conditional refetching based on query state
useQuery({
key: () => ['tasks', id.value],
query: getDocuments,
autoRefetch: (state) => {
// Polling based on data state
return state.data?.running ? 5000 : false
},
})