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

usePrefetchInfiniteQuery.ts

139 lines4.5 KB

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 QueryKey,
11 SkipToken,
12} from '@tanstack/query-core'
13import type { QueryClient } from './queryClient'
14import type { MaybeRefDeep, MaybeRefOrGetter } from './types'
15​
16export type UsePrefetchInfiniteQueryOptions<
17 TQueryFnData,
18 TError,
19 TData,
20 TQueryKey extends QueryKey,
21 TPageParam,
22> = DistributiveOmit<
23 InfiniteQueryExecuteOptions<
24 TQueryFnData,
25 TError,
26 TData,
27 TQueryKey,
28 TPageParam
29 >,
30 'queryFn'
31> & {
32 queryFn?: Exclude<
33 InfiniteQueryExecuteOptions<
34 TQueryFnData,
35 TError,
36 TData,
37 TQueryKey,
38 TPageParam
39 >['queryFn'],
40 SkipToken
41 >
43​
44function isGetter<T>(value: MaybeRefOrGetter<T>): value is () => T {
45 return typeof value === 'function'
47​
48/**
49 * `usePrefetchInfiniteQuery` does not return anything β€” it fires a prefetch as a reactive side effect, useful
50 * for kicking off a fetch ahead of the component that will actually render the data with `useInfiniteQuery`.
51 * You can pass everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`,
52 * though `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required
53 * unless a default query function has been defined.
54 *
55 * `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
56 * as well as pageParam information, and should return a single variable that will be passed to your query
57 * function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
58 *
59 * The prefetch is skipped if the query already has any cached state β€” including a `pending`/`error` state left
60 * over from a previous attempt β€” so it won't refetch data that's already there or already in flight. It
61 * re-runs whenever a reactive dependency in `options` (built with `infiniteQueryOptions`, for example) changes.
62 *
63 * Fire this during render, before a suspense boundary that wraps a component using `useInfiniteQuery`'s
64 * `suspense()` β€” see the {@link https://tanstack.com/query/latest/docs/framework/vue/guides/suspense | Suspense guide}.
65 *
66 * @param options - A `ref`, plain value, or reactive getter resolving to the
67 * {@link UsePrefetchInfiniteQueryOptions} to use β€” everything you can pass to `queryClient.infiniteQuery`.
68 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
69 * will be used.
70 * @returns `void` β€” nothing is returned.
71 *
72 * @example
73 * ```vue
74 * <script setup lang="ts">
75 * import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/vue-query'
76 * import Projects from './Projects.vue'
77 *
78 * const projectsOptions = infiniteQueryOptions({
79 * queryKey: ['projects'],
80 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
81 * initialPageParam: 0,
82 * getNextPageParam: (lastPage) => lastPage.nextId,
83 * })
84 *
85 * // Fire the prefetch as soon as this component runs, before `Projects` mounts and calls `useInfiniteQuery`.
86 * usePrefetchInfiniteQuery(projectsOptions)
87 * </script>
88 *
89 * <template>
90 * <Projects />
91 * </template>
92 * ```
93 */
94export function usePrefetchInfiniteQuery<
95 TQueryFnData = unknown,
96 TError = DefaultError,
97 TData = InfiniteData<TQueryFnData>,
98 TQueryKey extends QueryKey = QueryKey,
99 TPageParam = unknown,
100>(
101 options: MaybeRefOrGetter<
102 MaybeRefDeep<
103 UsePrefetchInfiniteQueryOptions<
104 TQueryFnData,
105 TError,
106 TData,
107 TQueryKey,
108 TPageParam
109 >
110 >
111 >,
112 queryClient?: QueryClient,
113): void {
114 if (process.env.NODE_ENV === 'development') {
115 if (!getCurrentScope()) {
116 console.warn(
117 '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.',
118 )
119 }
120 }
121​
122 const client = queryClient || useQueryClient()
123​
124 watchEffect(() => {
125 const resolvedOptions = isGetter(options) ? options() : unref(options)
126​
127 const clonedOptions: UsePrefetchInfiniteQueryOptions<
128 TQueryFnData,
129 TError,
130 TData,
131 TQueryKey,
132 TPageParam
133 > = cloneDeepUnref(resolvedOptions)
134​
135 if (!client.getQueryState(clonedOptions.queryKey)) {
136 void client.infiniteQuery(clonedOptions).then(noop).catch(noop)
137 }
138 })