@raf-query/react 0.3.0 → 0.4.0

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/CHANGELOG.md CHANGED
@@ -1,14 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0](https://github.com/KATT/raf-query/compare/0.3.1...0.4.0) (2026-10-13)
4
+
5
+ ### ⚠ BREAKING CHANGES
6
+
7
+ * **core:** fetch waits for stale data, as TanStack's fetchQuery does ([#63](https://github.com/KATT/raf-query/issues/63))
8
+
9
+ ### Bug Fixes
10
+
11
+ * **core:** fetch waits for stale data, as TanStack's fetchQuery does ([#63](https://github.com/KATT/raf-query/issues/63)) ([c9785d5](https://github.com/KATT/raf-query/commit/c9785d5e99c0578c1748de3330823cc0e65f2378))
12
+
13
+ ## [0.3.1](https://github.com/KATT/raf-query/compare/0.3.0...0.3.1) (2026-10-13)
14
+
15
+ - teach avoiding dependent queries; frame prefetch vs ensure as loading UX ([#62](https://github.com/KATT/raf-query/issues/62)) ([ec62760](https://github.com/KATT/raf-query/commit/ec627609cbbee9fa4ff0cf568f69964341e27daa))
16
+
3
17
  ## [0.3.0](https://github.com/KATT/raf-query/compare/0.2.8...0.3.0) (2026-10-13)
4
18
 
5
19
  ### ⚠ BREAKING CHANGES
6
20
 
7
- * RSC apps hydrate only through HydrationBoundary; QueryProvider moves to /rsc ([#61](https://github.com/KATT/raf-query/issues/61))
21
+ - RSC apps hydrate only through HydrationBoundary; QueryProvider moves to /rsc ([#61](https://github.com/KATT/raf-query/issues/61))
8
22
 
9
23
  ### Features
10
24
 
11
- * RSC apps hydrate only through HydrationBoundary; QueryProvider moves to /rsc ([#61](https://github.com/KATT/raf-query/issues/61)) ([772de79](https://github.com/KATT/raf-query/commit/772de796adfdf267a9184fd75624ff30ae53f4fc)), references [#59](https://github.com/KATT/raf-query/issues/59) [#59](https://github.com/KATT/raf-query/issues/59) [#59](https://github.com/KATT/raf-query/issues/59) [#59](https://github.com/KATT/raf-query/issues/59)
25
+ - RSC apps hydrate only through HydrationBoundary; QueryProvider moves to /rsc ([#61](https://github.com/KATT/raf-query/issues/61)) ([772de79](https://github.com/KATT/raf-query/commit/772de796adfdf267a9184fd75624ff30ae53f4fc)), references [#59](https://github.com/KATT/raf-query/issues/59) [#59](https://github.com/KATT/raf-query/issues/59) [#59](https://github.com/KATT/raf-query/issues/59) [#59](https://github.com/KATT/raf-query/issues/59)
12
26
 
13
27
  ## [0.2.8](https://github.com/KATT/raf-query/compare/0.2.7...0.2.8) (2026-10-13)
14
28
 
package/README.md CHANGED
@@ -120,11 +120,21 @@ declare module "@raf-query/react" {
120
120
 
121
121
  All three verbs live on the query client and accept a factory plus its input.
122
122
 
123
- | Call | Returns | Blocks? | Use for |
124
- | --------------------------------- | ----------------------------- | ------- | ---------------------------------------------------------------------- |
125
- | `qc.prefetch(factory, input)` | `QueryHandle` | No | Data the route can render without; it streams in |
126
- | `await qc.ensure(factory, input)` | `Promise<EnsuredQueryHandle>` | Yes | Data the route must not render without; `useQuery` returns it directly |
127
- | `await qc.fetch(factory, input)` | `Promise<Data>` | Yes | Rendering in a server component, or building another input |
123
+ | Call | Returns | Use for |
124
+ | --------------------------------- | ----------------------------- | ---------------------------------------------------------------------- |
125
+ | `qc.prefetch(factory, input)` | `QueryHandle` | Data the route can render without; it streams in |
126
+ | `await qc.ensure(factory, input)` | `Promise<EnsuredQueryHandle>` | Data the route must not render without; `useQuery` returns it directly |
127
+ | `await qc.fetch(factory, input)` | `Promise<Data>` | Rendering data in a server component |
128
+
129
+ They differ in what they wait for:
130
+
131
+ | Verb | Cache miss | Stale data | Fresh data | TanStack Query |
132
+ | ---------- | --------------------- | --------------------------------------------- | ---------------- | ---------------------------------------------- |
133
+ | `prefetch` | Starts the request | Refetches in the background | Reused | `prefetchQuery`, not awaited |
134
+ | `ensure` | Waits for the request | Resolves at once, refetches in the background | Resolves at once | `ensureQueryData({ revalidateIfStale: true })` |
135
+ | `fetch` | Waits for the request | Waits for the refetch | Resolves at once | `fetchQuery` |
136
+
137
+ So `prefetch` never holds anything up, `ensure` holds up only the first visit (returning to a page shows the cached data at once, then fresh data), and `fetch` always returns fresh data, at the cost of a round trip whenever the cache is stale. On the server, data counts as stale only once `invalidate` has marked it (see below).
128
138
 
129
139
  ```tsx
130
140
  // TanStack Start
@@ -139,6 +149,13 @@ export const Route = createFileRoute("/posts/$slug")({
139
149
  });
140
150
  ```
141
151
 
152
+ `prefetch` and `ensure` start the request at the same moment, so neither is faster. They differ in what the user sees while it's in flight:
153
+
154
+ - **`prefetch`: granular skeletons.** The navigation commits right away. Each part of the page that reads a query still loading suspends to its nearest Suspense boundary and fills in when its data streams in.
155
+ - **`ensure`: a blocked navigation.** The previous page stays on screen until the data is in. In TanStack Router, a route with a `pendingComponent` shows it once `pendingMs` (1 second by default) has passed, which makes a full-page skeleton.
156
+
157
+ Choose per query. `ensure` what the page can't lay out without, where a skeleton would only flash or shift (the post itself), and `prefetch` the rest (comments, sidebars, counts). Start every query before awaiting any of them, as the `prefetch` before the `await` above does, so a blocking query never delays the others.
158
+
142
159
  Await independent blocking queries together with `allKeyed`, which ships with raf-query. It's a ponyfill of the TC39 `Promise.allKeyed` proposal: a record of promises in, a promise of the same record out, rejecting on the first failure.
143
160
 
144
161
  ```ts
@@ -150,17 +167,78 @@ return await allKeyed({
150
167
  });
151
168
  ```
152
169
 
153
- Dependent inputs use `fetch`:
170
+ ### Avoid dependent queries
171
+
172
+ A query whose input comes from another query's result can't start until that one has finished. That's a waterfall: one extra round trip per level, on every navigation. Fix it where the data is shaped, so every query a route needs starts from what the URL already holds:
173
+
174
+ - **Put the input in the route.** If the page is about an organization, its id belongs in the path (`/orgs/$orgId`), not in a lookup on the current user.
175
+ - **Have the backend resolve it.** The server already knows the session and the relations, so an endpoint can return the child with its parent, or be keyed by what the client has (`GET /me/org` instead of `GET /me` then `GET /orgs/:id`).
176
+
177
+ <details>
178
+ <summary>Example: the current user's organization</summary>
179
+
180
+ A settings page needs the user and their organization, and the organization's id is a field of the user:
181
+
182
+ ```ts
183
+ export const Route = createFileRoute("/settings")({
184
+ loader: async (args) => {
185
+ const qc = args.context.queryClient;
186
+ const user = await qc.fetch(currentUserQuery);
187
+ return {
188
+ user: qc.prefetch(currentUserQuery),
189
+ org: qc.prefetch(orgQuery, { orgId: user.orgId }),
190
+ };
191
+ },
192
+ });
193
+ ```
194
+
195
+ With a 150 ms round trip, the organization can't arrive before 300 ms, and the navigation is blocked for the first 150 even though nothing on the page needed the user that early. Each query keyed by the organization (members, billing) would add another 150.
196
+
197
+ Fix it in the backend: the server can find the session's organization itself, so give it an endpoint and a query with no input.
198
+
199
+ ```ts
200
+ export function currentOrgQuery() {
201
+ return queryOptions({
202
+ queryKey: ["org", "current"],
203
+ queryFn: () => getCurrentOrg(),
204
+ });
205
+ }
206
+
207
+ export const Route = createFileRoute("/settings")({
208
+ loader: (args) => {
209
+ const qc = args.context.queryClient;
210
+ return {
211
+ user: qc.prefetch(currentUserQuery),
212
+ org: qc.prefetch(currentOrgQuery),
213
+ };
214
+ },
215
+ });
216
+ ```
217
+
218
+ Or fix it in the routes: links into the page come from places that already know the organization (the org switcher, a list of orgs), so they can put its id in the URL.
154
219
 
155
220
  ```ts
156
- const user = await qc.fetch(currentUserQuery);
157
- return { org: qc.prefetch(orgQuery, { orgId: user.orgId }) };
221
+ export const Route = createFileRoute("/orgs/$orgId/settings")({
222
+ loader: (args) => {
223
+ const qc = args.context.queryClient;
224
+ return {
225
+ user: qc.prefetch(currentUserQuery),
226
+ org: qc.prefetch(orgQuery, { orgId: args.params.orgId }),
227
+ };
228
+ },
229
+ });
158
230
  ```
159
231
 
232
+ Either way, both requests start together, and the page is ready after one round trip instead of two. Nothing waits, so each query can be a `prefetch` or an `ensure` purely for the loading experience you want.
233
+
234
+ </details>
235
+
236
+ When the data really can't be reshaped (a third-party API you don't control), the loader can `await qc.fetch(...)` the parent and build the input from its data, or a component can start the child with [`useWaterfall`](#render-time-inputs-usewaterfall). Both pay the extra round trip; the loader blocks the navigation on it, the component lets the rest of the page render first.
237
+
160
238
  Behavior:
161
239
 
162
- - Every verb dedupes against the cache. A fresh entry is reused; a stale entry is refetched in the background.
163
- - `ensure` and `fetch` wait only on a cache miss. With stale cached data they resolve immediately, so the navigation isn't held up, and revalidate in the background: the page renders the cached data and re-renders when the refetch lands.
240
+ - Every verb dedupes against the cache: a request already in flight for the entry is joined, not repeated.
241
+ - A stale `ensure` doesn't hold up the navigation: the page renders the cached data and re-renders when the background refetch lands. A stale `fetch` in a loader would, so loaders use `ensure`.
164
242
  - Loaders run on every navigation (TanStack Router reruns them when its own `staleTime` has passed, `0` by default), so returning to a page shows what's cached at once and then fresh data. With `defaultPreload: "intent"`, also set `defaultPreloadStaleTime: 0`: otherwise the router reuses a hover's preload for 30 seconds instead of running the loader on click, and the query cache never hears about the navigation.
165
243
  - On the server, cached data is always reused, whatever `staleTime` says. A server client renders once, so a background refetch could only repeat a request whose result is never shown. Entries marked by `invalidate` still refetch.
166
244
  - A failed `prefetch` never throws out of the loader. The error surfaces where the data is read, and is reported to the client's `onError`.
@@ -241,7 +319,7 @@ function PostPage() {
241
319
 
242
320
  When the cache updates — refetch, invalidation, `setData` — the hook returns a new, pre-resolved promise and the component re-renders without suspending. New data is structurally shared with the old: objects and arrays that are deeply equal keep their identity, and a refetch that returns equal data keeps the same promise, so nothing re-renders. Controls are tracked: reading `controls.isFetching` subscribes the component to `isFetching`, and a change to a control it never read doesn't re-render it. When the **first** load fails, the promise rejects and `use()` throws to the nearest error boundary. Resetting that boundary refetches the query.
243
321
 
244
- `useQuery` never refetches on mount: whatever started the query decides. Stale data refetches in the background when a loader or server component starts the query again (`prefetch`, `ensure`, `fetch`, or hydrating the server's copy), when `useWaterfall` mounts, when invalidated, on window focus or reconnect, on `refetch()`, or on `refetchInterval`.
322
+ `useQuery` never refetches on mount: whatever started the query decides. Stale data refetches when a loader or server component starts the query again (`prefetch`, `ensure`, `fetch`, or hydrating the server's copy), when `useWaterfall` mounts, when invalidated, on window focus or reconnect, on `refetch()`, or on `refetchInterval`.
245
323
 
246
324
  For an app-wide indicator, `useIsFetching()` returns how many queries have a request in flight, background refetches included, and re-renders when that changes. Pass a filter to count only some: `useIsFetching({ queryKey: ["comments"] })`. It's `0` on the server and during hydration, so the indicator never appears in server HTML. Outside React, `qc.isFetching(filter?)` returns the same number.
247
325
 
package/dist/client.d.mts CHANGED
@@ -41,7 +41,11 @@ export declare class QueryClient<TServices = never> {
41
41
  * directly.
42
42
  */
43
43
  ensure<TFactory extends QueryFactory<TServices>>(factory: TFactory, ...args: QueryCache.LoadArgs<TFactory>): Promise<EnsuredQueryHandle<TFactory>>;
44
- /** Like `ensure`, but resolves to the data. */
44
+ /**
45
+ * Start a query and resolve to its data. Waits on a cache miss and on
46
+ * stale data (on the server, only invalidated data), so the data is fresh.
47
+ * Rejects if that request fails.
48
+ */
45
49
  fetch<TFactory extends QueryFactory<TServices>>(factory: TFactory, ...args: QueryCache.LoadArgs<TFactory>): Promise<DataOf<TFactory>>;
46
50
  /**
47
51
  * Mark queries stale and refetch the ones being read. Resolves once those
package/dist/client.mjs CHANGED
@@ -69,7 +69,11 @@ var QueryClient = class {
69
69
  ensure(factory, ...args) {
70
70
  return runLoad(provided(this.effect.ensure(factory, ...args)));
71
71
  }
72
- /** Like `ensure`, but resolves to the data. */
72
+ /**
73
+ * Start a query and resolve to its data. Waits on a cache miss and on
74
+ * stale data (on the server, only invalidated data), so the data is fresh.
75
+ * Rejects if that request fails.
76
+ */
73
77
  fetch(factory, ...args) {
74
78
  return runLoad(provided(this.effect.fetch(factory, ...args)));
75
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raf-query/react",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "React bindings for raf-query, a render-as-you-fetch query library",
5
5
  "keywords": [
6
6
  "query",
@@ -47,7 +47,7 @@
47
47
  }
48
48
  },
49
49
  "dependencies": {
50
- "@raf-query/core": "0.3.0"
50
+ "@raf-query/core": "0.4.0"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@tanstack/react-router": "^1.170.0",