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

devtools.md

156 lines6.6 KB

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())],
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' })),
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 })),
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 })),
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 )
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 ],
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.