Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

vueQueryPlugin.ts

155 lines4.8 KB

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
27​
28interface ConfigOptions extends CommonOptions {
29 /** Options for the `QueryClient` the plugin constructs. Ignored if `queryClient` is also set. */
30 queryClientConfig?: QueryClientConfig
32​
33interface ClientOptions extends CommonOptions {
34 /** Use your own `QueryClient` instance instead of having the plugin construct one from `queryClientConfig`. */
35 queryClient?: QueryClient
37​
38export type VueQueryPluginOptions = ConfigOptions | ClientOptions
39​
40/**
41 * Installs a `QueryClient` on the Vue app, making it available to every descendant component through
42 * `useQueryClient` β€” the Vue equivalent of React's `QueryClientProvider`, but wired up as an app-level plugin
43 * instead of a wrapping component.
44 *
45 * @example
46 * ```ts
47 * import { createApp } from 'vue'
48 * import { VueQueryPlugin } from '@tanstack/vue-query'
49 *
50 * const app = createApp(App)
51 * app.use(VueQueryPlugin)
52 * ```
53 *
54 * @example
55 * Pass a `queryClient` you constructed yourself β€” useful for SSR, where you need a fresh `QueryClient` per
56 * request, or when the same instance also needs to be used outside of Vue components:
57 * ```ts
58 * import { QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
59 *
60 * const queryClient = new QueryClient()
61 * app.use(VueQueryPlugin, { queryClient })
62 * ```
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 if (client.isRestoring) {
103 client.isRestoring.value = false
104 }
105 options.clientPersisterOnSuccess?.(client)
106 })
107 }
108​
109 const cleanup = () => {
110 client.unmount()
111 persisterUnmount()
112 }
113​
114 if (app.onUnmount) {
115 app.onUnmount(cleanup)
116 } else {
117 const originalUnmount = app.unmount
118 app.unmount = function vueQueryUnmount() {
119 cleanup()
120 originalUnmount()
121 }
122 }
123​
124 if (isVue2) {
125 app.mixin({
126 beforeCreate() {
127 // HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L30
128 if (!this._provided) {
129 const provideCache = {}
130 Object.defineProperty(this, '_provided', {
131 get: () => provideCache,
132 set: (v) => Object.assign(provideCache, v),
133 })
134 }
135​
136 this._provided[clientKey] = client
137​
138 if (process.env.NODE_ENV === 'development') {
139 if (this === this.$root && options.enableDevtoolsV6Plugin) {
140 setupDevtools(this, client)
141 }
142 }
143 },
144 })
145 } else {
146 app.provide(clientKey, client)
147​
148 if (process.env.NODE_ENV === 'development') {
149 if (options.enableDevtoolsV6Plugin) {
150 setupDevtools(app, client)
151 }
152 }
153 }
154 },