Open History to see the last commit
1import { getCurrentScope, unref, watchEffect } from 'vue-demi'
2import { noop } from '@tanstack/query-core'
3import { useQueryClient } from './useQueryClient'
4import { cloneDeepUnref } from './utils'
5import type {
6 DefaultError,
7 DistributiveOmit,
8 InfiniteData,
9 InfiniteQueryExecuteOptions,
10 InfiniteQueryMode,
11 QueryKey,
12 SkipToken,
13} from '@tanstack/query-core'
14import type { QueryClient } from './queryClient'
15import type { MaybeRefDeep, MaybeRefOrGetter } from './types'
16β
17/**
18 * The options accepted by `usePrefetchInfiniteQuery` β everything you can pass to `queryClient.infiniteQuery`,
19 * except that `queryFn` can't be `skipToken`.
20 */
21export type UsePrefetchInfiniteQueryOptions<
22 TQueryFnData,
23 TError,
24 TData,
25 TQueryKey extends QueryKey,
26 TPageParam,
27 TMode extends InfiniteQueryMode | undefined = undefined,
28> = DistributiveOmit<
29 InfiniteQueryExecuteOptions<
30 TQueryFnData,
31 TError,
32 TData,
33 TQueryKey,
34 TPageParam,
35 TMode
36 >,
37 'queryFn'
38> & {
39 queryFn?: Exclude<
40 InfiniteQueryExecuteOptions<
41 TQueryFnData,
42 TError,
43 TData,
44 TQueryKey,
45 TPageParam,
46 TMode
47 >['queryFn'],
48 SkipToken
49 >
50}
51β
52/**
53 * Checks whether an option was passed as a getter function.
54 * @param value - The option value.
55 * @returns `true` if `value` is a function.
56 */
57function isGetter<T>(value: MaybeRefOrGetter<T>): value is () => T {
58 return typeof value === 'function'
59}
60β
61/**
62 * `usePrefetchInfiniteQuery` does not return anything β it fires a prefetch as a reactive side effect, useful
63 * for kicking off a fetch ahead of the component that will actually render the data with `useInfiniteQuery`.
64 * You can pass everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`,
65 * though `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required
66 * unless a default query function has been defined.
67 *
68 * `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
69 * as well as pageParam information, and should return a single variable that will be passed to your query
70 * function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
71 *
72 * The prefetch is skipped if the query already has any cached state β including a `pending`/`error` state left
73 * over from a previous attempt β so it won't refetch data that's already there or already in flight. It
74 * re-runs whenever a reactive dependency in `options` (built with `infiniteQueryOptions`, for example) changes.
75 *
76 * Fire this during render, before a suspense boundary that wraps a component using `useInfiniteQuery`'s
77 * `suspense()` β see the {@link https://tanstack.com/query/latest/docs/framework/vue/guides/suspense | Suspense guide}.
78 * @param options - A `ref`, plain value, or reactive getter resolving to the
79 * {@link UsePrefetchInfiniteQueryOptions} to use β everything you can pass to `queryClient.infiniteQuery`.
80 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
81 * will be used.
82 * @example
83 * ```vue
84 * <script setup lang="ts">
85 * import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/vue-query'
86 * import Projects from './Projects.vue'
87 *
88 * const projectsOptions = infiniteQueryOptions({
89 * queryKey: ['projects'],
90 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
91 * initialPageParam: 0,
92 * getNextPageParam: (lastPage) => lastPage.nextId,
93 * })
94 *
95 * // Fire the prefetch as soon as this component runs, before `Projects` mounts and calls `useInfiniteQuery`.
96 * usePrefetchInfiniteQuery(projectsOptions)
97 * </script>
98 *
99 * <template>
100 * <Projects />
101 * </template>
102 * ```
103 */
104export function usePrefetchInfiniteQuery<
105 TQueryFnData = unknown,
106 TError = DefaultError,
107 TData = InfiniteData<TQueryFnData>,
108 TQueryKey extends QueryKey = QueryKey,
109 TPageParam = unknown,
110>(
111 options: MaybeRefOrGetter<
112 MaybeRefDeep<
113 UsePrefetchInfiniteQueryOptions<
114 TQueryFnData,
115 TError,
116 TData,
117 TQueryKey,
118 TPageParam,
119 undefined
120 >
121 >
122 >,
123 queryClient?: QueryClient,
124): void
125export function usePrefetchInfiniteQuery<
126 TQueryFnData = unknown,
127 TError = DefaultError,
128 TData = InfiniteData<TQueryFnData>,
129 TQueryKey extends QueryKey = QueryKey,
130 TPageParam = unknown,
131>(
132 options: MaybeRefOrGetter<
133 MaybeRefDeep<
134 UsePrefetchInfiniteQueryOptions<
135 TQueryFnData,
136 TError,
137 TData,
138 TQueryKey,
139 TPageParam,
140 InfiniteQueryMode
141 >
142 >
143 >,
144 queryClient?: QueryClient,
145): void
146export function usePrefetchInfiniteQuery<
147 TQueryFnData = unknown,
148 TError = DefaultError,
149 TData = InfiniteData<TQueryFnData>,
150 TQueryKey extends QueryKey = QueryKey,
151 TPageParam = unknown,
152 TMode extends InfiniteQueryMode | undefined = InfiniteQueryMode | undefined,
153>(
154 options: MaybeRefOrGetter<
155 MaybeRefDeep<
156 UsePrefetchInfiniteQueryOptions<
157 TQueryFnData,
158 TError,
159 TData,
160 TQueryKey,
161 TPageParam,
162 TMode
163 >
164 >
165 >,
166 queryClient?: QueryClient,
167): void {
168 if (process.env.NODE_ENV === 'development') {
169 if (!getCurrentScope()) {
170 console.warn(
171 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
172 )
173 }
174 }
175β
176 const client = queryClient || useQueryClient()
177β
178 watchEffect(() => {
179 const resolvedOptions = isGetter(options) ? options() : unref(options)
180β
181 const clonedOptions: UsePrefetchInfiniteQueryOptions<
182 TQueryFnData,
183 TError,
184 TData,
185 TQueryKey,
186 TPageParam,
187 TMode
188 > = cloneDeepUnref(resolvedOptions)
189β
190 if (!client.getQueryState(clonedOptions.queryKey)) {
191 void client.infiniteQuery(clonedOptions).then(noop).catch(noop)
192 }
193 })
194}