Skip to content

Commit dd33940

Browse files
Enable Tanstack Query Devtools (#2807)
* support tanstack query devtools * tweak logging * init devtools even on prod
1 parent af485de commit dd33940

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

frontends/api/src/ssr/usePrefetchWarnings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { userQueries } from "../hooks/user/queries"
44

55
const logQueries = (...args: [...string[], Query[]]) => {
66
const queries = args.pop() as Query[]
7+
console.group("Prefetch Info")
78
console.info(...args)
89
console.table(
910
queries.map((query) => ({
@@ -15,6 +16,7 @@ const logQueries = (...args: [...string[], Query[]]) => {
1516
})),
1617
["hash", "status", "observerCount", "disabled"],
1718
)
19+
console.groupEnd()
1820
}
1921

2022
const PREFETCH_EXEMPT_QUERIES = [userQueries.me().queryKey]

frontends/main/src/app/getQueryClient.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,48 @@ function getQueryClient() {
141141
// have a suspense boundary BELOW the creation of the query client
142142
if (!browserQueryClient) {
143143
browserQueryClient = makeBrowserQueryClient()
144+
initBrowserDevTools(browserQueryClient)
144145
}
146+
145147
return browserQueryClient
146148
}
147149
}
148150

151+
declare global {
152+
interface Window {
153+
__TANSTACK_QUERY_CLIENT__?: QueryClient
154+
}
155+
}
156+
/**
157+
* Initialize Tanstack Query browser dev tools.
158+
*
159+
* - This can be enabled in production, it only affects people with the extension installed.
160+
* - See https://tanstack.com/query/v5/docs/framework/react/devtools
161+
*/
162+
function initBrowserDevTools(browserQueryClient: QueryClient) {
163+
if (process.env.NODE_ENV === "development") {
164+
// Log a tip about using Tanstack Query devtools
165+
// But only once... We don't want to spam devs on every hot reload
166+
if (!window.__TANSTACK_QUERY_CLIENT__) {
167+
const parts = [
168+
{
169+
text: "API Query Debugging\n",
170+
style: "color: blue; font-weight: bold;",
171+
},
172+
{
173+
text: "Tip: You can use Tanstack Query devtools to inspect query data, even if the data was fetched server-side. See https://tanstack.com/query/v5/docs/framework/react/devtools",
174+
style: "color: unset; font-weight: normal;",
175+
},
176+
]
177+
console.log(
178+
parts.map(({ text }) => `%c${text}`).join(""),
179+
...parts.map(({ style }) => style),
180+
)
181+
}
182+
}
183+
window.__TANSTACK_QUERY_CLIENT__ = browserQueryClient
184+
}
185+
149186
/**
150187
* Use a custom focus manager for react-query's refetchOnWindowFocus feature.
151188
*

0 commit comments

Comments
 (0)