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

devtools.md

126 lines5.2 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 - Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
81- `shadowDOMTarget?: ShadowRoot`
82 - Default behavior will apply the devtool's styles to the head tag within the DOM.
83 - Use this to pass a shadow DOM target to the devtools so that the styles will be applied within the shadow DOM instead of within the head tag in the light DOM.
84- `hideDisabledQueries?: boolean`
85 - Defaults to `false`.
86 - Set this to true to hide disabled queries from the devtools panel.
87- `theme?: Theme`
88 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
89 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
90​
91## Devtools in production
92​
93Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
94​
95```svelte
96<script lang="ts">
97 import { onMount } from 'svelte'
98 import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query'
99 import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
100 import Example from './Example.svelte'
101​
102 const queryClient = new QueryClient()
103​
104 let showDevtools = $state(false)
105​
106 onMount(() => {
107 Object.assign(window, {
108 toggleDevtools: () => (showDevtools = !showDevtools),
109 })
110 })
111</script>
112​
113<QueryClientProvider client={queryClient}>
114 <Example />
115 <SvelteQueryDevtools initialIsOpen />
116 {#if showDevtools}
117 {#await import('@tanstack/svelte-query-devtools/production') then { SvelteQueryDevtools: SvelteQueryDevtoolsProduction }}
118 <SvelteQueryDevtoolsProduction />
119 {/await}
120 {/if}
121</QueryClientProvider>
122```
123​
124With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
125​
126For TypeScript, you would need to set `moduleResolution` to `'bundler'`, `'node16'`, or `'nodenext'` in your tsconfig.