Skip to content

Commit 05455c8

Browse files
authored
fix(angular-query-devtools): fix SSR errors (TanStack#6724)
By running devtools code on browser platform only
1 parent c9cd303 commit 05455c8

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

packages/angular-query-devtools-experimental/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
"devDependencies": {
3535
"@angular/compiler-cli": "^17.0.8",
3636
"@angular/core": "^17.0.8",
37+
"@angular/common": "^17.0.8",
3738
"@tanstack/angular-query-experimental": "workspace:*",
3839
"ng-packagr": "^17.0.3",
3940
"typescript": "5.2.2",
4041
"zone.js": "^0.14.2"
4142
},
4243
"peerDependencies": {
44+
"@angular/common": "^17",
4345
"@angular/core": "^17",
4446
"@tanstack/angular-query-experimental": "workspace:^"
4547
},

packages/angular-query-devtools-experimental/src/angular-query-devtools.component.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isPlatformBrowser } from '@angular/common'
12
import * as queryDevtools from '@tanstack/query-devtools'
23
import {
34
injectQueryClient,
@@ -8,8 +9,10 @@ import {
89
Component,
910
ElementRef,
1011
Input,
12+
PLATFORM_ID,
1113
ViewChild,
1214
booleanAttribute,
15+
inject,
1316
} from '@angular/core'
1417
import { QueryClient } from '@tanstack/angular-query-experimental'
1518
import type {
@@ -57,6 +60,7 @@ export class AngularQueryDevtools
5760
/**
5861
* The position of the Angular Query devtools panel.
5962
* 'top' | 'bottom' | 'left' | 'right'
63+
* Defaults to 'bottom'.
6064
* @example
6165
* <angular-query-devtools position="bottom" />
6266
*/
@@ -85,30 +89,35 @@ export class AngularQueryDevtools
8589

8690
#devtools?: TanstackQueryDevtools
8791

88-
readonly #injectedClient: QueryClient | null = injectQueryClient({
89-
optional: true,
90-
})
92+
readonly #isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
93+
94+
readonly #injectedClient: QueryClient | null = this.#isBrowser
95+
? injectQueryClient({
96+
optional: true,
97+
})
98+
: null
9199

92100
ngOnChanges(changes: SimpleChanges) {
93101
if (!this.#devtools) return
94102
if (changes['client']) {
95103
this.#devtools.setClient(this.#getAppliedQueryClient())
96104
}
97-
if (changes['buttonPosition'] && this.buttonPosition) {
105+
if (changes['buttonPosition'] && this.buttonPosition !== undefined) {
98106
this.#devtools.setButtonPosition(this.buttonPosition)
99107
}
100-
if (changes['position'] && this.position) {
108+
if (changes['position'] && this.position !== undefined) {
101109
this.#devtools.setPosition(this.position)
102110
}
103-
if (changes['initialIsOpen'] && this.initialIsOpen) {
111+
if (changes['initialIsOpen'] && this.initialIsOpen !== undefined) {
104112
this.#devtools.setInitialIsOpen(this.initialIsOpen)
105113
}
106-
if (changes['errorTypes'] && this.errorTypes) {
114+
if (changes['errorTypes'] && this.errorTypes !== undefined) {
107115
this.#devtools.setErrorTypes(this.errorTypes)
108116
}
109117
}
110118

111119
ngAfterViewInit() {
120+
if (!this.#isBrowser) return
112121
const devtools = new queryDevtools.TanstackQueryDevtools({
113122
client: this.#getAppliedQueryClient(),
114123
queryFlavor: 'Angular Query',

packages/angular-query-experimental/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"zone.js": "^0.14.2"
5959
},
6060
"peerDependencies": {
61-
"@angular/core": "^17"
61+
"@angular/core": "^17",
62+
"@angular/common": "^17"
6263
},
6364
"module": "build/fesm2022/tanstack-angular-query-experimental.mjs",
6465
"types": "build/index.d.ts",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)