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