@hitch42/applet 0.1.1-beta.1 → 0.1.1-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_internal/client.js +120 -11
- package/dist/_internal/log-context.d.ts +34 -0
- package/dist/_internal/log-context.js +2 -0
- package/dist/_internal/server.d.ts +14 -1
- package/dist/_internal/server.js +122 -118
- package/dist/applet.js +53 -29
- package/dist/build-Bh_S5BUv.js +2 -0
- package/dist/{build-Rk3xDZV7.js → build-CEfFoIZZ.js} +30 -14
- package/dist/ci-BKw14X2h.js +69 -0
- package/dist/client.d.ts +3 -3
- package/dist/{contract-DMrpbaLJ.d.ts → contract-CsUpTBe0.d.ts} +1 -1
- package/dist/{deploy-D3BGDqgC.js → deploy-wGYjwXrx.js} +249 -35
- package/dist/email-DBTVhNB_.js +62 -0
- package/dist/{generate-CH0VQmUi.js → generate-BEIYs17x.js} +79 -25
- package/dist/generate-Dox-OYt6.js +2 -0
- package/dist/headers-DkNr9MLJ.js +17 -0
- package/dist/index.d.ts +123 -11
- package/dist/index.js +142 -26
- package/dist/{init-BS1Y-eNL.js → init-6oGPM46R.js} +17 -18
- package/dist/load-config-xEdTXs0y.js +27 -0
- package/dist/log-context-zu8U9Sjm.js +73 -0
- package/dist/login-CUx3TmFQ.js +12 -0
- package/dist/{logs-m00pzr_O.js → logs-DSjuEN0Q.js} +20 -22
- package/dist/{manifest-BQCAxATS.d.ts → manifest-C6_F4bL8.d.ts} +9 -0
- package/dist/{manifest-DyoJ1MIL.js → manifest-CJSL9GuC.js} +25 -3
- package/dist/me-BW7Paary.js +32 -0
- package/dist/{project-CxN-Dtc7.js → project-BRZGd6xq.js} +227 -34
- package/dist/prompt-ChS-grYO.js +29 -0
- package/dist/query-cache-CXmAyLv3.js +82 -0
- package/dist/react.d.ts +3 -2
- package/dist/react.js +98 -80
- package/dist/schedules-C8_Svr4h.js +42 -0
- package/dist/server.d.ts +9 -4
- package/dist/server.js +29 -8
- package/dist/telephony-protocol-D-nf3Gca.d.ts +76 -0
- package/dist/telephony.d.ts +97 -0
- package/dist/telephony.js +726 -0
- package/dist/{upload-YbRKEqR6.d.ts → upload-DKZzz5kW.d.ts} +6 -6
- package/dist/{variables-CP7Ce3Np.js → variables-DT5RyHdH.js} +14 -39
- package/dist/vite.d.ts +3 -60
- package/dist/vite.js +503 -34
- package/dist/workflow-engine-BOTzURKM.js +111 -0
- package/dist/{auth-DaVK0k5R.js → workspace-D0dnnlxL.js} +75 -40
- package/package.json +19 -6
- package/scaffold/.agents/skills/hitch-applets/SKILL.md +60 -15
- package/scaffold/.agents/skills/hitch-applets/references/email-tracking.md +49 -0
- package/scaffold/.agents/skills/hitch-applets/references/files.md +3 -3
- package/scaffold/.agents/skills/hitch-applets/references/manifest.md +29 -2
- package/scaffold/.agents/skills/hitch-applets/references/markdown.md +2 -0
- package/scaffold/.agents/skills/hitch-applets/references/performance.md +74 -0
- package/scaffold/.agents/skills/hitch-applets/references/records.md +32 -8
- package/scaffold/.agents/skills/hitch-applets/references/schedules.md +7 -2
- package/scaffold/.agents/skills/hitch-applets/references/server.md +0 -3
- package/scaffold/.agents/skills/hitch-applets/references/workflows.md +11 -3
- package/scaffold/.agents/skills/hitch-cli/SKILL.md +129 -0
- package/scaffold/.agents/skills/hitch-data/SKILL.md +52 -0
- package/scaffold/.agents/skills/hitch-data/references/attributes.md +127 -0
- package/scaffold/.agents/skills/hitch-data/references/limits.md +64 -0
- package/scaffold/.agents/skills/hitch-theory/SKILL.md +151 -0
- package/scaffold/.agents/skills/react-best-practices/SKILL.md +127 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-effect-event-deps.md +50 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-init-once.md +42 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-use-latest.md +39 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-cheap-condition-before-await.md +37 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-defer-await.md +82 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-dependencies.md +48 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-parallel.md +24 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-analyzable-paths.md +42 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-barrel-imports.md +39 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-conditional.md +35 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-defer-third-party.md +46 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-dynamic-imports.md +38 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-preload.md +40 -0
- package/scaffold/.agents/skills/react-best-practices/rules/client-event-listeners.md +78 -0
- package/scaffold/.agents/skills/react-best-practices/rules/client-localstorage-schema.md +74 -0
- package/scaffold/.agents/skills/react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-batch-dom-css.md +110 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-cache-function-results.md +58 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-cache-property-access.md +28 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-cache-storage.md +56 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-combine-iterations.md +32 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-early-exit.md +50 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-flatmap-filter.md +51 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-index-maps.md +37 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-length-check-first.md +50 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-min-max-loop.md +82 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-request-idle-callback.md +104 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-activity.md +26 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-animate-svg-wrapper.md +38 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-conditional-render.md +32 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-hoist-jsx.md +36 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-resource-hints.md +81 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-script-defer-async.md +36 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-usetransition-loading.md +43 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-dependencies.md +45 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-derived-state-no-effect.md +40 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-derived-state.md +29 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-functional-setstate.md +77 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-lazy-state-init.md +56 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-memo-with-default-value.md +36 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-memo.md +44 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-move-effect-to-event.md +45 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-no-inline-components.md +75 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-simple-expression-in-memo.md +35 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-split-combined-hooks.md +64 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-transitions.md +40 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-use-deferred-value.md +59 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-use-ref-transient-values.md +73 -0
- package/scaffold/.agents/skills/tanstack-query/SKILL.md +136 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/automatic-invalidation-after-mutations.md +124 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/broadcast-realtime-and-multi-tab-synchronization.md +150 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/compose-query-with-tanstack-router-and-start.md +248 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/enforce-query-best-practices-with-eslint.md +167 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/persist-offline-and-restore-caches.md +181 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/query-data-and-forms.md +156 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/build-query-abstractions.md +130 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/cancel-queries-and-consume-abort-signals.md +176 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/concurrent-optimistic-updates.md +135 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/coordinate-query-execution.md +188 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/design-query-keys-and-options.md +182 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/fetch-and-observe-queries.md +193 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/implement-optimistic-updates-and-cache-writes.md +168 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/paginate-and-build-infinite-queries.md +188 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/seed-placeholder-select-and-transform-data.md +188 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/selectors-and-derived-state.md +145 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/tune-defaults-freshness-retries-and-refetching.md +172 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/understand-query-internals-and-observers.md +104 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/write-mutations-and-invalidate-related-queries.md +183 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/debug-with-devtools.md +169 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/handle-status-and-errors.md +119 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/shape-data-and-render-efficiently.md +184 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/use-framework-adapter-reactivity.md +185 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/use-suspense-and-error-boundaries.md +208 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/migrate-major-versions-and-codemods.md +169 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/prefetch-and-remove-request-waterfalls.md +228 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/setup-query-client-and-providers.md +161 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/ssr-hydration-and-streaming.md +209 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/test-query-code.md +160 -0
- package/scaffold/.agents/skills/tanstack-router/SKILL.md +134 -0
- package/scaffold/.agents/skills/tanstack-router/references/compositions/router-query.md +412 -0
- package/scaffold/.agents/skills/tanstack-router/references/react-router.md +495 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/auth-and-guards.md +495 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/code-splitting.md +318 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/data-loading.md +490 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/index.md +158 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/navigation.md +433 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/not-found-and-errors.md +436 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/path-params.md +383 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/search-params.md +343 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/ssr.md +462 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/type-safety.md +493 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-plugin.md +250 -0
- package/scaffold/.agents/skills/tanstack-router/references/virtual-file-routes.md +215 -0
- package/scaffold/.agents/skills/web-design-guidelines/SKILL.md +184 -0
- package/scaffold/.github/workflows/hitch.yml +15 -0
- package/scaffold/AGENTS.md +2 -2
- package/scaffold/applet/src/client/components/ui/alert.tsx +1 -1
- package/scaffold/applet/src/client/components/ui/button.tsx +1 -1
- package/scaffold/applet/src/client/components/ui/checkbox.tsx +1 -1
- package/scaffold/applet/src/client/components/ui/dialog.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/dropdown-menu.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/input-group.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/select.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/sheet.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/spinner.tsx +2 -1
- package/scaffold/applet/src/client/components/ui/tabs.tsx +1 -1
- package/scaffold/applet/src/client/lib/utils.ts +1 -6
- package/scaffold/applet/src/client/styles/globals.css +2 -0
- package/dist/build-CEku1lyb.js +0 -2
- package/dist/generate-6Nbt7uii.js +0 -2
- package/dist/load-config-BnxocLAQ.js +0 -56
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lifecycle/migrate-major-versions-and-codemods
|
|
3
|
+
description: >
|
|
4
|
+
Use this when migrating TanStack Query across React Query v3, React Query v4,
|
|
5
|
+
TanStack Query v5, Vue Query v5, Svelte Query v6, removed overloads,
|
|
6
|
+
object syntax, cacheTime to gcTime, removed query callbacks, keepPreviousData
|
|
7
|
+
migration, and query-codemods.
|
|
8
|
+
type: lifecycle
|
|
9
|
+
library: TanStack Query
|
|
10
|
+
library_version: "5.101.0"
|
|
11
|
+
sources:
|
|
12
|
+
- TanStack/query:docs/framework/react/guides/migrating-to-react-query-3.md
|
|
13
|
+
- TanStack/query:docs/framework/react/guides/migrating-to-react-query-4.md
|
|
14
|
+
- TanStack/query:docs/framework/react/guides/migrating-to-v5.md
|
|
15
|
+
- TanStack/query:docs/framework/vue/guides/migrating-to-v5.md
|
|
16
|
+
- TanStack/query:docs/framework/svelte/migrate-from-v5-to-v6.md
|
|
17
|
+
- TanStack/query:packages/query-codemods/package.json
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Setup
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { useQuery } from "@tanstack/react-query";
|
|
24
|
+
|
|
25
|
+
export function useTodos() {
|
|
26
|
+
return useQuery({
|
|
27
|
+
queryKey: ["todos"],
|
|
28
|
+
queryFn: async () => [{ id: 1 }],
|
|
29
|
+
gcTime: 5 * 60 * 1000,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Core Patterns
|
|
35
|
+
|
|
36
|
+
### Use object syntax everywhere
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { QueryClient, useQuery } from "@tanstack/react-query";
|
|
40
|
+
|
|
41
|
+
const queryClient = new QueryClient();
|
|
42
|
+
|
|
43
|
+
export function useTodo(id: string) {
|
|
44
|
+
return useQuery({ queryKey: ["todo", id], queryFn: async () => ({ id }) });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function prefetchTodo(id: string) {
|
|
48
|
+
return queryClient.prefetchQuery({
|
|
49
|
+
queryKey: ["todo", id],
|
|
50
|
+
queryFn: async () => ({ id }),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Rename cacheTime to gcTime
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
59
|
+
|
|
60
|
+
export const queryClient = new QueryClient({
|
|
61
|
+
defaultOptions: { queries: { gcTime: 10 * 60 * 1000 } },
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Migrate keepPreviousData
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { keepPreviousData, useQuery } from "@tanstack/react-query";
|
|
69
|
+
|
|
70
|
+
export function usePage(page: number) {
|
|
71
|
+
return useQuery({
|
|
72
|
+
queryKey: ["page", page],
|
|
73
|
+
queryFn: async () => ({ page }),
|
|
74
|
+
placeholderData: keepPreviousData,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Common Mistakes
|
|
80
|
+
|
|
81
|
+
### CRITICAL v4 overload syntax
|
|
82
|
+
|
|
83
|
+
Wrong:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { useQuery } from "@tanstack/react-query";
|
|
87
|
+
|
|
88
|
+
export function useTodos() {
|
|
89
|
+
return useQuery(["todos"], async () => [{ id: 1 }]);
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Correct:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { useQuery } from "@tanstack/react-query";
|
|
97
|
+
|
|
98
|
+
export function useTodos() {
|
|
99
|
+
return useQuery({ queryKey: ["todos"], queryFn: async () => [{ id: 1 }] });
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
v5 removed hook and client overloads in favor of a single object signature.
|
|
104
|
+
|
|
105
|
+
Source: TanStack/query:docs/framework/react/guides/migrating-to-v5.md
|
|
106
|
+
|
|
107
|
+
### HIGH Removed query callbacks
|
|
108
|
+
|
|
109
|
+
Wrong:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { useQuery } from "@tanstack/react-query";
|
|
113
|
+
|
|
114
|
+
export function useTodos() {
|
|
115
|
+
return useQuery({
|
|
116
|
+
queryKey: ["todos"],
|
|
117
|
+
queryFn: async () => [{ id: 1 }],
|
|
118
|
+
onSuccess: () => console.log("loaded"),
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Correct:
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import * as React from "react";
|
|
127
|
+
import { useQuery } from "@tanstack/react-query";
|
|
128
|
+
|
|
129
|
+
export function TodosLogger() {
|
|
130
|
+
const { data } = useQuery({
|
|
131
|
+
queryKey: ["todos"],
|
|
132
|
+
queryFn: async () => [{ id: 1 }],
|
|
133
|
+
});
|
|
134
|
+
React.useEffect(() => {
|
|
135
|
+
if (data) console.log("loaded");
|
|
136
|
+
}, [data]);
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
v5 removed query callbacks from queries; react to data changes outside the query options.
|
|
142
|
+
|
|
143
|
+
Source: TanStack/query:docs/framework/react/guides/migrating-to-v5.md
|
|
144
|
+
|
|
145
|
+
### HIGH cacheTime in v5
|
|
146
|
+
|
|
147
|
+
Wrong:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
151
|
+
|
|
152
|
+
export const queryClient = new QueryClient({
|
|
153
|
+
defaultOptions: { queries: { cacheTime: 60_000 } },
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Correct:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
161
|
+
|
|
162
|
+
export const queryClient = new QueryClient({
|
|
163
|
+
defaultOptions: { queries: { gcTime: 60_000 } },
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`cacheTime` was renamed to `gcTime` to describe garbage collection of unused queries.
|
|
168
|
+
|
|
169
|
+
Source: TanStack/query:docs/framework/react/guides/migrating-to-v5.md
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lifecycle/prefetch-and-remove-request-waterfalls
|
|
3
|
+
description: >
|
|
4
|
+
Use this when flattening request waterfalls with prefetchQuery,
|
|
5
|
+
prefetchInfiniteQuery, ensureQueryData, fetchQuery, usePrefetchQuery,
|
|
6
|
+
usePrefetchInfiniteQuery, route loaders, TanStack Router loaders, Start routes,
|
|
7
|
+
Suspense prefetching, and query-function prefetching.
|
|
8
|
+
type: lifecycle
|
|
9
|
+
library: TanStack Query
|
|
10
|
+
library_version: "5.101.0"
|
|
11
|
+
requires:
|
|
12
|
+
- core/design-query-keys-and-options
|
|
13
|
+
- core/fetch-and-observe-queries
|
|
14
|
+
- core/tune-defaults-freshness-retries-and-refetching
|
|
15
|
+
sources:
|
|
16
|
+
- TanStack/query:docs/framework/react/guides/prefetching.md
|
|
17
|
+
- TanStack/query:docs/framework/react/guides/request-waterfalls.md
|
|
18
|
+
- TanStack/query:docs/framework/react/reference/usePrefetchQuery.md
|
|
19
|
+
- TanStack/query:docs/framework/react/reference/usePrefetchInfiniteQuery.md
|
|
20
|
+
- TanStack/query:docs/reference/QueryClient.md
|
|
21
|
+
- TanStack/query:examples/react/react-router/src/routes/root.tsx
|
|
22
|
+
- TanStack/query:examples/react/react-router/src/routes/contact.tsx
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Setup
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { QueryClient, queryOptions } from "@tanstack/react-query";
|
|
29
|
+
|
|
30
|
+
const queryClient = new QueryClient();
|
|
31
|
+
|
|
32
|
+
const todosOptions = queryOptions({
|
|
33
|
+
queryKey: ["todos"],
|
|
34
|
+
queryFn: async () => [{ id: 1, title: "Ship" }],
|
|
35
|
+
staleTime: 60_000,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export function preloadTodos() {
|
|
39
|
+
return queryClient.prefetchQuery(todosOptions);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Core Patterns
|
|
44
|
+
|
|
45
|
+
### Use loaders for route-critical data
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { QueryClient, queryOptions } from "@tanstack/react-query";
|
|
49
|
+
|
|
50
|
+
const queryClient = new QueryClient();
|
|
51
|
+
const contactOptions = (contactId: string) =>
|
|
52
|
+
queryOptions({
|
|
53
|
+
queryKey: ["contact", contactId],
|
|
54
|
+
queryFn: async () => ({ id: contactId, name: "Ada" }),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export function contactLoader(contactId: string) {
|
|
58
|
+
return queryClient.ensureQueryData(contactOptions(contactId));
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Prefetch before Suspense can suspend
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { Suspense } from "react";
|
|
66
|
+
import { usePrefetchQuery, useSuspenseQuery } from "@tanstack/react-query";
|
|
67
|
+
|
|
68
|
+
const commentsOptions = {
|
|
69
|
+
queryKey: ["comments"],
|
|
70
|
+
queryFn: async () => [{ id: 1 }],
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
function Comments() {
|
|
74
|
+
const { data } = useSuspenseQuery(commentsOptions);
|
|
75
|
+
return <pre>{JSON.stringify(data)}</pre>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function CommentsSection() {
|
|
79
|
+
usePrefetchQuery(commentsOptions);
|
|
80
|
+
return (
|
|
81
|
+
<Suspense fallback={<p>Loading</p>}>
|
|
82
|
+
<Comments />
|
|
83
|
+
</Suspense>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Pick the right client method
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
92
|
+
|
|
93
|
+
const queryClient = new QueryClient();
|
|
94
|
+
|
|
95
|
+
export function getTodo(id: string) {
|
|
96
|
+
return queryClient.fetchQuery({
|
|
97
|
+
queryKey: ["todo", id],
|
|
98
|
+
queryFn: async () => ({ id }),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Use `fetchQuery` when the caller needs data or thrown errors; use `prefetchQuery` when it only needs to warm the cache.
|
|
104
|
+
|
|
105
|
+
## Common Mistakes
|
|
106
|
+
|
|
107
|
+
### HIGH Expecting prefetchQuery to return data
|
|
108
|
+
|
|
109
|
+
Wrong:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
113
|
+
|
|
114
|
+
const queryClient = new QueryClient();
|
|
115
|
+
const data = await queryClient.prefetchQuery({
|
|
116
|
+
queryKey: ["todo", 1],
|
|
117
|
+
queryFn: async () => ({ id: 1 }),
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Correct:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
125
|
+
|
|
126
|
+
const queryClient = new QueryClient();
|
|
127
|
+
const data = await queryClient.fetchQuery({
|
|
128
|
+
queryKey: ["todo", 1],
|
|
129
|
+
queryFn: async () => ({ id: 1 }),
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`prefetchQuery` returns void and swallows errors; `fetchQuery` returns data and throws.
|
|
134
|
+
|
|
135
|
+
Source: TanStack/query:docs/framework/react/guides/prefetching.md
|
|
136
|
+
|
|
137
|
+
### HIGH Prefetch staleTime only set on prefetch
|
|
138
|
+
|
|
139
|
+
Wrong:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { QueryClient, useQuery } from "@tanstack/react-query";
|
|
143
|
+
|
|
144
|
+
const queryClient = new QueryClient();
|
|
145
|
+
await queryClient.prefetchQuery({
|
|
146
|
+
queryKey: ["todos"],
|
|
147
|
+
queryFn: async () => [{ id: 1 }],
|
|
148
|
+
staleTime: 60_000,
|
|
149
|
+
});
|
|
150
|
+
export function useTodos() {
|
|
151
|
+
return useQuery({ queryKey: ["todos"], queryFn: async () => [{ id: 1 }] });
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Correct:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { QueryClient, queryOptions, useQuery } from "@tanstack/react-query";
|
|
159
|
+
|
|
160
|
+
const queryClient = new QueryClient();
|
|
161
|
+
const options = queryOptions({
|
|
162
|
+
queryKey: ["todos"],
|
|
163
|
+
queryFn: async () => [{ id: 1 }],
|
|
164
|
+
staleTime: 60_000,
|
|
165
|
+
});
|
|
166
|
+
await queryClient.prefetchQuery(options);
|
|
167
|
+
export function useTodos() {
|
|
168
|
+
return useQuery(options);
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Prefetch call options do not automatically configure the later observer.
|
|
173
|
+
|
|
174
|
+
Source: TanStack/query:docs/framework/react/guides/prefetching.md
|
|
175
|
+
|
|
176
|
+
### HIGH Suspense prefetch after suspension
|
|
177
|
+
|
|
178
|
+
Wrong:
|
|
179
|
+
|
|
180
|
+
```tsx
|
|
181
|
+
import * as React from "react";
|
|
182
|
+
import { useQueryClient, useSuspenseQuery } from "@tanstack/react-query";
|
|
183
|
+
|
|
184
|
+
export function Article() {
|
|
185
|
+
const queryClient = useQueryClient();
|
|
186
|
+
const article = useSuspenseQuery({
|
|
187
|
+
queryKey: ["article"],
|
|
188
|
+
queryFn: async () => ({ id: 1 }),
|
|
189
|
+
});
|
|
190
|
+
React.useEffect(() => {
|
|
191
|
+
queryClient.prefetchQuery({
|
|
192
|
+
queryKey: ["comments"],
|
|
193
|
+
queryFn: async () => [],
|
|
194
|
+
});
|
|
195
|
+
}, [queryClient]);
|
|
196
|
+
return <pre>{JSON.stringify(article.data)}</pre>;
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Correct:
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
import { Suspense } from "react";
|
|
204
|
+
import { usePrefetchQuery, useSuspenseQuery } from "@tanstack/react-query";
|
|
205
|
+
|
|
206
|
+
function Article() {
|
|
207
|
+
const article = useSuspenseQuery({
|
|
208
|
+
queryKey: ["article"],
|
|
209
|
+
queryFn: async () => ({ id: 1 }),
|
|
210
|
+
});
|
|
211
|
+
return <pre>{JSON.stringify(article.data)}</pre>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function ArticleRoute() {
|
|
215
|
+
usePrefetchQuery({ queryKey: ["comments"], queryFn: async () => [] });
|
|
216
|
+
return (
|
|
217
|
+
<Suspense fallback={<p>Loading</p>}>
|
|
218
|
+
<Article />
|
|
219
|
+
</Suspense>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Effects do not run until after a suspenseful query resolves, so they cannot flatten that waterfall.
|
|
225
|
+
|
|
226
|
+
Source: TanStack/query:docs/framework/react/guides/prefetching.md
|
|
227
|
+
|
|
228
|
+
See also: `compositions/compose-query-with-tanstack-router-and-start` for Router and Start loader prefetching.
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lifecycle/setup-query-client-and-providers
|
|
3
|
+
description: >
|
|
4
|
+
Use this when creating a TanStack Query QueryClient, QueryClientProvider, VueQueryPlugin,
|
|
5
|
+
provideTanStackQuery, Svelte QueryClientProvider, Angular providers, Lit controllers, or
|
|
6
|
+
SSR request-local clients. Covers stable client lifetime and provider wiring.
|
|
7
|
+
type: lifecycle
|
|
8
|
+
library: TanStack Query
|
|
9
|
+
library_version: "5.101.0"
|
|
10
|
+
sources:
|
|
11
|
+
- TanStack/query:docs/framework/react/quick-start.md
|
|
12
|
+
- TanStack/query:docs/framework/react/reference/QueryClientProvider.md
|
|
13
|
+
- TanStack/query:docs/eslint/stable-query-client.md
|
|
14
|
+
- TanStack/query:docs/framework/vue/guides/custom-client.md
|
|
15
|
+
- TanStack/query:docs/framework/angular/overview.md
|
|
16
|
+
- TanStack/query:docs/framework/lit/guides/reactive-controllers-vs-hooks.md
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Setup
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import * as React from "react";
|
|
23
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
24
|
+
|
|
25
|
+
export function AppProviders(props: { children: React.ReactNode }) {
|
|
26
|
+
const [queryClient] = React.useState(() => new QueryClient());
|
|
27
|
+
return <QueryClientProvider client={queryClient}>{props.children}</QueryClientProvider>;
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Core Patterns
|
|
32
|
+
|
|
33
|
+
### Create request-local SSR clients
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
37
|
+
|
|
38
|
+
export function createServerQueryClient() {
|
|
39
|
+
return new QueryClient({
|
|
40
|
+
defaultOptions: { queries: { staleTime: 60_000 } },
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Use adapter-native providers
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
49
|
+
|
|
50
|
+
export function createQueryClient() {
|
|
51
|
+
return new QueryClient({
|
|
52
|
+
defaultOptions: { queries: { retry: 2 } },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
React and Preact use `QueryClientProvider`; Vue uses `VueQueryPlugin`; Angular uses `provideTanStackQuery`; Svelte and Lit use their adapter provider/controller APIs.
|
|
58
|
+
|
|
59
|
+
### Pass an explicit client at integration boundaries
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
63
|
+
|
|
64
|
+
const queryClient = new QueryClient();
|
|
65
|
+
|
|
66
|
+
export function getTodos() {
|
|
67
|
+
return queryClient.ensureQueryData({
|
|
68
|
+
queryKey: ["todos"],
|
|
69
|
+
queryFn: async () => [{ id: 1, title: "Ship" }],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Common Mistakes
|
|
75
|
+
|
|
76
|
+
### CRITICAL New client on every render
|
|
77
|
+
|
|
78
|
+
Wrong:
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
82
|
+
|
|
83
|
+
export function App(props: { children: React.ReactNode }) {
|
|
84
|
+
const queryClient = new QueryClient();
|
|
85
|
+
return <QueryClientProvider client={queryClient}>{props.children}</QueryClientProvider>;
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Correct:
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import * as React from "react";
|
|
93
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
94
|
+
|
|
95
|
+
export function App(props: { children: React.ReactNode }) {
|
|
96
|
+
const [queryClient] = React.useState(() => new QueryClient());
|
|
97
|
+
return <QueryClientProvider client={queryClient}>{props.children}</QueryClientProvider>;
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Recreating the client discards caches and subscriptions on render.
|
|
102
|
+
|
|
103
|
+
Source: TanStack/query:docs/eslint/stable-query-client.md
|
|
104
|
+
|
|
105
|
+
### CRITICAL Shared SSR cache between users
|
|
106
|
+
|
|
107
|
+
Wrong:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
111
|
+
|
|
112
|
+
export const queryClient = new QueryClient();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Correct:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
119
|
+
|
|
120
|
+
export function createRequestQueryClient() {
|
|
121
|
+
return new QueryClient();
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
A module-level server client can leak one request's cached data into another request.
|
|
126
|
+
|
|
127
|
+
Source: TanStack/query:docs/framework/react/guides/ssr.md
|
|
128
|
+
|
|
129
|
+
### HIGH Ambiguous Lit fallback client
|
|
130
|
+
|
|
131
|
+
Wrong:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { QueryController } from "@tanstack/lit-query";
|
|
135
|
+
|
|
136
|
+
export class TodoElement extends HTMLElement {
|
|
137
|
+
todos = new QueryController(this, {
|
|
138
|
+
queryKey: ["todos"],
|
|
139
|
+
queryFn: async () => [],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Correct:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
148
|
+
import { QueryController } from "@tanstack/lit-query";
|
|
149
|
+
|
|
150
|
+
const queryClient = new QueryClient();
|
|
151
|
+
|
|
152
|
+
export class TodoElement extends HTMLElement {
|
|
153
|
+
todos = new QueryController(this, { queryKey: ["todos"], queryFn: async () => [] }, queryClient);
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Lit controllers need a clear provider or explicit client when the element is not under a provider tree.
|
|
158
|
+
|
|
159
|
+
Source: TanStack/query:docs/framework/lit/guides/reactive-controllers-vs-hooks.md
|
|
160
|
+
|
|
161
|
+
See also: `lifecycle/ssr-hydration-and-streaming` for server cache handoff.
|