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

QueryStrictMode.tsx

45 lines1.6 KB

1'use client'
2import { DevQueryStrictMode } from './QueryStrictModeDev'
3import type { QueryStrictModeMissingPrefetchError } from './QueryStrictModeError'
4import type {
5 QueryStrictModeMissingPrefetchEvent,
6 QueryStrictModeProps,
7 QueryStrictModeUnusedPrefetchEvent,
8} from './QueryStrictModeTypes'
9​
10/**
11 * Reports prefetching issues in development. See
12 * {@link QueryStrictModeMissingPrefetchEvent} and {@link QueryStrictModeUnusedPrefetchEvent}.
13 *
14 * By default, a missing prefetch throws a {@link QueryStrictModeMissingPrefetchError} to an error
15 * boundary, and an unused prefetch is logged with `console.warn`. Pass `handleMissingPrefetch` or
16 * `handleUnusedPrefetch` to filter problems, or to log or throw them instead.
17 *
18 * Only affects development builds; returns children in production.
19 * @param props - The `children` to check, and optionally `handleMissingPrefetch`,
20 * `handleUnusedPrefetch`, an `unusedPrefetchTimeout`, and a `queryClient`.
21 * @returns The `children`.
22 * @example
23 * ```tsx
24 * import { QueryStrictMode } from '@tanstack/react-query'
25 *
26 * function App() {
27 * return (
28 * <QueryStrictMode
29 * handleMissingPrefetch={(event) => {
30 * if (!event.query.meta?.allowWaterfall) console.warn(event.message)
31 * }}
32 * >
33 * <Routes />
34 * </QueryStrictMode>
35 * )
36 * }
37 * ```
38 */
39// A single expression, so that bundlers drop `DevQueryStrictMode` once `NODE_ENV` is replaced
40export const QueryStrictMode = (props: QueryStrictModeProps) =>
41 process.env.NODE_ENV === 'production' ? (
42 props.children
43 ) : (
44 <DevQueryStrictMode {...props} />
45 )