sweet_crm/plugins/vue-query.ts

37 lines
891 B
TypeScript
Raw Normal View History

2024-07-30 08:14:15 +00:00
import type {
2024-07-31 07:38:59 +00:00
DehydratedState,
VueQueryPluginOptions,
2024-07-30 08:14:15 +00:00
} from '@tanstack/vue-query'
import {
2024-07-31 07:38:59 +00:00
QueryClient,
VueQueryPlugin,
dehydrate,
hydrate,
2024-07-30 08:14:15 +00:00
} from '@tanstack/vue-query'
// Nuxt 3 app aliases
import { defineNuxtPlugin, useState } from '#imports'
export default defineNuxtPlugin((nuxt) => {
2024-07-31 07:38:59 +00:00
const vueQueryState = useState<DehydratedState | null>('vue-query')
2024-07-30 08:14:15 +00:00
2024-07-31 07:38:59 +00:00
// Modify your Vue Query global settings here
const queryClient = new QueryClient({
defaultOptions: { queries: { staleTime: 5000 } },
})
const options: VueQueryPluginOptions = { queryClient }
2024-07-30 08:14:15 +00:00
2024-07-31 07:38:59 +00:00
nuxt.vueApp.use(VueQueryPlugin, options)
2024-07-30 08:14:15 +00:00
2024-07-31 07:38:59 +00:00
if (import.meta.server) {
nuxt.hooks.hook('app:rendered', () => {
vueQueryState.value = dehydrate(queryClient)
})
}
2024-07-30 08:14:15 +00:00
2024-07-31 07:38:59 +00:00
if (import.meta.client) {
nuxt.hooks.hook('app:created', () => {
hydrate(queryClient, vueQueryState.value)
})
}
2024-07-30 08:14:15 +00:00
})