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

devtools.md

147 lines6.9 KB

1---
2id: devtools
3title: Devtools
4---
5​
6> 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:
7>
8> - <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>
9> - <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>
10> - <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>
11​
12## Install and Import the Devtools
13​
14The devtools are a separate package that you need to install:
15​
16```sh
17npm i @tanstack/svelte-query-devtools
18```
19​
20or
21​
22```sh
23pnpm add @tanstack/svelte-query-devtools
24```
25​
26or
27​
28```sh
29yarn add @tanstack/svelte-query-devtools
30```
31​
32or
33​
34```sh
35bun add @tanstack/svelte-query-devtools
36```
37​
38You can import the devtools like this:
39​
40```ts
41import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
42```
43​
44By default, Svelte Query Devtools are only rendered in development, using `DEV` from `esm-env`. In production, the devtools component renders nothing.
45​
46## Floating Mode
47​
48Floating 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.
49​
50Place the following code as high in your Svelte app as you can. The closer it is to the root of the page, the better it will work!
51​
52```ts
53<script>
54 import { QueryClientProvider } from '@tanstack/svelte-query'
55 import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
56</script>
57​
58<QueryClientProvider client={queryClient}>
59 {/* The rest of your application */}
60 <SvelteQueryDevtools />
61</QueryClientProvider>
62```
63​
64### Options
65​
66- `initialIsOpen: boolean`
67 - Set this `true` if you want the dev tools to default to being open
68- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
69 - Defaults to `bottom-right`
70 - The position of the TanStack logo to open and close the devtools panel
71 - If `relative`, the button is placed in the location that you render the devtools.
72- `position?: "top" | "bottom" | "left" | "right"`
73 - Defaults to `bottom`
74 - The position of the Svelte Query devtools panel
75- `client?: QueryClient`,
76 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
77- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
78 - 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.
79- `styleNonce?: string`
80 - Deprecated. The devtools no longer add a `<style>` element, so they need no nonce. This option does nothing.
81- `shadowDOMTarget?: ShadowRoot`
82 - 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.
83- `trustedTypesPolicy?: { createHTML: (html: string) => unknown }`
84 - 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 })`.
85- `hideDisabledQueries?: boolean`
86 - Defaults to `false`.
87 - Set this to true to hide disabled queries from the devtools panel.
88- `theme?: Theme`
89 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
90 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
91​
92## Filter queries and mutations
93​
94The **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**:
95​
96- **Key** matches the query hash, or the mutation key and submission time.
97- **Data** matches the query data, or the mutation variables or data, the same way **Search data** does below.
98- **All** matches either.
99​
100The selected scope is remembered across reloads, like the other filter settings.
101​
102## Search query and mutation data
103​
104Select 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.
105​
106Matching 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.
107​
108The Data Explorer highlights up to 1,000 matches at a time.
109​
110Select **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.
111​
112## Devtools in production
113​
114Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
115​
116```svelte
117<script lang="ts">
118 import { onMount } from 'svelte'
119 import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query'
120 import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
121 import Example from './Example.svelte'
122​
123 const queryClient = new QueryClient()
124​
125 let showDevtools = $state(false)
126​
127 onMount(() => {
128 Object.assign(window, {
129 toggleDevtools: () => (showDevtools = !showDevtools),
130 })
131 })
132</script>
133​
134<QueryClientProvider client={queryClient}>
135 <Example />
136 <SvelteQueryDevtools initialIsOpen />
137 {#if showDevtools}
138 {#await import('@tanstack/svelte-query-devtools/production') then { SvelteQueryDevtools: SvelteQueryDevtoolsProduction }}
139 <SvelteQueryDevtoolsProduction />
140 {/await}
141 {/if}
142</QueryClientProvider>
143```
144​
145With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
146​
147For TypeScript, you would need to set `moduleResolution` to `'bundler'`, `'node16'`, or `'nodenext'` in your tsconfig.