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 if (client.isRestoring) {
104 client.isRestoring.value = false
105 }
106 options.clientPersisterOnSuccess?.(client)
107 })
108 }
109β
110 const cleanup = () => {
111 client.unmount()
112 persisterUnmount()
113 }
114β
115 if (app.onUnmount) {
116 app.onUnmount(cleanup)
117 } else {
118 const originalUnmount = app.unmount
119 app.unmount = function vueQueryUnmount() {
120 cleanup()
121 originalUnmount()
122 }
123 }
124β
125 if (isVue2) {
126 app.mixin({
127 beforeCreate() {
128 // HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L30
129 if (!this._provided) {
130 const provideCache = {}
131 Object.defineProperty(this, '_provided', {
132 get: () => provideCache,
133 set: (v) => Object.assign(provideCache, v),
134 })
135 }
136β
137 this._provided[clientKey] = client
138β
139 if (process.env.NODE_ENV === 'development') {
140 if (this === this.$root && options.enableDevtoolsV6Plugin) {
141 setupDevtools(this, client)
142 }
143 }
144 },
145 })
146 } else {
147 app.provide(clientKey, client)
148β
149 if (process.env.NODE_ENV === 'development') {
150 if (options.enableDevtoolsV6Plugin) {
151 setupDevtools(app, client)
152 }
153 }
154 }
155 },
156}