Open History to see the last commit
1import type {
2 DefaultError,
3 DefaultedQueryObserverOptions,
4 Query,
5 QueryKey,
6 QueryObserver,
7 QueryObserverResult,
8} from '@tanstack/query-core'
9import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'
10β
11/**
12 * The default `throwOnError` of the suspense hooks: throws the error to the
13 * nearest error boundary only if the query has no data to show.
14 *
15 * @param _error - The error of the query. Unused.
16 * @param query - The query that errored.
17 * @returns `true` if the query has no data.
18 */
19export const defaultThrowOnError = <
20 TQueryFnData = unknown,
21 TError = DefaultError,
22 TData = TQueryFnData,
23 TQueryKey extends QueryKey = QueryKey,
24>(
25 _error: TError,
26 query: Query<TQueryFnData, TError, TData, TQueryKey>,
27) => query.state.data === undefined
28β
29/**
30 * Raises `staleTime` and a numeric `gcTime` to at least 1000ms when `suspense`
31 * is enabled, so a component that remounts after suspending doesn't refetch
32 * right away or find the query garbage collected. A `'static'` `staleTime` is
33 * kept as is. Mutates the options.
34 *
35 * @param defaultedOptions - The defaulted query options to adjust.
36 */
37export const ensureSuspenseTimers = (
38 defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,
39) => {
40 if (defaultedOptions.suspense) {
41 // Handle staleTime to ensure minimum 1000ms in Suspense mode
42 // This prevents unnecessary refetching when components remount after suspending
43 const MIN_SUSPENSE_TIME_MS = 1000
44β
45 const clamp = (value: number | 'static' | undefined) =>
46 value === 'static'
47 ? value
48 : Math.max(value ?? MIN_SUSPENSE_TIME_MS, MIN_SUSPENSE_TIME_MS)
49β
50 const originalStaleTime = defaultedOptions.staleTime
51 defaultedOptions.staleTime =
52 typeof originalStaleTime === 'function'
53 ? (...args) => clamp(originalStaleTime(...args))
54 : clamp(originalStaleTime)
55β
56 if (typeof defaultedOptions.gcTime === 'number') {
57 defaultedOptions.gcTime = Math.max(
58 defaultedOptions.gcTime,
59 MIN_SUSPENSE_TIME_MS,
60 )
61 }
62 }
63}
64β
65/**
66 * Checks whether a query should suspend: `suspense` is enabled and the result
67 * is still `pending`.
68 *
69 * @param defaultedOptions - The defaulted query options, if any.
70 * @param result - The current result of the observer.
71 * @returns `true` if the component should suspend.
72 */
73export const shouldSuspend = (
74 defaultedOptions:
75 | DefaultedQueryObserverOptions<any, any, any, any, any>
76 | undefined,
77 result: QueryObserverResult<any, any>,
78) => defaultedOptions?.suspense && result.isPending
79β
80/**
81 * Fetches the query for a suspending component, without affecting the
82 * observer's own result. If the fetch fails, the reset state of the error
83 * boundary is cleared, so the error is thrown to it.
84 *
85 * @param defaultedOptions - The defaulted query options to fetch with.
86 * @param observer - The observer of the query.
87 * @param errorResetBoundary - The value of the nearest
88 * `QueryErrorResetBoundary`.
89 * @returns A promise that resolves once the fetch settles. It never rejects.
90 */
91export const fetchOptimistic = <
92 TQueryFnData,
93 TError,
94 TData,
95 TQueryData,
96 TQueryKey extends QueryKey,
97>(
98 defaultedOptions: DefaultedQueryObserverOptions<
99 TQueryFnData,
100 TError,
101 TData,
102 TQueryData,
103 TQueryKey
104 >,
105 observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
106 errorResetBoundary: QueryErrorResetBoundaryValue,
107) =>
108 observer.fetchOptimistic(defaultedOptions).catch(() => {
109 errorResetBoundary.clearReset()
110 })