Open History to see the last commit
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:
11>
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 )
66}
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 )
118}
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 ),
187)
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 )
209}
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 })),
225)
226```
227β
228For TypeScript, you would need to set `moduleResolution` to `'bundler'`, `'node16'`, or `'nodenext'` in your tsconfig.