Automatically refetch queries when they become stale in Pinia Colada
npm install @pinia/colada-plugin-auto-refetchAutomatically refetch queries when they become stale in Pinia Colada.
``sh`
npm install @pinia/colada-plugin-auto-refetch
`js
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:
`ts
// 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
},
})
``