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

devtools.md

216 lines7.8 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## Enable devtools
13​
14Add the devtools package (in addition to `@tanstack/angular-query`):
15​
16```bash
17npm install @tanstack/angular-query-devtools
18```
19​
20The devtools help you debug and inspect your queries and mutations. You can enable the devtools by adding `withDevtools` to `provideTanStackQuery`.
21​
22By default, Angular Query Devtools only load in development.
23​
24```ts
25import { QueryClient, provideTanStackQuery } from '@tanstack/angular-query'
26​
27import { withDevtools } from '@tanstack/angular-query-devtools'
28​
29export const appConfig: ApplicationConfig = {
30 providers: [provideTanStackQuery(() => new QueryClient(), withDevtools())],
32```
33​
34## Devtools in production
35​
36If you need the real implementation in production, import from the `production` entrypoint.
37​
38```ts
39import { withDevtools } from '@tanstack/angular-query-devtools/production'
40```
41​
42To control when devtools are rendered, use the `loadDevtools` option.
43​
44When omitted or set to `'auto'`, devtools are only rendered in development mode.
45​
46```ts
47import { withDevtools } from '@tanstack/angular-query-devtools'
48​
49providers: [provideTanStackQuery(() => new QueryClient(), withDevtools())]
50​
51// which is equivalent to
52providers: [
53 provideTanStackQuery(
54 () => new QueryClient(),
55 withDevtools(() => ({ loadDevtools: 'auto' })),
56 ),
58```
59​
60When setting the option to true, the devtools will be rendered in both development and production mode.
61​
62This is useful if you want to load devtools based on [Angular environment configurations](https://angular.dev/tools/cli/environments). E.g. you could set this to true when the application is running on your production build staging environment.
63​
64```ts
65import { environment } from './environments/environment'
66// Make sure to use the production sub-path to load devtools in production builds
67import { withDevtools } from '@tanstack/angular-query-devtools/production'
68​
69providers: [
70 provideTanStackQuery(
71 () => new QueryClient(),
72 withDevtools(() => ({ loadDevtools: environment.loadDevtools })),
73 ),
75```
76​
77When setting the option to false, the devtools will not be rendered.
78​
79```ts
80providers: [
81 provideTanStackQuery(
82 () => new QueryClient(),
83 withDevtools(() => ({ loadDevtools: false })),
84 ),
86```
87​
88### Webpack file replacements
89​
90Some webpack-based Angular builders do not apply package export conditions when
91they bundle application code. Use the explicit `production` and `stub`
92entrypoints with Angular CLI file replacements to keep the devtools dependency
93out of production bundles.
94​
95Create an application-level import that can be replaced:
96​
97```ts
98// src/app/query-devtools.ts
99export { withDevtools } from '@tanstack/angular-query-devtools/production'
100```
101​
102```ts
103// src/app/query-devtools.stub.ts
104export { withDevtools } from '@tanstack/angular-query-devtools/stub'
105```
106​
107Import the application-level module from your config:
108​
109```ts
110import { withDevtools } from './query-devtools'
111```
112​
113Then configure the production build:
114​
115```json
117 "configurations": {
118 "production": {
119 "fileReplacements": [
120 {
121 "replace": "src/app/query-devtools.ts",
122 "with": "src/app/query-devtools.stub.ts"
123 }
124 ]
125 }
126 }
128```
129​
130The same pattern is available for the programmatic panel:
131`@tanstack/angular-query-devtools/devtools-panel/production` and
132`@tanstack/angular-query-devtools/devtools-panel/stub`.
133​
134## Reactive options
135​
136Mutable options can be Angular signals. For example, a signal derived from a keyboard shortcut can show devtools on demand:
137​
138```ts
139import { Injectable, isDevMode } from '@angular/core'
140import { fromEvent, map, scan } from 'rxjs'
141import { toSignal } from '@angular/core/rxjs-interop'
142​
143@Injectable({ providedIn: 'root' })
144export class DevtoolsOptionsManager {
145 loadDevtools = toSignal(
146 fromEvent<KeyboardEvent>(document, 'keydown').pipe(
147 map(
148 (event): boolean =>
149 event.metaKey && event.ctrlKey && event.shiftKey && event.key === 'D',
150 ),
151 scan((acc, curr) => acc || curr, isDevMode()),
152 ),
153 {
154 initialValue: isDevMode(),
155 },
156 )
158```
159​
160The callback runs once in an Angular injection context, so it can call
161`inject()`. Return signals as option values to update them reactively:
162​
163```ts
164// ...
165// πŸ‘‡ Import from the production sub-path to make devtools available in production builds
166import { inject } from '@angular/core'
167import { withDevtools } from '@tanstack/angular-query-devtools/production'
168​
169export const appConfig: ApplicationConfig = {
170 providers: [
171 provideHttpClient(),
172 provideTanStackQuery(
173 () => new QueryClient(),
174 withDevtools(() => ({
175 loadDevtools: inject(DevtoolsOptionsManager).loadDevtools,
176 })),
177 ),
178 ],
180```
181​
182### Options returned from the callback
183​
184`loadDevtools`, `client`, `position`, `errorTypes`, `buttonPosition`,
185`initialIsOpen`, and `theme` accept either their documented static value or a
186signal containing that value.
187​
188`styleNonce`, `shadowDOMTarget`, and `hideDisabledQueries` are construction-time
189options and do not accept signals.
190​
191- `loadDevtools?: 'auto' | boolean`
192 - Omit or `'auto'`: load devtools only in development mode.
193 - Use this to control whether devtools load when using the `/production` import.
194- `initialIsOpen?: Boolean`
195 - Set this to `true` if you want the tools to default to being open
196- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
197 - Defaults to `bottom-right`
198 - The position of the TanStack logo to open and close the devtools panel
199 - If `relative`, the button is placed in the location that you render the devtools.
200- `position?: "top" | "bottom" | "left" | "right"`
201 - Defaults to `bottom`
202 - The position of the Angular Query devtools panel
203- `client?: QueryClient`,
204 - Use this to use a custom QueryClient. Otherwise, the QueryClient provided through `provideTanStackQuery` will be injected.
205- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
206 - 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.
207- `styleNonce?: string`
208 - 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.
209- `shadowDOMTarget?: ShadowRoot`
210 - Default behavior will apply the devtool's styles to the head tag within the DOM.
211 - 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.
212- `hideDisabledQueries?: boolean`
213 - Set this to true to hide disabled queries from the devtools panel.
214- `theme?: "light" | "dark" | "system"`
215 - Defaults to `system`.
216 - Sets the theme of the devtools panel.