@raf-query/react 0.3.1 → 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,8 +1,18 @@
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
+
3
13
  ## [0.3.1](https://github.com/KATT/raf-query/compare/0.3.0...0.3.1) (2026-10-13)
4
14
 
5
- * 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))
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))
6
16
 
7
17
  ## [0.3.0](https://github.com/KATT/raf-query/compare/0.2.8...0.3.0) (2026-10-13)
8
18
 
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 data in a server component |
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
@@ -227,8 +237,8 @@ When the data really can't be reshaped (a third-party API you don't control), th
227
237
 
228
238
  Behavior:
229
239
 
230
- - Every verb dedupes against the cache. A fresh entry is reused; a stale entry is refetched in the background.
231
- - `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`.
232
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.
233
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.
234
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`.
@@ -309,7 +319,7 @@ function PostPage() {
309
319
 
310
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.
311
321
 
312
- `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`.
313
323
 
314
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.
315
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.1",
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.1"
50
+ "@raf-query/core": "0.4.0"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@tanstack/react-router": "^1.170.0",