Open History to see the last commit
1---
2id: devtools
3title: Devtools
4---
5β
6## Enable devtools
7β
8The devtools help you debug and inspect your queries and mutations. You can enable the devtools by adding `withDevtools` to `provideTanStackQuery`.
9β
10By default, Angular Query Devtools are only included in development mode bundles, so you don't need to worry about excluding them during a production build.
11β
12```ts
13import {
14 QueryClient,
15 provideTanStackQuery,
16} from '@tanstack/angular-query-experimental'
17β
18import { withDevtools } from '@tanstack/angular-query-experimental/devtools'
19β
20export const appConfig: ApplicationConfig = {
21 providers: [provideTanStackQuery(new QueryClient(), withDevtools())],
22}
23```
24β
25## Devtools in production
26β
27Devtools are automatically excluded from production builds. However, it might be desirable to lazy load the devtools in production.
28β
29To use `withDevtools` in production builds, import using the `production` sub-path. The function exported from the production subpath is identical to the main one, but won't be excluded from production builds.
30β
31```ts
32import { withDevtools } from '@tanstack/angular-query-experimental/devtools/production'
33```
34β
35To control when devtools are loaded, you can use the `loadDevtools` option. This is particularly useful if you want to load devtools based on environment configurations or user interaction. For instance, you might have a test environment running in production mode but still require devtools to be available.
36β
37When not setting the option or setting it to 'auto', the devtools will be loaded automatically when Angular runs in development mode.
38β
39```ts
40import { withDevtools } from '@tanstack/angular-query-experimental/devtools'
41β
42provideTanStackQuery(new QueryClient(), withDevtools())
43β
44// which is equivalent to
45provideTanStackQuery(
46 new QueryClient(),
47 withDevtools(() => ({ loadDevtools: 'auto' })),
48)
49```
50β
51When setting the option to true, the devtools will be loaded in both development and production mode.
52β
53This is particularly useful if you want to load devtools based on environment configurations. E.g. you could set this to true either when `isDevMode()` is true or when the application is running on your production build staging environment.
54β
55```ts
56// Make sure to use the production sub-path to load devtools in production builds
57import { withDevtools } from '@tanstack/angular-query-experimental/devtools/production'
58β
59provideTanStackQuery(
60 new QueryClient(),
61 withDevtools(() => ({ loadDevtools: true })),
62)
63```
64β
65When setting the option to false, the devtools will not be loaded.
66β
67```ts
68provideTanStackQuery(
69 new QueryClient(),
70 withDevtools(() => ({ loadDevtools: false })),
71)
72```
73β
74## Derive options through reactivity
75β
76Options are passed to `withDevtools` from a callback function to support reactivity through signals. In the following example
77a signal is created from a RxJS observable that emits on a keyboard shortcut. When the derived signal is set to true, the devtools are lazily loaded.
78β
79> If you don't need devtools in production builds, don't use the `production` sub-path. Even though most of the devtools are lazy loaded on-demand, code is needed for on-demand loading and option handling. When importing devtools from `@tanstack/angular-query-experimental/devtools`, all devtools code will be excluded from your build and no lazy chunks will be created, minimizing deployment size.
80β
81The example below always loads devtools in development mode and loads on-demand in production mode when a keyboard shortcut is pressed.
82β
83```ts
84import { Injectable, isDevMode } from '@angular/core'
85import { fromEvent, map, scan } from 'rxjs'
86import { toSignal } from '@angular/core/rxjs-interop'
87β
88@Injectable({ providedIn: 'root' })
89export class DevtoolsOptionsManager {
90 loadDevtools = toSignal(
91 fromEvent<KeyboardEvent>(document, 'keydown').pipe(
92 map(
93 (event): boolean =>
94 event.metaKey && event.ctrlKey && event.shiftKey && event.key === 'D',
95 ),
96 scan((acc, curr) => acc || curr, isDevMode()),
97 ),
98 {
99 initialValue: isDevMode(),
100 },
101 )
102}
103```
104β
105If you want to use an injectable such as a service in the callback you can use `deps`. The injected value will be passed as parameter to the callback function.
106β
107This is similar to `deps` in Angular's [`useFactory`](https://angular.dev/guide/di/dependency-injection-providers#factory-providers-usefactory) provider.
108β
109```ts
110// ...
111import { withDevtools } from '@tanstack/angular-query-experimental/devtools/production'
112β
113export const appConfig: ApplicationConfig = {
114 providers: [
115 provideHttpClient(),
116 provideTanStackQuery(
117 new QueryClient(),
118 withDevtools(
119 (devToolsOptionsManager: DevtoolsOptionsManager) => ({
120 loadDevtools: devToolsOptionsManager.loadDevtools(),
121 }),
122 {
123 // `deps` is used to inject and pass `DevtoolsOptionsManager` to the `withDevtools` callback.
124 deps: [DevtoolsOptionsManager],
125 },
126 ),
127 ),
128 ],
129}
130```
131β
132### Options returned from the callback
133β
134Of these options `loadDevtools`, `client`, `position`, `errorTypes`, `buttonPosition`, and `initialIsOpen` support reactivity through signals.
135β
136- `loadDevtools?: 'auto' | boolean`
137 - Defaults to `auto`: lazily loads devtools when in development mode. Skips loading in production mode.
138 - Use this to control if the devtools are loaded.
139- `initialIsOpen?: Boolean`
140 - Set this to `true` if you want the tools to default to being open
141- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
142 - Defaults to `bottom-right`
143 - The position of the TanStack logo to open and close the devtools panel
144 - If `relative`, the button is placed in the location that you render the devtools.
145- `position?: "top" | "bottom" | "left" | "right"`
146 - Defaults to `bottom`
147 - The position of the Angular Query devtools panel
148- `client?: QueryClient`,
149 - Use this to use a custom QueryClient. Otherwise, the QueryClient provided through `provideTanStackQuery` will be injected.
150- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
151 - 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.
152- `styleNonce?: string`
153 - 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.
154- `shadowDOMTarget?: ShadowRoot`
155 - Default behavior will apply the devtool's styles to the head tag within the DOM.
156 - 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.