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

devtools.md

228 lines10.4 KB

1---
2id: devtools
3title: Devtools
4---
5​
6Wave your hands in the air and shout hooray because Preact Query comes with dedicated devtools! πŸ₯³
7​
8When you begin your Preact Query journey, you'll want these devtools by your side. They help visualize all of the inner workings of Preact Query and will likely save you hours of debugging if you find yourself in a pinch!
9​
10> For Chrome, Firefox, and Edge users: Third-party browser extensions are available for debugging TanStack Query directly in browser DevTools. These provide the same functionality as the framework-specific devtools packages:
12> - <img alt="Chrome logo" src="https://www.google.com/chrome/static/images/chrome-logo.svg" width="16" height="16" class="inline mr-1 not-prose" /> <a href="https://chromewebstore.google.com/detail/tanstack-query-devtools/annajfchloimdhceglpgglpeepfghfai">Devtools for Chrome</a>
13> - <img alt="Firefox logo" src="https://upload.wikimedia.org/wikipedia/commons/a/a0/Firefox_logo%2C_2019.svg" width="16" height="16" class="inline mr-1 not-prose" /> <a href="https://addons.mozilla.org/en-US/firefox/addon/tanstack-query-devtools/">Devtools for Firefox</a>
14> - <img alt="Edge logo" src="https://upload.wikimedia.org/wikipedia/commons/9/98/Microsoft_Edge_logo_%282019%29.svg" width="16" height="16" class="inline mr-1 not-prose" /> <a href="https://microsoftedge.microsoft.com/addons/detail/tanstack-query-devtools/edmdpkgkacmjopodhfolmphdenmddobj">Devtools for Edge</a>
15​
16## Install and Import the Devtools
17​
18The devtools are a separate package that you need to install:
19​
20```sh
21npm i @tanstack/preact-query-devtools
22```
23​
24or
25​
26```sh
27pnpm add @tanstack/preact-query-devtools
28```
29​
30or
31​
32```sh
33yarn add @tanstack/preact-query-devtools
34```
35​
36or
37​
38```sh
39bun add @tanstack/preact-query-devtools
40```
41​
42You can import the devtools like this:
43​
44```tsx
45import { PreactQueryDevtools } from '@tanstack/preact-query-devtools'
46```
47​
48By default, Preact Query Devtools are only included in bundles when `process.env.NODE_ENV === 'development'`, so you don't need to worry about excluding them during a production build.
49​
50## Floating Mode
51​
52Floating Mode will mount the devtools as a fixed, floating element in your app and provide a toggle in the corner of the screen to show and hide the devtools. This toggle state will be stored and remembered in localStorage across reloads.
53​
54Place the following code as high in your Preact app as you can. The closer it is to the root of the page, the better it will work!
55​
56```tsx
57import { PreactQueryDevtools } from '@tanstack/preact-query-devtools'
58​
59function App() {
60 return (
61 <QueryClientProvider client={queryClient}>
62 {/* The rest of your application */}
63 <PreactQueryDevtools initialIsOpen={false} />
64 </QueryClientProvider>
65 )
67```
68​
69### Options
70​
71- `initialIsOpen: boolean`
72 - Set this `true` if you want the dev tools to default to being open
73- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
74 - Defaults to `bottom-right`
75 - The position of the TanStack logo to open and close the devtools panel
76- `position?: "top" | "bottom" | "left" | "right"`
77 - Defaults to `bottom`
78 - The position of the Preact Query devtools panel
79- `client?: QueryClient`,
80 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
81- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
82 - Use this to predefine some errors that can be triggered on your queries. Initializer will be called (with the specific query) when that error is toggled on from the UI. It must return an Error.
83- `styleNonce?: string`
84 - Deprecated. The devtools no longer add a `<style>` element, so they need no nonce. This option does nothing.
85- `shadowDOMTarget?: ShadowRoot`
86 - The shadow root to add the devtools styles to. By default, the styles are added to the shadow root the devtools render in, or to the document when they do not render in a shadow root.
87- `trustedTypesPolicy?: { createHTML: (html: string) => unknown }`
88 - The Trusted Types policy the devtools use to create their HTML templates. Pass one when your page enforces `require-trusted-types-for 'script'`. The devtools only pass it their own markup, so a policy that returns its input is enough, for example `trustedTypes.createPolicy('devtools', { createHTML: (html) => html })`.
89- `hideDisabledQueries?: boolean`
90 - Defaults to `false`.
91 - Set this to true to hide disabled queries from the devtools panel.
92- `theme?: "light" | "dark" | "system"`
93 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
94 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
95​
96## Embedded Mode
97​
98Embedded mode will show the development tools as a fixed element in your application, so you can use our panel in your own development tools.
99​
100Place the following code as high in your Preact app as you can. The closer it is to the root of the page, the better it will work!
101​
102```tsx
103import { PreactQueryDevtoolsPanel } from '@tanstack/preact-query-devtools'
104import { useState } from 'preact/hooks'
105​
106function App() {
107 const [isOpen, setIsOpen] = useState(false)
108​
109 return (
110 <QueryClientProvider client={queryClient}>
111 {/* The rest of your application */}
112 <button
113 onClick={() => setIsOpen(!isOpen)}
114 >{`${isOpen ? 'Close' : 'Open'} the devtools panel`}</button>
115 {isOpen && <PreactQueryDevtoolsPanel onClose={() => setIsOpen(false)} />}
116 </QueryClientProvider>
117 )
119```
120​
121### Options
122​
123- `style?: CSSProperties`
124 - Custom styles for the devtools panel
125 - Default: `{ height: '100%', display: 'flex', flexDirection: 'column' }`
126 - The panel fills the height of its container, or is `500px` tall when the container has no set height.
127 - Example: `{ height: '100%' }`
128 - Example: `{ height: '100%', width: '100%' }`
129- `onClose?: () => void`
130 - Callback function that is called when the devtools panel is closed
131- `client?: QueryClient`,
132 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
133- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
134 - Use this to predefine some errors that can be triggered on your queries. Initializer will be called (with the specific query) when that error is toggled on from the UI. It must return an Error.
135- `styleNonce?: string`
136 - Deprecated. The devtools no longer add a `<style>` element, so they need no nonce. This option does nothing.
137- `shadowDOMTarget?: ShadowRoot`
138 - The shadow root to add the devtools styles to. By default, the styles are added to the shadow root the devtools render in, or to the document when they do not render in a shadow root.
139- `trustedTypesPolicy?: { createHTML: (html: string) => unknown }`
140 - The Trusted Types policy the devtools use to create their HTML templates. Pass one when your page enforces `require-trusted-types-for 'script'`. The devtools only pass it their own markup, so a policy that returns its input is enough, for example `trustedTypes.createPolicy('devtools', { createHTML: (html) => html })`.
141- `hideDisabledQueries?: boolean`
142 - Defaults to `false`.
143 - Set this to true to hide disabled queries from the devtools panel.
144- `theme?: "light" | "dark" | "system"`
145 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
146 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
147​
148## Filter queries and mutations
149​
150The **Filter** input above the query and mutation lists uses the **Key** scope by default. Select the scope button inside the input to switch between **Key**, **Data**, and **All**:
151​
152- **Key** matches the query hash, or the mutation key and submission time.
153- **Data** matches the query data, or the mutation variables or data, the same way **Search data** does below.
154- **All** matches either.
155​
156The selected scope is remembered across reloads, like the other filter settings.
157​
158## Search query and mutation data
159​
160Select a query or mutation and use **Search data** in its Data Explorer to search nested property names and primitive values. Search uses case-insensitive text matching and treats special characters as plain text.
161​
162Matching text is highlighted. Matching editable values have a highlighted input. The explorer opens matching branches and the first matching page in each paginated collection. Other pages with matches are highlighted so you can open them. You can still inspect other rows and edit query data.
163​
164The Data Explorer highlights up to 1,000 matches at a time.
165​
166Select **Clear** or press **Escape** in the search field to restore your manual expansion. Selecting another query or mutation clears the search, unless the filter scope is **Data** or **All**: then the search starts with the filter text. For a mutation, it starts only in the variables or data that match the filter.
167​
168## Devtools in production
169​
170Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
171​
172```tsx
173import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
174import { PreactQueryDevtools } from '@tanstack/preact-query-devtools'
175import { lazy, Suspense } from 'preact/compat'
176import { useEffect, useState } from 'preact/hooks'
177import { Example } from './Example'
178​
179const queryClient = new QueryClient()
180​
181const PreactQueryDevtoolsProduction = lazy(() =>
182 import('@tanstack/preact-query-devtools/build/modern/production.js').then(
183 (d) => ({
184 default: d.PreactQueryDevtools,
185 }),
186 ),
188​
189function App() {
190 const [showDevtools, setShowDevtools] = useState(false)
191​
192 useEffect(() => {
193 Object.assign(window, {
194 toggleDevtools: () => setShowDevtools((old) => !old),
195 })
196 }, [])
197​
198 return (
199 <QueryClientProvider client={queryClient}>
200 <Example />
201 <PreactQueryDevtools initialIsOpen />
202 {showDevtools && (
203 <Suspense fallback={null}>
204 <PreactQueryDevtoolsProduction />
205 </Suspense>
206 )}
207 </QueryClientProvider>
208 )
210​
211export default App
212```
213​
214With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
215​
216### Modern bundlers
217​
218If your bundler supports package exports, you can use the following import path:
219​
220```tsx
221const PreactQueryDevtoolsProduction = lazy(() =>
222 import('@tanstack/preact-query-devtools/production').then((d) => ({
223 default: d.PreactQueryDevtools,
224 })),
226```
227​
228For TypeScript, you would need to set `moduleResolution` to `'bundler'`, `'node16'`, or `'nodenext'` in your tsconfig.