Skip to content

Commit

Permalink
feat: add vue query
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Nov 9, 2023
1 parent 36764c4 commit d985e7a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@heroicons/vue": "^2.0.18",
"@sidebase/nuxt-auth": "0.6.0-rc.1",
"@tailwindcss/forms": "^0.5.6",
"@tanstack/vue-query": "^5.8.1",
"@tanstack/vue-virtual": "3.0.0-beta.68",
"@vueuse/components": "^10.5.0",
"@vueuse/core": "^10.5.0",
Expand Down
44 changes: 44 additions & 0 deletions plugins/vue-query.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { DehydratedState, VueQueryPluginOptions } from '@tanstack/vue-query'
import { dehydrate, hydrate, QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
// Nuxt 3 app aliases
import { defineNuxtPlugin, useState } from '#imports'

/**
* @see https://github.com/TanStack/query/blob/main/examples/vue/nuxt3/plugins/vue-query.ts
*/
export default defineNuxtPlugin((nuxt) => {
const vueQueryState = useState<DehydratedState | null>('vue-query')

const queryClient = new QueryClient({
defaultOptions: {
queries: {
// Do not refetch
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
refetchIntervalInBackground: false,
refetchInterval: false,

staleTime: 5000,

// Disable retries, it is handled by Nuxt
retry: false
}
}
})
const options: VueQueryPluginOptions = { queryClient }

nuxt.vueApp.use(VueQueryPlugin, options)

if (process.server) {
nuxt.hooks.hook('app:rendered', () => {
vueQueryState.value = dehydrate(queryClient)
})
}

if (process.client) {
nuxt.hooks.hook('app:created', () => {
hydrate(queryClient, vueQueryState.value)
})
}
})
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d985e7a

Please sign in to comment.