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