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