Open History to see the last commit
1import { isVue2 } from 'vue-demi'
2import { environmentManager } from '@tanstack/query-core'
3β
4import { QueryClient } from './queryClient'
5import { getClientKey } from './utils'
6import { setupDevtools } from './devtools/devtools'
7import type { QueryClientConfig } from './types'
8β
9type ClientPersister = (client: QueryClient) => [() => void, Promise<void>]
10β
11interface CommonOptions {
12 /** Enables the legacy Vue Devtools v6 plugin panel. Prefer `@tanstack/vue-query-devtools` for Vue 3 apps. */
13 enableDevtoolsV6Plugin?: boolean
14 /**
15 * Distinguishes multiple `QueryClient`s provided in the same app β pass the same key to `useQueryClient` to
16 * pick this one. Only needed when installing more than one client.
17 */
18 queryClientKey?: string
19 /**
20 * Restores a persisted cache before the app mounts. While it runs, `queryClient.isRestoring` is `true`, which
21 * suppresses fetches β see `usePersistQueryClient` in `@tanstack/query-persist-client-core` for building one.
22 */
23 clientPersister?: ClientPersister
24 /** Called once `clientPersister`'s returned promise resolves and the cache has finished restoring. */
25 clientPersisterOnSuccess?: (client: QueryClient) => void
26}
27β
28interface ConfigOptions extends CommonOptions {
29 /** Options for the `QueryClient` the plugin constructs. Ignored if `queryClient` is also set. */
30 queryClientConfig?: QueryClientConfig
31}
32β
33interface ClientOptions extends CommonOptions {
34 /** Use your own `QueryClient` instance instead of having the plugin construct one from `queryClientConfig`. */
35 queryClient?: QueryClient
36}
37β
38/**
39 * The options accepted by `VueQueryPlugin`: either a `queryClient` to install, or a `queryClientConfig` for the
40 * client the plugin creates, plus the shared options.
41 */
42export type VueQueryPluginOptions = ConfigOptions | ClientOptions
43β
44/**
45 * Installs a `QueryClient` on the Vue app, making it available to every descendant component through
46 * `useQueryClient` β the Vue equivalent of React's `QueryClientProvider`, but wired up as an app-level plugin
47 * instead of a wrapping component.
48 * @example
49 * ```ts
50 * import { createApp } from 'vue'
51 * import { VueQueryPlugin } from '@tanstack/vue-query'
52 *
53 * const app = createApp(App)
54 * app.use(VueQueryPlugin)
55 * ```
56 * @example
57 * Pass a `queryClient` you constructed yourself β useful for SSR, where you need a fresh `QueryClient` per
58 * request, or when the same instance also needs to be used outside of Vue components:
59 * ```ts
60 * import { QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
61 *
62 * const queryClient = new QueryClient()
63 * app.use(VueQueryPlugin, { queryClient })
64 * ```
65 * @example
66 * Or pass `queryClientConfig` to let the plugin construct the `QueryClient` for you, with your own defaults:
67 * ```ts
68 * app.use(VueQueryPlugin, {
69 * queryClientConfig: {
70 * defaultOptions: { queries: { staleTime: 5 * 1000 } },
71 * },
72 * })
73 * ```
74 */
75export const VueQueryPlugin = {
76 install: (app: any, options: VueQueryPluginOptions = {}) => {
77 const clientKey = getClientKey(options.queryClientKey)
78 let client: QueryClient
79β
80 if ('queryClient' in options && options.queryClient) {
81 client = options.queryClient
82 } else {
83 const clientConfig =
84 'queryClientConfig' in options ? options.queryClientConfig : undefined
85 client = new QueryClient(clientConfig)
86 }
87β
88 if (!environmentManager.isServer()) {
89 client.mount()
90 }
91β
92 let persisterUnmount = () => {
93 // noop
94 }
95β
96 if (options.clientPersister) {
97 if (client.isRestoring) {
98 client.isRestoring.value = true
99 }
100 const [unmount, promise] = options.clientPersister(client)
101 persisterUnmount = unmount
102 promise.then(
103 () => {
104 if (client.isRestoring) {
105 client.isRestoring.value = false
106 }
107 options.clientPersisterOnSuccess?.(client)
108 },
109 () => {
110 if (client.isRestoring) {
111 client.isRestoring.value = false
112 }
113 },
114 )
115 }
116β
117 const cleanup = () => {
118 client.unmount()
119 persisterUnmount()
120 }
121β
122 if (app.onUnmount) {
123 app.onUnmount(cleanup)
124 } else {
125 const originalUnmount = app.unmount
126 app.unmount = function vueQueryUnmount() {
127 cleanup()
128 originalUnmount()
129 }
130 }
131β
132 if (isVue2) {
133 app.mixin({
134 beforeCreate() {
135 // HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L30
136 if (!this._provided) {
137 const provideCache = {}
138 Object.defineProperty(this, '_provided', {
139 get: () => provideCache,
140 set: (v) => Object.assign(provideCache, v),
141 })
142 }
143β
144 this._provided[clientKey] = client
145β
146 if (process.env.NODE_ENV === 'development') {
147 if (this === this.$root && options.enableDevtoolsV6Plugin) {
148 setupDevtools(this, client)
149 }
150 }
151 },
152 })
153 } else {
154 app.provide(clientKey, client)
155β
156 if (process.env.NODE_ENV === 'development') {
157 if (options.enableDevtoolsV6Plugin) {
158 setupDevtools(app, client)
159 }
160 }
161 }
162 },
163}