@tanstack/solid-query 6.0.0-rc.3 → 6.0.0-rc.5
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/build/_tsup-dts-rollup.d.cts +5 -6
- package/build/_tsup-dts-rollup.d.ts +5 -6
- package/build/dev.cjs +47 -34
- package/build/dev.js +49 -36
- package/build/index.cjs +47 -34
- package/build/index.js +49 -36
- package/package.json +10 -7
- package/src/QueryClientProvider.tsx +51 -31
- package/src/cacheAggregate.ts +2 -3
- package/src/useBaseQuery.ts +84 -40
|
@@ -321,12 +321,11 @@ export { FetchStatus }
|
|
|
321
321
|
*
|
|
322
322
|
* A mutation can additionally declare its invalidation SCOPE with
|
|
323
323
|
* `X-Revalidate` keys (the `revalidate` option of Solid's `reload`/
|
|
324
|
-
* `redirect` response helpers). The payload covers the slice of
|
|
325
|
-
* the server could recompute; whatever a declared key matches
|
|
326
|
-
* swept client-side (see the consumer below).
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* present, rather than skipping the source.
|
|
324
|
+
* `redirect`/`respond` response helpers). The payload covers the slice of
|
|
325
|
+
* that scope the server could recompute; whatever a declared key matches
|
|
326
|
+
* beyond it is swept client-side (see the consumer below). The transport
|
|
327
|
+
* delivers a response carrying keys to the consumer whether or not the
|
|
328
|
+
* collector contributed a slice — with no slice, the whole scope is swept.
|
|
330
329
|
*/
|
|
331
330
|
declare const FLIGHT_DATA_SOURCE = "sq";
|
|
332
331
|
export { FLIGHT_DATA_SOURCE }
|
|
@@ -321,12 +321,11 @@ export { FetchStatus }
|
|
|
321
321
|
*
|
|
322
322
|
* A mutation can additionally declare its invalidation SCOPE with
|
|
323
323
|
* `X-Revalidate` keys (the `revalidate` option of Solid's `reload`/
|
|
324
|
-
* `redirect` response helpers). The payload covers the slice of
|
|
325
|
-
* the server could recompute; whatever a declared key matches
|
|
326
|
-
* swept client-side (see the consumer below).
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* present, rather than skipping the source.
|
|
324
|
+
* `redirect`/`respond` response helpers). The payload covers the slice of
|
|
325
|
+
* that scope the server could recompute; whatever a declared key matches
|
|
326
|
+
* beyond it is swept client-side (see the consumer below). The transport
|
|
327
|
+
* delivers a response carrying keys to the consumer whether or not the
|
|
328
|
+
* collector contributed a slice — with no slice, the whole scope is swept.
|
|
330
329
|
*/
|
|
331
330
|
declare const FLIGHT_DATA_SOURCE = "sq";
|
|
332
331
|
export { FLIGHT_DATA_SOURCE }
|
package/build/dev.cjs
CHANGED
|
@@ -14,14 +14,24 @@ exports.QueryClient = class QueryClient extends queryCore.QueryClient {
|
|
|
14
14
|
var isServer = typeof window === "undefined";
|
|
15
15
|
var HYDRATION_KEY_PREFIX = "sq:";
|
|
16
16
|
exports.FLIGHT_DATA_SOURCE = "sq";
|
|
17
|
+
var REVALIDATE_ALL = "*";
|
|
17
18
|
function sweepRevalidatedQueries(client, payload, response) {
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
const covered = new Set(payload
|
|
19
|
+
const declared = response.headers.get(web.REVALIDATE_HEADER);
|
|
20
|
+
if (declared === null) return;
|
|
21
|
+
const covered = new Set(payload?.queries.map((query) => query.queryHash) ?? []);
|
|
22
|
+
const uncovered = (query) => !covered.has(query.queryHash);
|
|
23
|
+
const keys = declared.split(",");
|
|
24
|
+
if (keys.includes(REVALIDATE_ALL)) {
|
|
25
|
+
client.invalidateQueries({
|
|
26
|
+
predicate: uncovered
|
|
27
|
+
}).catch(() => void 0);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
21
30
|
for (const key of keys) {
|
|
31
|
+
if (!key) continue;
|
|
22
32
|
client.invalidateQueries({
|
|
23
33
|
queryKey: [key],
|
|
24
|
-
predicate:
|
|
34
|
+
predicate: uncovered
|
|
25
35
|
}).catch(() => void 0);
|
|
26
36
|
}
|
|
27
37
|
}
|
|
@@ -37,8 +47,8 @@ exports.useQueryClient = (queryClient) => {
|
|
|
37
47
|
return client();
|
|
38
48
|
};
|
|
39
49
|
function serializeCacheOnServer(client) {
|
|
40
|
-
const
|
|
41
|
-
if (!
|
|
50
|
+
const writer = web.getHydrationWriter();
|
|
51
|
+
if (!writer || !writer.async || !solidJs.isHydratable()) return;
|
|
42
52
|
const cache = client.getQueryCache();
|
|
43
53
|
const shouldDehydrateQuery = client.getDefaultOptions().dehydrate?.shouldDehydrateQuery;
|
|
44
54
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -48,7 +58,7 @@ function serializeCacheOnServer(client) {
|
|
|
48
58
|
const state = query.state;
|
|
49
59
|
if (state.status === "success") {
|
|
50
60
|
seen.add(query.queryHash);
|
|
51
|
-
|
|
61
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, {
|
|
52
62
|
data: state.data,
|
|
53
63
|
t: state.dataUpdatedAt
|
|
54
64
|
});
|
|
@@ -56,7 +66,7 @@ function serializeCacheOnServer(client) {
|
|
|
56
66
|
const promise = query.promise;
|
|
57
67
|
if (!promise) return;
|
|
58
68
|
seen.add(query.queryHash);
|
|
59
|
-
|
|
69
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, promise.then(() => ({
|
|
60
70
|
data: query.state.data,
|
|
61
71
|
t: query.state.dataUpdatedAt
|
|
62
72
|
})));
|
|
@@ -131,7 +141,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
131
141
|
() => defaultedOptions(),
|
|
132
142
|
(opts) => observer.setOptions(opts)
|
|
133
143
|
);
|
|
134
|
-
const hydratedMount =
|
|
144
|
+
const hydratedMount = solidJs.isHydrating();
|
|
135
145
|
const [version, setVersion] = solidJs.createSignal(0, { ownedWrite: true });
|
|
136
146
|
const [primed, setPrimed] = solidJs.createSignal(!hydratedMount, {
|
|
137
147
|
ownedWrite: true
|
|
@@ -146,6 +156,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
146
156
|
let disposed = false;
|
|
147
157
|
let shouldAttach = false;
|
|
148
158
|
let activeClient = solidJs.untrack(client);
|
|
159
|
+
let lastQuery;
|
|
149
160
|
const attach = () => {
|
|
150
161
|
if (!disposed && !observerSub && !solidJs.untrack(isRestoring)) {
|
|
151
162
|
shouldAttach = true;
|
|
@@ -155,6 +166,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
155
166
|
const syncClient = (c) => {
|
|
156
167
|
if (isServer2 || c === activeClient) return;
|
|
157
168
|
activeClient = c;
|
|
169
|
+
lastQuery = void 0;
|
|
158
170
|
cacheSub?.();
|
|
159
171
|
cacheSub = c.getQueryCache().subscribe(onCacheEvent);
|
|
160
172
|
observerSub?.();
|
|
@@ -203,29 +215,19 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
203
215
|
};
|
|
204
216
|
if (!isServer2) {
|
|
205
217
|
cacheSub = activeClient.getQueryCache().subscribe(onCacheEvent);
|
|
206
|
-
const sc = solidJs.sharedConfig;
|
|
207
218
|
const registryKey = HYDRATION_KEY_PREFIX + solidJs.untrack(defaultedOptions).queryHash;
|
|
208
|
-
const primeAndAttach = (
|
|
209
|
-
prime(
|
|
219
|
+
const primeAndAttach = (entry2) => {
|
|
220
|
+
prime(entry2);
|
|
210
221
|
setPrimed(true);
|
|
211
222
|
attach();
|
|
212
223
|
};
|
|
213
224
|
const settle = hydratedMount ? primeAndAttach : prime;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
else if (typeof entry.then === "function")
|
|
221
|
-
entry.then(settle, () => settle());
|
|
222
|
-
else settle(entry);
|
|
223
|
-
} else {
|
|
224
|
-
settle(entry);
|
|
225
|
-
}
|
|
226
|
-
} else if (hydratedMount) {
|
|
227
|
-
primeAndAttach();
|
|
228
|
-
}
|
|
225
|
+
const entry = web.takeHydrationValue(registryKey);
|
|
226
|
+
if (entry?.status === "resolved") settle(entry.value);
|
|
227
|
+
else if (entry?.status === "rejected") settle();
|
|
228
|
+
else if (entry?.status === "pending")
|
|
229
|
+
entry.promise.then(settle, () => settle());
|
|
230
|
+
else if (hydratedMount) primeAndAttach();
|
|
229
231
|
if (!hydratedMount) {
|
|
230
232
|
solidJs.createRenderEffect(
|
|
231
233
|
() => isRestoring(),
|
|
@@ -242,11 +244,21 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
242
244
|
cacheSub = null;
|
|
243
245
|
});
|
|
244
246
|
}
|
|
245
|
-
const
|
|
246
|
-
version();
|
|
247
|
+
const lookupQuery = () => {
|
|
247
248
|
const c = client();
|
|
248
249
|
syncClient(c);
|
|
249
|
-
|
|
250
|
+
const cache = c.getQueryCache();
|
|
251
|
+
const opts = defaultedOptions();
|
|
252
|
+
const existing = cache.get(
|
|
253
|
+
opts.queryHash
|
|
254
|
+
);
|
|
255
|
+
if (existing) return lastQuery = existing;
|
|
256
|
+
if (lastQuery?.queryHash === opts.queryHash) return lastQuery;
|
|
257
|
+
return lastQuery = cache.build(c, opts);
|
|
258
|
+
};
|
|
259
|
+
const query = () => {
|
|
260
|
+
version();
|
|
261
|
+
return lookupQuery();
|
|
250
262
|
};
|
|
251
263
|
const isEnabled = () => {
|
|
252
264
|
const opts = defaultedOptions();
|
|
@@ -293,7 +305,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
293
305
|
}
|
|
294
306
|
if (state.data !== void 0) return wrap(state.data);
|
|
295
307
|
if (isEnabled()) {
|
|
296
|
-
if (
|
|
308
|
+
if (solidJs.isHydrating()) {
|
|
297
309
|
return NEVER;
|
|
298
310
|
}
|
|
299
311
|
if (!primed()) {
|
|
@@ -305,6 +317,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
305
317
|
if (!isServer2) observer.setOptions(opts);
|
|
306
318
|
return chainOnce(q.fetch(opts), select, wrap);
|
|
307
319
|
}
|
|
320
|
+
if (isServer2) return { value: void 0 };
|
|
308
321
|
return NEVER;
|
|
309
322
|
};
|
|
310
323
|
const dataStore = solidJs.createProjection(
|
|
@@ -324,7 +337,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
324
337
|
(draft) => {
|
|
325
338
|
Object.assign(draft, metaFrom(query().state));
|
|
326
339
|
},
|
|
327
|
-
solidJs.untrack(() => metaFrom(
|
|
340
|
+
solidJs.untrack(() => metaFrom(lookupQuery().state))
|
|
328
341
|
);
|
|
329
342
|
const meta = isServer2 ? new Proxy({}, {
|
|
330
343
|
get: (_, key) => serverMeta()[key]
|
|
@@ -337,7 +350,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
337
350
|
return solidJs.untrack(query).state.errorUpdateCount;
|
|
338
351
|
}
|
|
339
352
|
} : solidJs.untrack(() => {
|
|
340
|
-
const state =
|
|
353
|
+
const state = lookupQuery().state;
|
|
341
354
|
return {
|
|
342
355
|
dataUpdateCount: state.dataUpdateCount,
|
|
343
356
|
errorUpdateCount: state.errorUpdateCount
|
|
@@ -469,7 +482,7 @@ async function dehydrateSettled(client, options) {
|
|
|
469
482
|
}
|
|
470
483
|
var isServer3 = typeof window === "undefined";
|
|
471
484
|
function createCacheAggregate(subscribe, read, empty) {
|
|
472
|
-
const hydratedMount =
|
|
485
|
+
const hydratedMount = solidJs.isHydrating();
|
|
473
486
|
const [durable, setDurable] = solidJs.createSignal(
|
|
474
487
|
hydratedMount || isServer3 ? empty : solidJs.untrack(() => read(empty)),
|
|
475
488
|
{ ownedWrite: true }
|
package/build/dev.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { noop, QueryClient as QueryClient$1, hydrate, dehydrate, shouldThrowError, InfiniteQueryObserver, QueryObserver, replaceEqualDeep } from '@tanstack/query-core';
|
|
2
2
|
export * from '@tanstack/query-core';
|
|
3
|
-
import { createContext, useContext, onCleanup, createMemo, createOptimistic, createSignal, action, createRenderEffect, untrack, repeat, $TRACK,
|
|
4
|
-
import { createComponent, REVALIDATE_HEADER } from '@solidjs/web';
|
|
3
|
+
import { createContext, useContext, onCleanup, createMemo, createOptimistic, createSignal, action, createRenderEffect, untrack, repeat, $TRACK, isHydratable, isHydrating, createEffect, createProjection, runWithOwner, isPending, resolve } from 'solid-js';
|
|
4
|
+
import { createComponent, getHydrationWriter, REVALIDATE_HEADER, takeHydrationValue } from '@solidjs/web';
|
|
5
5
|
import { subscribeFlightData } from '@solidjs/web/server-functions';
|
|
6
6
|
|
|
7
7
|
// src/index.ts
|
|
@@ -13,14 +13,24 @@ var QueryClient = class extends QueryClient$1 {
|
|
|
13
13
|
var isServer = typeof window === "undefined";
|
|
14
14
|
var HYDRATION_KEY_PREFIX = "sq:";
|
|
15
15
|
var FLIGHT_DATA_SOURCE = "sq";
|
|
16
|
+
var REVALIDATE_ALL = "*";
|
|
16
17
|
function sweepRevalidatedQueries(client, payload, response) {
|
|
17
|
-
const
|
|
18
|
-
if (
|
|
19
|
-
const covered = new Set(payload
|
|
18
|
+
const declared = response.headers.get(REVALIDATE_HEADER);
|
|
19
|
+
if (declared === null) return;
|
|
20
|
+
const covered = new Set(payload?.queries.map((query) => query.queryHash) ?? []);
|
|
21
|
+
const uncovered = (query) => !covered.has(query.queryHash);
|
|
22
|
+
const keys = declared.split(",");
|
|
23
|
+
if (keys.includes(REVALIDATE_ALL)) {
|
|
24
|
+
client.invalidateQueries({
|
|
25
|
+
predicate: uncovered
|
|
26
|
+
}).catch(() => void 0);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
20
29
|
for (const key of keys) {
|
|
30
|
+
if (!key) continue;
|
|
21
31
|
client.invalidateQueries({
|
|
22
32
|
queryKey: [key],
|
|
23
|
-
predicate:
|
|
33
|
+
predicate: uncovered
|
|
24
34
|
}).catch(() => void 0);
|
|
25
35
|
}
|
|
26
36
|
}
|
|
@@ -36,8 +46,8 @@ var useQueryClient = (queryClient) => {
|
|
|
36
46
|
return client();
|
|
37
47
|
};
|
|
38
48
|
function serializeCacheOnServer(client) {
|
|
39
|
-
const
|
|
40
|
-
if (!
|
|
49
|
+
const writer = getHydrationWriter();
|
|
50
|
+
if (!writer || !writer.async || !isHydratable()) return;
|
|
41
51
|
const cache = client.getQueryCache();
|
|
42
52
|
const shouldDehydrateQuery = client.getDefaultOptions().dehydrate?.shouldDehydrateQuery;
|
|
43
53
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -47,7 +57,7 @@ function serializeCacheOnServer(client) {
|
|
|
47
57
|
const state = query.state;
|
|
48
58
|
if (state.status === "success") {
|
|
49
59
|
seen.add(query.queryHash);
|
|
50
|
-
|
|
60
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, {
|
|
51
61
|
data: state.data,
|
|
52
62
|
t: state.dataUpdatedAt
|
|
53
63
|
});
|
|
@@ -55,7 +65,7 @@ function serializeCacheOnServer(client) {
|
|
|
55
65
|
const promise = query.promise;
|
|
56
66
|
if (!promise) return;
|
|
57
67
|
seen.add(query.queryHash);
|
|
58
|
-
|
|
68
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, promise.then(() => ({
|
|
59
69
|
data: query.state.data,
|
|
60
70
|
t: query.state.dataUpdatedAt
|
|
61
71
|
})));
|
|
@@ -130,7 +140,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
130
140
|
() => defaultedOptions(),
|
|
131
141
|
(opts) => observer.setOptions(opts)
|
|
132
142
|
);
|
|
133
|
-
const hydratedMount =
|
|
143
|
+
const hydratedMount = isHydrating();
|
|
134
144
|
const [version, setVersion] = createSignal(0, { ownedWrite: true });
|
|
135
145
|
const [primed, setPrimed] = createSignal(!hydratedMount, {
|
|
136
146
|
ownedWrite: true
|
|
@@ -145,6 +155,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
145
155
|
let disposed = false;
|
|
146
156
|
let shouldAttach = false;
|
|
147
157
|
let activeClient = untrack(client);
|
|
158
|
+
let lastQuery;
|
|
148
159
|
const attach = () => {
|
|
149
160
|
if (!disposed && !observerSub && !untrack(isRestoring)) {
|
|
150
161
|
shouldAttach = true;
|
|
@@ -154,6 +165,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
154
165
|
const syncClient = (c) => {
|
|
155
166
|
if (isServer2 || c === activeClient) return;
|
|
156
167
|
activeClient = c;
|
|
168
|
+
lastQuery = void 0;
|
|
157
169
|
cacheSub?.();
|
|
158
170
|
cacheSub = c.getQueryCache().subscribe(onCacheEvent);
|
|
159
171
|
observerSub?.();
|
|
@@ -202,29 +214,19 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
202
214
|
};
|
|
203
215
|
if (!isServer2) {
|
|
204
216
|
cacheSub = activeClient.getQueryCache().subscribe(onCacheEvent);
|
|
205
|
-
const sc = sharedConfig;
|
|
206
217
|
const registryKey = HYDRATION_KEY_PREFIX + untrack(defaultedOptions).queryHash;
|
|
207
|
-
const primeAndAttach = (
|
|
208
|
-
prime(
|
|
218
|
+
const primeAndAttach = (entry2) => {
|
|
219
|
+
prime(entry2);
|
|
209
220
|
setPrimed(true);
|
|
210
221
|
attach();
|
|
211
222
|
};
|
|
212
223
|
const settle = hydratedMount ? primeAndAttach : prime;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
else if (typeof entry.then === "function")
|
|
220
|
-
entry.then(settle, () => settle());
|
|
221
|
-
else settle(entry);
|
|
222
|
-
} else {
|
|
223
|
-
settle(entry);
|
|
224
|
-
}
|
|
225
|
-
} else if (hydratedMount) {
|
|
226
|
-
primeAndAttach();
|
|
227
|
-
}
|
|
224
|
+
const entry = takeHydrationValue(registryKey);
|
|
225
|
+
if (entry?.status === "resolved") settle(entry.value);
|
|
226
|
+
else if (entry?.status === "rejected") settle();
|
|
227
|
+
else if (entry?.status === "pending")
|
|
228
|
+
entry.promise.then(settle, () => settle());
|
|
229
|
+
else if (hydratedMount) primeAndAttach();
|
|
228
230
|
if (!hydratedMount) {
|
|
229
231
|
createRenderEffect(
|
|
230
232
|
() => isRestoring(),
|
|
@@ -241,11 +243,21 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
241
243
|
cacheSub = null;
|
|
242
244
|
});
|
|
243
245
|
}
|
|
244
|
-
const
|
|
245
|
-
version();
|
|
246
|
+
const lookupQuery = () => {
|
|
246
247
|
const c = client();
|
|
247
248
|
syncClient(c);
|
|
248
|
-
|
|
249
|
+
const cache = c.getQueryCache();
|
|
250
|
+
const opts = defaultedOptions();
|
|
251
|
+
const existing = cache.get(
|
|
252
|
+
opts.queryHash
|
|
253
|
+
);
|
|
254
|
+
if (existing) return lastQuery = existing;
|
|
255
|
+
if (lastQuery?.queryHash === opts.queryHash) return lastQuery;
|
|
256
|
+
return lastQuery = cache.build(c, opts);
|
|
257
|
+
};
|
|
258
|
+
const query = () => {
|
|
259
|
+
version();
|
|
260
|
+
return lookupQuery();
|
|
249
261
|
};
|
|
250
262
|
const isEnabled = () => {
|
|
251
263
|
const opts = defaultedOptions();
|
|
@@ -292,7 +304,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
292
304
|
}
|
|
293
305
|
if (state.data !== void 0) return wrap(state.data);
|
|
294
306
|
if (isEnabled()) {
|
|
295
|
-
if (
|
|
307
|
+
if (isHydrating()) {
|
|
296
308
|
return NEVER;
|
|
297
309
|
}
|
|
298
310
|
if (!primed()) {
|
|
@@ -304,6 +316,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
304
316
|
if (!isServer2) observer.setOptions(opts);
|
|
305
317
|
return chainOnce(q.fetch(opts), select, wrap);
|
|
306
318
|
}
|
|
319
|
+
if (isServer2) return { value: void 0 };
|
|
307
320
|
return NEVER;
|
|
308
321
|
};
|
|
309
322
|
const dataStore = createProjection(
|
|
@@ -323,7 +336,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
323
336
|
(draft) => {
|
|
324
337
|
Object.assign(draft, metaFrom(query().state));
|
|
325
338
|
},
|
|
326
|
-
untrack(() => metaFrom(
|
|
339
|
+
untrack(() => metaFrom(lookupQuery().state))
|
|
327
340
|
);
|
|
328
341
|
const meta = isServer2 ? new Proxy({}, {
|
|
329
342
|
get: (_, key) => serverMeta()[key]
|
|
@@ -336,7 +349,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
336
349
|
return untrack(query).state.errorUpdateCount;
|
|
337
350
|
}
|
|
338
351
|
} : untrack(() => {
|
|
339
|
-
const state =
|
|
352
|
+
const state = lookupQuery().state;
|
|
340
353
|
return {
|
|
341
354
|
dataUpdateCount: state.dataUpdateCount,
|
|
342
355
|
errorUpdateCount: state.errorUpdateCount
|
|
@@ -468,7 +481,7 @@ async function dehydrateSettled(client, options) {
|
|
|
468
481
|
}
|
|
469
482
|
var isServer3 = typeof window === "undefined";
|
|
470
483
|
function createCacheAggregate(subscribe, read, empty) {
|
|
471
|
-
const hydratedMount =
|
|
484
|
+
const hydratedMount = isHydrating();
|
|
472
485
|
const [durable, setDurable] = createSignal(
|
|
473
486
|
hydratedMount || isServer3 ? empty : untrack(() => read(empty)),
|
|
474
487
|
{ ownedWrite: true }
|
package/build/index.cjs
CHANGED
|
@@ -14,14 +14,24 @@ exports.QueryClient = class QueryClient extends queryCore.QueryClient {
|
|
|
14
14
|
var isServer = typeof window === "undefined";
|
|
15
15
|
var HYDRATION_KEY_PREFIX = "sq:";
|
|
16
16
|
exports.FLIGHT_DATA_SOURCE = "sq";
|
|
17
|
+
var REVALIDATE_ALL = "*";
|
|
17
18
|
function sweepRevalidatedQueries(client, payload, response) {
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
const covered = new Set(payload
|
|
19
|
+
const declared = response.headers.get(web.REVALIDATE_HEADER);
|
|
20
|
+
if (declared === null) return;
|
|
21
|
+
const covered = new Set(payload?.queries.map((query) => query.queryHash) ?? []);
|
|
22
|
+
const uncovered = (query) => !covered.has(query.queryHash);
|
|
23
|
+
const keys = declared.split(",");
|
|
24
|
+
if (keys.includes(REVALIDATE_ALL)) {
|
|
25
|
+
client.invalidateQueries({
|
|
26
|
+
predicate: uncovered
|
|
27
|
+
}).catch(() => void 0);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
21
30
|
for (const key of keys) {
|
|
31
|
+
if (!key) continue;
|
|
22
32
|
client.invalidateQueries({
|
|
23
33
|
queryKey: [key],
|
|
24
|
-
predicate:
|
|
34
|
+
predicate: uncovered
|
|
25
35
|
}).catch(() => void 0);
|
|
26
36
|
}
|
|
27
37
|
}
|
|
@@ -37,8 +47,8 @@ exports.useQueryClient = (queryClient) => {
|
|
|
37
47
|
return client();
|
|
38
48
|
};
|
|
39
49
|
function serializeCacheOnServer(client) {
|
|
40
|
-
const
|
|
41
|
-
if (!
|
|
50
|
+
const writer = web.getHydrationWriter();
|
|
51
|
+
if (!writer || !writer.async || !solidJs.isHydratable()) return;
|
|
42
52
|
const cache = client.getQueryCache();
|
|
43
53
|
const shouldDehydrateQuery = client.getDefaultOptions().dehydrate?.shouldDehydrateQuery;
|
|
44
54
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -48,7 +58,7 @@ function serializeCacheOnServer(client) {
|
|
|
48
58
|
const state = query.state;
|
|
49
59
|
if (state.status === "success") {
|
|
50
60
|
seen.add(query.queryHash);
|
|
51
|
-
|
|
61
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, {
|
|
52
62
|
data: state.data,
|
|
53
63
|
t: state.dataUpdatedAt
|
|
54
64
|
});
|
|
@@ -56,7 +66,7 @@ function serializeCacheOnServer(client) {
|
|
|
56
66
|
const promise = query.promise;
|
|
57
67
|
if (!promise) return;
|
|
58
68
|
seen.add(query.queryHash);
|
|
59
|
-
|
|
69
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, promise.then(() => ({
|
|
60
70
|
data: query.state.data,
|
|
61
71
|
t: query.state.dataUpdatedAt
|
|
62
72
|
})));
|
|
@@ -131,7 +141,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
131
141
|
() => defaultedOptions(),
|
|
132
142
|
(opts) => observer.setOptions(opts)
|
|
133
143
|
);
|
|
134
|
-
const hydratedMount =
|
|
144
|
+
const hydratedMount = solidJs.isHydrating();
|
|
135
145
|
const [version, setVersion] = solidJs.createSignal(0, { ownedWrite: true });
|
|
136
146
|
const [primed, setPrimed] = solidJs.createSignal(!hydratedMount, {
|
|
137
147
|
ownedWrite: true
|
|
@@ -146,6 +156,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
146
156
|
let disposed = false;
|
|
147
157
|
let shouldAttach = false;
|
|
148
158
|
let activeClient = solidJs.untrack(client);
|
|
159
|
+
let lastQuery;
|
|
149
160
|
const attach = () => {
|
|
150
161
|
if (!disposed && !observerSub && !solidJs.untrack(isRestoring)) {
|
|
151
162
|
shouldAttach = true;
|
|
@@ -155,6 +166,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
155
166
|
const syncClient = (c) => {
|
|
156
167
|
if (isServer2 || c === activeClient) return;
|
|
157
168
|
activeClient = c;
|
|
169
|
+
lastQuery = void 0;
|
|
158
170
|
cacheSub?.();
|
|
159
171
|
cacheSub = c.getQueryCache().subscribe(onCacheEvent);
|
|
160
172
|
observerSub?.();
|
|
@@ -203,29 +215,19 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
203
215
|
};
|
|
204
216
|
if (!isServer2) {
|
|
205
217
|
cacheSub = activeClient.getQueryCache().subscribe(onCacheEvent);
|
|
206
|
-
const sc = solidJs.sharedConfig;
|
|
207
218
|
const registryKey = HYDRATION_KEY_PREFIX + solidJs.untrack(defaultedOptions).queryHash;
|
|
208
|
-
const primeAndAttach = (
|
|
209
|
-
prime(
|
|
219
|
+
const primeAndAttach = (entry2) => {
|
|
220
|
+
prime(entry2);
|
|
210
221
|
setPrimed(true);
|
|
211
222
|
attach();
|
|
212
223
|
};
|
|
213
224
|
const settle = hydratedMount ? primeAndAttach : prime;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
else if (typeof entry.then === "function")
|
|
221
|
-
entry.then(settle, () => settle());
|
|
222
|
-
else settle(entry);
|
|
223
|
-
} else {
|
|
224
|
-
settle(entry);
|
|
225
|
-
}
|
|
226
|
-
} else if (hydratedMount) {
|
|
227
|
-
primeAndAttach();
|
|
228
|
-
}
|
|
225
|
+
const entry = web.takeHydrationValue(registryKey);
|
|
226
|
+
if (entry?.status === "resolved") settle(entry.value);
|
|
227
|
+
else if (entry?.status === "rejected") settle();
|
|
228
|
+
else if (entry?.status === "pending")
|
|
229
|
+
entry.promise.then(settle, () => settle());
|
|
230
|
+
else if (hydratedMount) primeAndAttach();
|
|
229
231
|
if (!hydratedMount) {
|
|
230
232
|
solidJs.createRenderEffect(
|
|
231
233
|
() => isRestoring(),
|
|
@@ -242,11 +244,21 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
242
244
|
cacheSub = null;
|
|
243
245
|
});
|
|
244
246
|
}
|
|
245
|
-
const
|
|
246
|
-
version();
|
|
247
|
+
const lookupQuery = () => {
|
|
247
248
|
const c = client();
|
|
248
249
|
syncClient(c);
|
|
249
|
-
|
|
250
|
+
const cache = c.getQueryCache();
|
|
251
|
+
const opts = defaultedOptions();
|
|
252
|
+
const existing = cache.get(
|
|
253
|
+
opts.queryHash
|
|
254
|
+
);
|
|
255
|
+
if (existing) return lastQuery = existing;
|
|
256
|
+
if (lastQuery?.queryHash === opts.queryHash) return lastQuery;
|
|
257
|
+
return lastQuery = cache.build(c, opts);
|
|
258
|
+
};
|
|
259
|
+
const query = () => {
|
|
260
|
+
version();
|
|
261
|
+
return lookupQuery();
|
|
250
262
|
};
|
|
251
263
|
const isEnabled = () => {
|
|
252
264
|
const opts = defaultedOptions();
|
|
@@ -293,7 +305,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
293
305
|
}
|
|
294
306
|
if (state.data !== void 0) return wrap(state.data);
|
|
295
307
|
if (isEnabled()) {
|
|
296
|
-
if (
|
|
308
|
+
if (solidJs.isHydrating()) {
|
|
297
309
|
return NEVER;
|
|
298
310
|
}
|
|
299
311
|
if (!primed()) {
|
|
@@ -305,6 +317,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
305
317
|
if (!isServer2) observer.setOptions(opts);
|
|
306
318
|
return chainOnce(q.fetch(opts), select, wrap);
|
|
307
319
|
}
|
|
320
|
+
if (isServer2) return { value: void 0 };
|
|
308
321
|
return NEVER;
|
|
309
322
|
};
|
|
310
323
|
const dataStore = solidJs.createProjection(
|
|
@@ -324,7 +337,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
324
337
|
(draft) => {
|
|
325
338
|
Object.assign(draft, metaFrom(query().state));
|
|
326
339
|
},
|
|
327
|
-
solidJs.untrack(() => metaFrom(
|
|
340
|
+
solidJs.untrack(() => metaFrom(lookupQuery().state))
|
|
328
341
|
);
|
|
329
342
|
const meta = isServer2 ? new Proxy({}, {
|
|
330
343
|
get: (_, key) => serverMeta()[key]
|
|
@@ -337,7 +350,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
337
350
|
return solidJs.untrack(query).state.errorUpdateCount;
|
|
338
351
|
}
|
|
339
352
|
} : solidJs.untrack(() => {
|
|
340
|
-
const state =
|
|
353
|
+
const state = lookupQuery().state;
|
|
341
354
|
return {
|
|
342
355
|
dataUpdateCount: state.dataUpdateCount,
|
|
343
356
|
errorUpdateCount: state.errorUpdateCount
|
|
@@ -469,7 +482,7 @@ async function dehydrateSettled(client, options) {
|
|
|
469
482
|
}
|
|
470
483
|
var isServer3 = typeof window === "undefined";
|
|
471
484
|
function createCacheAggregate(subscribe, read, empty) {
|
|
472
|
-
const hydratedMount =
|
|
485
|
+
const hydratedMount = solidJs.isHydrating();
|
|
473
486
|
const [durable, setDurable] = solidJs.createSignal(
|
|
474
487
|
hydratedMount || isServer3 ? empty : solidJs.untrack(() => read(empty)),
|
|
475
488
|
{ ownedWrite: true }
|
package/build/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { noop, QueryClient as QueryClient$1, hydrate, dehydrate, shouldThrowError, InfiniteQueryObserver, QueryObserver, replaceEqualDeep } from '@tanstack/query-core';
|
|
2
2
|
export * from '@tanstack/query-core';
|
|
3
|
-
import { createContext, useContext, onCleanup, createMemo, createOptimistic, createSignal, action, createRenderEffect, untrack, repeat, $TRACK,
|
|
4
|
-
import { createComponent, REVALIDATE_HEADER } from '@solidjs/web';
|
|
3
|
+
import { createContext, useContext, onCleanup, createMemo, createOptimistic, createSignal, action, createRenderEffect, untrack, repeat, $TRACK, isHydratable, isHydrating, createEffect, createProjection, runWithOwner, isPending, resolve } from 'solid-js';
|
|
4
|
+
import { createComponent, getHydrationWriter, REVALIDATE_HEADER, takeHydrationValue } from '@solidjs/web';
|
|
5
5
|
import { subscribeFlightData } from '@solidjs/web/server-functions';
|
|
6
6
|
|
|
7
7
|
// src/index.ts
|
|
@@ -13,14 +13,24 @@ var QueryClient = class extends QueryClient$1 {
|
|
|
13
13
|
var isServer = typeof window === "undefined";
|
|
14
14
|
var HYDRATION_KEY_PREFIX = "sq:";
|
|
15
15
|
var FLIGHT_DATA_SOURCE = "sq";
|
|
16
|
+
var REVALIDATE_ALL = "*";
|
|
16
17
|
function sweepRevalidatedQueries(client, payload, response) {
|
|
17
|
-
const
|
|
18
|
-
if (
|
|
19
|
-
const covered = new Set(payload
|
|
18
|
+
const declared = response.headers.get(REVALIDATE_HEADER);
|
|
19
|
+
if (declared === null) return;
|
|
20
|
+
const covered = new Set(payload?.queries.map((query) => query.queryHash) ?? []);
|
|
21
|
+
const uncovered = (query) => !covered.has(query.queryHash);
|
|
22
|
+
const keys = declared.split(",");
|
|
23
|
+
if (keys.includes(REVALIDATE_ALL)) {
|
|
24
|
+
client.invalidateQueries({
|
|
25
|
+
predicate: uncovered
|
|
26
|
+
}).catch(() => void 0);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
20
29
|
for (const key of keys) {
|
|
30
|
+
if (!key) continue;
|
|
21
31
|
client.invalidateQueries({
|
|
22
32
|
queryKey: [key],
|
|
23
|
-
predicate:
|
|
33
|
+
predicate: uncovered
|
|
24
34
|
}).catch(() => void 0);
|
|
25
35
|
}
|
|
26
36
|
}
|
|
@@ -36,8 +46,8 @@ var useQueryClient = (queryClient) => {
|
|
|
36
46
|
return client();
|
|
37
47
|
};
|
|
38
48
|
function serializeCacheOnServer(client) {
|
|
39
|
-
const
|
|
40
|
-
if (!
|
|
49
|
+
const writer = getHydrationWriter();
|
|
50
|
+
if (!writer || !writer.async || !isHydratable()) return;
|
|
41
51
|
const cache = client.getQueryCache();
|
|
42
52
|
const shouldDehydrateQuery = client.getDefaultOptions().dehydrate?.shouldDehydrateQuery;
|
|
43
53
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -47,7 +57,7 @@ function serializeCacheOnServer(client) {
|
|
|
47
57
|
const state = query.state;
|
|
48
58
|
if (state.status === "success") {
|
|
49
59
|
seen.add(query.queryHash);
|
|
50
|
-
|
|
60
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, {
|
|
51
61
|
data: state.data,
|
|
52
62
|
t: state.dataUpdatedAt
|
|
53
63
|
});
|
|
@@ -55,7 +65,7 @@ function serializeCacheOnServer(client) {
|
|
|
55
65
|
const promise = query.promise;
|
|
56
66
|
if (!promise) return;
|
|
57
67
|
seen.add(query.queryHash);
|
|
58
|
-
|
|
68
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, promise.then(() => ({
|
|
59
69
|
data: query.state.data,
|
|
60
70
|
t: query.state.dataUpdatedAt
|
|
61
71
|
})));
|
|
@@ -130,7 +140,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
130
140
|
() => defaultedOptions(),
|
|
131
141
|
(opts) => observer.setOptions(opts)
|
|
132
142
|
);
|
|
133
|
-
const hydratedMount =
|
|
143
|
+
const hydratedMount = isHydrating();
|
|
134
144
|
const [version, setVersion] = createSignal(0, { ownedWrite: true });
|
|
135
145
|
const [primed, setPrimed] = createSignal(!hydratedMount, {
|
|
136
146
|
ownedWrite: true
|
|
@@ -145,6 +155,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
145
155
|
let disposed = false;
|
|
146
156
|
let shouldAttach = false;
|
|
147
157
|
let activeClient = untrack(client);
|
|
158
|
+
let lastQuery;
|
|
148
159
|
const attach = () => {
|
|
149
160
|
if (!disposed && !observerSub && !untrack(isRestoring)) {
|
|
150
161
|
shouldAttach = true;
|
|
@@ -154,6 +165,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
154
165
|
const syncClient = (c) => {
|
|
155
166
|
if (isServer2 || c === activeClient) return;
|
|
156
167
|
activeClient = c;
|
|
168
|
+
lastQuery = void 0;
|
|
157
169
|
cacheSub?.();
|
|
158
170
|
cacheSub = c.getQueryCache().subscribe(onCacheEvent);
|
|
159
171
|
observerSub?.();
|
|
@@ -202,29 +214,19 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
202
214
|
};
|
|
203
215
|
if (!isServer2) {
|
|
204
216
|
cacheSub = activeClient.getQueryCache().subscribe(onCacheEvent);
|
|
205
|
-
const sc = sharedConfig;
|
|
206
217
|
const registryKey = HYDRATION_KEY_PREFIX + untrack(defaultedOptions).queryHash;
|
|
207
|
-
const primeAndAttach = (
|
|
208
|
-
prime(
|
|
218
|
+
const primeAndAttach = (entry2) => {
|
|
219
|
+
prime(entry2);
|
|
209
220
|
setPrimed(true);
|
|
210
221
|
attach();
|
|
211
222
|
};
|
|
212
223
|
const settle = hydratedMount ? primeAndAttach : prime;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
else if (typeof entry.then === "function")
|
|
220
|
-
entry.then(settle, () => settle());
|
|
221
|
-
else settle(entry);
|
|
222
|
-
} else {
|
|
223
|
-
settle(entry);
|
|
224
|
-
}
|
|
225
|
-
} else if (hydratedMount) {
|
|
226
|
-
primeAndAttach();
|
|
227
|
-
}
|
|
224
|
+
const entry = takeHydrationValue(registryKey);
|
|
225
|
+
if (entry?.status === "resolved") settle(entry.value);
|
|
226
|
+
else if (entry?.status === "rejected") settle();
|
|
227
|
+
else if (entry?.status === "pending")
|
|
228
|
+
entry.promise.then(settle, () => settle());
|
|
229
|
+
else if (hydratedMount) primeAndAttach();
|
|
228
230
|
if (!hydratedMount) {
|
|
229
231
|
createRenderEffect(
|
|
230
232
|
() => isRestoring(),
|
|
@@ -241,11 +243,21 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
241
243
|
cacheSub = null;
|
|
242
244
|
});
|
|
243
245
|
}
|
|
244
|
-
const
|
|
245
|
-
version();
|
|
246
|
+
const lookupQuery = () => {
|
|
246
247
|
const c = client();
|
|
247
248
|
syncClient(c);
|
|
248
|
-
|
|
249
|
+
const cache = c.getQueryCache();
|
|
250
|
+
const opts = defaultedOptions();
|
|
251
|
+
const existing = cache.get(
|
|
252
|
+
opts.queryHash
|
|
253
|
+
);
|
|
254
|
+
if (existing) return lastQuery = existing;
|
|
255
|
+
if (lastQuery?.queryHash === opts.queryHash) return lastQuery;
|
|
256
|
+
return lastQuery = cache.build(c, opts);
|
|
257
|
+
};
|
|
258
|
+
const query = () => {
|
|
259
|
+
version();
|
|
260
|
+
return lookupQuery();
|
|
249
261
|
};
|
|
250
262
|
const isEnabled = () => {
|
|
251
263
|
const opts = defaultedOptions();
|
|
@@ -292,7 +304,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
292
304
|
}
|
|
293
305
|
if (state.data !== void 0) return wrap(state.data);
|
|
294
306
|
if (isEnabled()) {
|
|
295
|
-
if (
|
|
307
|
+
if (isHydrating()) {
|
|
296
308
|
return NEVER;
|
|
297
309
|
}
|
|
298
310
|
if (!primed()) {
|
|
@@ -304,6 +316,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
304
316
|
if (!isServer2) observer.setOptions(opts);
|
|
305
317
|
return chainOnce(q.fetch(opts), select, wrap);
|
|
306
318
|
}
|
|
319
|
+
if (isServer2) return { value: void 0 };
|
|
307
320
|
return NEVER;
|
|
308
321
|
};
|
|
309
322
|
const dataStore = createProjection(
|
|
@@ -323,7 +336,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
323
336
|
(draft) => {
|
|
324
337
|
Object.assign(draft, metaFrom(query().state));
|
|
325
338
|
},
|
|
326
|
-
untrack(() => metaFrom(
|
|
339
|
+
untrack(() => metaFrom(lookupQuery().state))
|
|
327
340
|
);
|
|
328
341
|
const meta = isServer2 ? new Proxy({}, {
|
|
329
342
|
get: (_, key) => serverMeta()[key]
|
|
@@ -336,7 +349,7 @@ function useBaseQueryLayer(options, Observer, queryClient) {
|
|
|
336
349
|
return untrack(query).state.errorUpdateCount;
|
|
337
350
|
}
|
|
338
351
|
} : untrack(() => {
|
|
339
|
-
const state =
|
|
352
|
+
const state = lookupQuery().state;
|
|
340
353
|
return {
|
|
341
354
|
dataUpdateCount: state.dataUpdateCount,
|
|
342
355
|
errorUpdateCount: state.errorUpdateCount
|
|
@@ -468,7 +481,7 @@ async function dehydrateSettled(client, options) {
|
|
|
468
481
|
}
|
|
469
482
|
var isServer3 = typeof window === "undefined";
|
|
470
483
|
function createCacheAggregate(subscribe, read, empty) {
|
|
471
|
-
const hydratedMount =
|
|
484
|
+
const hydratedMount = isHydrating();
|
|
472
485
|
const [durable, setDurable] = createSignal(
|
|
473
486
|
hydratedMount || isServer3 ? empty : untrack(() => read(empty)),
|
|
474
487
|
{ ownedWrite: true }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-query",
|
|
3
|
-
"version": "6.0.0-rc.
|
|
3
|
+
"version": "6.0.0-rc.5",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Solid",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,18 +51,21 @@
|
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@babel/core": "^7.28.0",
|
|
53
53
|
"@babel/preset-typescript": "^7.18.6",
|
|
54
|
+
"@solidjs/babel-plugin": "^2.0.0-rc.13",
|
|
54
55
|
"@solidjs/testing-library": "^0.8.10",
|
|
55
|
-
"@solidjs/vite-plugin": "^3.0.0-next.
|
|
56
|
-
"@solidjs/web": "^2.0.0-rc.
|
|
57
|
-
"
|
|
56
|
+
"@solidjs/vite-plugin": "^3.0.0-next.46",
|
|
57
|
+
"@solidjs/web": "^2.0.0-rc.13",
|
|
58
|
+
"@vitest/coverage-istanbul": "4.0.6",
|
|
58
59
|
"npm-run-all2": "^5.0.0",
|
|
59
|
-
"solid-js": "^2.0.0-rc.
|
|
60
|
+
"solid-js": "^2.0.0-rc.13",
|
|
60
61
|
"tsup-preset-solid": "^2.2.0",
|
|
62
|
+
"vite": "^8.3.0",
|
|
63
|
+
"vitest": "^4.0.18",
|
|
61
64
|
"@tanstack/query-test-utils": "0.0.0"
|
|
62
65
|
},
|
|
63
66
|
"peerDependencies": {
|
|
64
|
-
"@solidjs/web": ">=2.0.0-rc.
|
|
65
|
-
"solid-js": ">=2.0.0-rc.
|
|
67
|
+
"@solidjs/web": ">=2.0.0-rc.13 <3.0.0",
|
|
68
|
+
"solid-js": ">=2.0.0-rc.13 <3.0.0"
|
|
66
69
|
},
|
|
67
70
|
"scripts": {
|
|
68
71
|
"clean": "premove ./build ./coverage ./dist-ts",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createContext,
|
|
2
|
-
import { REVALIDATE_HEADER } from '@solidjs/web'
|
|
1
|
+
import { createContext, isHydratable, onCleanup, useContext } from 'solid-js'
|
|
2
|
+
import { REVALIDATE_HEADER, getHydrationWriter } from '@solidjs/web'
|
|
3
3
|
import { hydrate } from '@tanstack/query-core'
|
|
4
4
|
import { subscribeFlightData } from '@solidjs/web/server-functions'
|
|
5
5
|
import type { DehydratedState, Query } from '@tanstack/query-core'
|
|
@@ -39,15 +39,21 @@ export const HYDRATION_KEY_PREFIX = 'sq:'
|
|
|
39
39
|
*
|
|
40
40
|
* A mutation can additionally declare its invalidation SCOPE with
|
|
41
41
|
* `X-Revalidate` keys (the `revalidate` option of Solid's `reload`/
|
|
42
|
-
* `redirect` response helpers). The payload covers the slice of
|
|
43
|
-
* the server could recompute; whatever a declared key matches
|
|
44
|
-
* swept client-side (see the consumer below).
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* present, rather than skipping the source.
|
|
42
|
+
* `redirect`/`respond` response helpers). The payload covers the slice of
|
|
43
|
+
* that scope the server could recompute; whatever a declared key matches
|
|
44
|
+
* beyond it is swept client-side (see the consumer below). The transport
|
|
45
|
+
* delivers a response carrying keys to the consumer whether or not the
|
|
46
|
+
* collector contributed a slice — with no slice, the whole scope is swept.
|
|
48
47
|
*/
|
|
49
48
|
export const FLIGHT_DATA_SOURCE = 'sq'
|
|
50
49
|
|
|
50
|
+
/**
|
|
51
|
+
* The reserved `X-Revalidate` key meaning every entry: `revalidate: '*'`
|
|
52
|
+
* on Solid's response helpers. Named so a host that keys its cache
|
|
53
|
+
* differently still reads the same spelling as "all".
|
|
54
|
+
*/
|
|
55
|
+
const REVALIDATE_ALL = '*'
|
|
56
|
+
|
|
51
57
|
/**
|
|
52
58
|
* The client half of key-scoped invalidation. A mutation declares its
|
|
53
59
|
* invalidation scope with `X-Revalidate` keys; the flight payload covers
|
|
@@ -61,24 +67,44 @@ export const FLIGHT_DATA_SOURCE = 'sq'
|
|
|
61
67
|
* A key matches by queryKey prefix — `revalidate: 'users'` sweeps every
|
|
62
68
|
* query whose key begins with `'users'`. Payload-covered hashes are exempt:
|
|
63
69
|
* they hydrated with fresh data a moment ago, and refetching them would
|
|
64
|
-
* spend the round trip single flight just saved.
|
|
70
|
+
* spend the round trip single flight just saved. The payload is absent
|
|
71
|
+
* when the server folded no slice for this cache (no collector registered,
|
|
72
|
+
* a redirect leaving the app) — then nothing is covered and the declared
|
|
73
|
+
* scope is swept in full.
|
|
74
|
+
*
|
|
75
|
+
* The header's three states are three scopes. Absent: the mutation
|
|
76
|
+
* declared nothing, and this cache is left as it is — the route data the
|
|
77
|
+
* router reloads is its own business, and a response helper without
|
|
78
|
+
* `revalidate` is a host-default that Solid Router reads as "everything"
|
|
79
|
+
* but the query cache does not presume. Empty (`revalidate: []`): nothing,
|
|
80
|
+
* explicitly. The reserved key `*` (`revalidate: '*'`): every query in the
|
|
81
|
+
* cache, minus what the payload covered — the way to say "all" to any
|
|
82
|
+
* host, rather than relying on one host's default.
|
|
65
83
|
*/
|
|
66
84
|
function sweepRevalidatedQueries(
|
|
67
85
|
client: QueryClient,
|
|
68
|
-
payload: DehydratedState,
|
|
86
|
+
payload: DehydratedState | undefined,
|
|
69
87
|
response: Response,
|
|
70
88
|
): void {
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
const covered = new Set(
|
|
89
|
+
const declared = response.headers.get(REVALIDATE_HEADER)
|
|
90
|
+
if (declared === null) return
|
|
91
|
+
const covered = new Set(
|
|
92
|
+
payload?.queries.map((query) => query.queryHash) ?? [],
|
|
93
|
+
)
|
|
94
|
+
const uncovered = (query: Query) => !covered.has(query.queryHash)
|
|
95
|
+
const keys = declared.split(',')
|
|
96
|
+
// Background refetch failures surface through the queries' own error
|
|
97
|
+
// states; an unhandled rejection here would fail the mutation instead.
|
|
98
|
+
if (keys.includes(REVALIDATE_ALL)) {
|
|
99
|
+
client.invalidateQueries({ predicate: uncovered }).catch(() => undefined)
|
|
100
|
+
return
|
|
101
|
+
}
|
|
74
102
|
for (const key of keys) {
|
|
103
|
+
// An empty key is the empty declaration's spelling, not a prefix that
|
|
104
|
+
// happens to match everything.
|
|
105
|
+
if (!key) continue
|
|
75
106
|
client
|
|
76
|
-
.invalidateQueries({
|
|
77
|
-
queryKey: [key],
|
|
78
|
-
predicate: (query) => !covered.has(query.queryHash),
|
|
79
|
-
})
|
|
80
|
-
// Background refetch failures surface through the queries' own error
|
|
81
|
-
// states; an unhandled rejection here would fail the mutation instead.
|
|
107
|
+
.invalidateQueries({ queryKey: [key], predicate: uncovered })
|
|
82
108
|
.catch(() => undefined)
|
|
83
109
|
}
|
|
84
110
|
}
|
|
@@ -122,16 +148,8 @@ export type QueryClientProviderProps = {
|
|
|
122
148
|
* serialize, so seroval's cross-reference dedupe emits it once.
|
|
123
149
|
*/
|
|
124
150
|
function serializeCacheOnServer(client: QueryClient): void {
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
context?: {
|
|
128
|
-
async?: boolean
|
|
129
|
-
noHydrate?: boolean
|
|
130
|
-
serialize: (key: string, value: unknown) => void
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
).context
|
|
134
|
-
if (!ctx || !ctx.async || ctx.noHydrate) return
|
|
151
|
+
const writer = getHydrationWriter()
|
|
152
|
+
if (!writer || !writer.async || !isHydratable()) return
|
|
135
153
|
|
|
136
154
|
const cache = client.getQueryCache()
|
|
137
155
|
// The standard dehydrate filter gates the wire here too, so apps keep
|
|
@@ -149,7 +167,7 @@ function serializeCacheOnServer(client: QueryClient): void {
|
|
|
149
167
|
const state = query.state
|
|
150
168
|
if (state.status === 'success') {
|
|
151
169
|
seen.add(query.queryHash)
|
|
152
|
-
|
|
170
|
+
writer.write(HYDRATION_KEY_PREFIX + query.queryHash, {
|
|
153
171
|
data: state.data,
|
|
154
172
|
t: state.dataUpdatedAt,
|
|
155
173
|
})
|
|
@@ -159,7 +177,7 @@ function serializeCacheOnServer(client: QueryClient): void {
|
|
|
159
177
|
const promise = query.promise as Promise<unknown> | undefined
|
|
160
178
|
if (!promise) return
|
|
161
179
|
seen.add(query.queryHash)
|
|
162
|
-
|
|
180
|
+
writer.write(
|
|
163
181
|
HYDRATION_KEY_PREFIX + query.queryHash,
|
|
164
182
|
promise.then(() => ({
|
|
165
183
|
data: query.state.data,
|
|
@@ -208,6 +226,8 @@ export const QueryClientProvider = (
|
|
|
208
226
|
subscribeFlightData<DehydratedState>(
|
|
209
227
|
FLIGHT_DATA_SOURCE,
|
|
210
228
|
(data, context) => {
|
|
229
|
+
// `data` is undefined for a response that carried metadata but
|
|
230
|
+
// no slice for this cache; `hydrate` ignores a non-object.
|
|
211
231
|
hydrate(props.client, data)
|
|
212
232
|
// Fresh data first, then the declared-scope sweep: stale marks
|
|
213
233
|
// land synchronously, refetches run in the background — the
|
package/src/cacheAggregate.ts
CHANGED
|
@@ -2,8 +2,8 @@ import {
|
|
|
2
2
|
createEffect,
|
|
3
3
|
createOptimistic,
|
|
4
4
|
createSignal,
|
|
5
|
+
isHydrating,
|
|
5
6
|
onCleanup,
|
|
6
|
-
sharedConfig,
|
|
7
7
|
untrack,
|
|
8
8
|
} from 'solid-js'
|
|
9
9
|
import type { Accessor } from 'solid-js'
|
|
@@ -47,8 +47,7 @@ export function createCacheAggregate<T>(
|
|
|
47
47
|
read: (prev: T) => T,
|
|
48
48
|
empty: T,
|
|
49
49
|
): Accessor<T> {
|
|
50
|
-
const hydratedMount =
|
|
51
|
-
!isServer && (sharedConfig as { hydrating?: boolean }).hydrating === true
|
|
50
|
+
const hydratedMount = isHydrating()
|
|
52
51
|
|
|
53
52
|
// Cast: createSignal's value overload excludes functions (a function
|
|
54
53
|
// argument means a compute); aggregate values are counts and arrays.
|
package/src/useBaseQuery.ts
CHANGED
|
@@ -4,13 +4,14 @@ import {
|
|
|
4
4
|
createProjection,
|
|
5
5
|
createRenderEffect,
|
|
6
6
|
createSignal,
|
|
7
|
+
isHydrating,
|
|
7
8
|
isPending as isValuePending,
|
|
8
9
|
onCleanup,
|
|
9
10
|
resolve,
|
|
10
11
|
runWithOwner,
|
|
11
|
-
sharedConfig,
|
|
12
12
|
untrack,
|
|
13
13
|
} from 'solid-js'
|
|
14
|
+
import { takeHydrationValue } from '@solidjs/web'
|
|
14
15
|
import { HYDRATION_KEY_PREFIX, useQueryClient } from './QueryClientProvider'
|
|
15
16
|
import { useIsRestoring } from './isRestoring'
|
|
16
17
|
import type { UseBaseQueryOptions, UseBaseQueryResult } from './types'
|
|
@@ -166,12 +167,10 @@ export function useBaseQueryLayer<
|
|
|
166
167
|
)
|
|
167
168
|
|
|
168
169
|
/**
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* has no such field — undefined there, and unused.)
|
|
170
|
+
* Solid's own "this component is being hydrated" answer, captured at
|
|
171
|
+
* setup. Always false on the server.
|
|
172
172
|
*/
|
|
173
|
-
const hydratedMount =
|
|
174
|
-
!isServer && (sharedConfig as { hydrating?: boolean }).hydrating === true
|
|
173
|
+
const hydratedMount = isHydrating()
|
|
175
174
|
|
|
176
175
|
/**
|
|
177
176
|
* One version signal per hook, bumped by cache events for this query hash.
|
|
@@ -216,6 +215,27 @@ export function useBaseQueryLayer<
|
|
|
216
215
|
* swap re-attaches the rebuilt observer iff its predecessor was attached. */
|
|
217
216
|
let shouldAttach = false
|
|
218
217
|
let activeClient = untrack(client)
|
|
218
|
+
/**
|
|
219
|
+
* The entry this hook last read at its current hash, and the fallback for
|
|
220
|
+
* `query()` when the cache no longer holds that hash. Removal is the case
|
|
221
|
+
* that needs it: `removeQueries()` (or `clear()`) fires 'removed' for a
|
|
222
|
+
* hash this hook is mounted on, which bumps `version` and re-runs every
|
|
223
|
+
* derived read, and a plain `build()` there would re-create the entry the
|
|
224
|
+
* caller just removed. That resurrection is not passive, since the 'added'
|
|
225
|
+
* entry also re-points the still-live observer, whose mount-fetch policy
|
|
226
|
+
* then refetches and repopulates the key, contrary to `removeQueries`
|
|
227
|
+
* being documented to remove entries instead of refetching them.
|
|
228
|
+
*
|
|
229
|
+
* The removed instance keeps its final state, so reads hold their last
|
|
230
|
+
* value until options change or a real entry returns (`setQueryData`, a
|
|
231
|
+
* refetch, a later mount). That is what the other adapters do: a React
|
|
232
|
+
* observer keeps rendering the removed query's last result while the
|
|
233
|
+
* cache stays empty.
|
|
234
|
+
*
|
|
235
|
+
* Cleared on a client swap, below: the fallback is only meaningful for the
|
|
236
|
+
* cache it was read from.
|
|
237
|
+
*/
|
|
238
|
+
let lastQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined
|
|
219
239
|
|
|
220
240
|
const attach = () => {
|
|
221
241
|
if (!disposed && !observerSub && !untrack(isRestoring)) {
|
|
@@ -236,6 +256,7 @@ export function useBaseQueryLayer<
|
|
|
236
256
|
const syncClient = (c: QueryClient) => {
|
|
237
257
|
if (isServer || c === activeClient) return
|
|
238
258
|
activeClient = c
|
|
259
|
+
lastQuery = undefined
|
|
239
260
|
cacheSub?.()
|
|
240
261
|
cacheSub = c.getQueryCache().subscribe(onCacheEvent)
|
|
241
262
|
observerSub?.()
|
|
@@ -314,10 +335,6 @@ export function useBaseQueryLayer<
|
|
|
314
335
|
|
|
315
336
|
if (!isServer) {
|
|
316
337
|
cacheSub = activeClient.getQueryCache().subscribe(onCacheEvent)
|
|
317
|
-
const sc = sharedConfig as unknown as {
|
|
318
|
-
has?: (id: string) => boolean
|
|
319
|
-
load?: (id: string) => any
|
|
320
|
-
}
|
|
321
338
|
const registryKey =
|
|
322
339
|
HYDRATION_KEY_PREFIX + untrack(defaultedOptions).queryHash
|
|
323
340
|
const primeAndAttach = (entry?: Partial<HydratedEntry> | null) => {
|
|
@@ -326,24 +343,12 @@ export function useBaseQueryLayer<
|
|
|
326
343
|
attach()
|
|
327
344
|
}
|
|
328
345
|
const settle = hydratedMount ? primeAndAttach : prime
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
// ones are plain thenable refs that land with their chunk.
|
|
336
|
-
if (entry.s === 1) settle(entry.v)
|
|
337
|
-
else if (entry.s === 2) settle()
|
|
338
|
-
else if (typeof entry.then === 'function')
|
|
339
|
-
entry.then(settle, () => settle())
|
|
340
|
-
else settle(entry)
|
|
341
|
-
} else {
|
|
342
|
-
settle(entry)
|
|
343
|
-
}
|
|
344
|
-
} else if (hydratedMount) {
|
|
345
|
-
primeAndAttach()
|
|
346
|
-
}
|
|
346
|
+
const entry = takeHydrationValue<Partial<HydratedEntry> | null>(registryKey)
|
|
347
|
+
if (entry?.status === 'resolved') settle(entry.value)
|
|
348
|
+
else if (entry?.status === 'rejected') settle()
|
|
349
|
+
else if (entry?.status === 'pending')
|
|
350
|
+
entry.promise.then(settle, () => settle())
|
|
351
|
+
else if (hydratedMount) primeAndAttach()
|
|
347
352
|
if (!hydratedMount) {
|
|
348
353
|
createRenderEffect(
|
|
349
354
|
() => isRestoring(),
|
|
@@ -361,11 +366,40 @@ export function useBaseQueryLayer<
|
|
|
361
366
|
})
|
|
362
367
|
}
|
|
363
368
|
|
|
364
|
-
|
|
365
|
-
|
|
369
|
+
/**
|
|
370
|
+
* The cache entry for the current options, without the `version` read —
|
|
371
|
+
* for the setup-time snapshots below (`metaProjection`'s seed,
|
|
372
|
+
* `mountedAt`), which run in whatever computation is instantiating this
|
|
373
|
+
* component. Under hydration that matters: priming bumps `version`
|
|
374
|
+
* during setup, and Solid holds a write made during the hydration pass —
|
|
375
|
+
* a computation in the pass that reads the written signal, tracked or
|
|
376
|
+
* not, is served the pre-write snapshot and replays at release. For the
|
|
377
|
+
* hook's own derived nodes that replay is the takeover. For the
|
|
378
|
+
* instantiating computation (a boundary's children, a `<Show>` branch)
|
|
379
|
+
* it is a remount: the hydrated component is disposed and re-created as
|
|
380
|
+
* a client render, and the server value stops holding the DOM. So the
|
|
381
|
+
* snapshots read the cache, never the signal.
|
|
382
|
+
*/
|
|
383
|
+
const lookupQuery = (): Query<
|
|
384
|
+
TQueryFnData,
|
|
385
|
+
TError,
|
|
386
|
+
TQueryData,
|
|
387
|
+
TQueryKey
|
|
388
|
+
> => {
|
|
366
389
|
const c = client()
|
|
367
390
|
syncClient(c)
|
|
368
|
-
|
|
391
|
+
const cache = c.getQueryCache()
|
|
392
|
+
const opts = defaultedOptions()
|
|
393
|
+
const existing = cache.get<TQueryFnData, TError, TQueryData, TQueryKey>(
|
|
394
|
+
opts.queryHash,
|
|
395
|
+
)
|
|
396
|
+
if (existing) return (lastQuery = existing)
|
|
397
|
+
if (lastQuery?.queryHash === opts.queryHash) return lastQuery
|
|
398
|
+
return (lastQuery = cache.build(c, opts as any) as any)
|
|
399
|
+
}
|
|
400
|
+
const query = (): Query<TQueryFnData, TError, TQueryData, TQueryKey> => {
|
|
401
|
+
version()
|
|
402
|
+
return lookupQuery()
|
|
369
403
|
}
|
|
370
404
|
|
|
371
405
|
const isEnabled = () => {
|
|
@@ -493,7 +527,7 @@ export function useBaseQueryLayer<
|
|
|
493
527
|
* priming; if a fetch is genuinely needed, the post-priming observer
|
|
494
528
|
* attach issues it outside the window.
|
|
495
529
|
*/
|
|
496
|
-
if (
|
|
530
|
+
if (isHydrating()) {
|
|
497
531
|
return NEVER
|
|
498
532
|
}
|
|
499
533
|
/**
|
|
@@ -531,6 +565,18 @@ export function useBaseQueryLayer<
|
|
|
531
565
|
if (!isServer) observer.setOptions(opts as any)
|
|
532
566
|
return chainOnce(q.fetch(opts as any), select, wrap)
|
|
533
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* Disabled, with nothing cached. The three guards above are all
|
|
570
|
+
* unreachable on the server, but this one is not, and parking here has
|
|
571
|
+
* no server meaning: the render has to finish, and nothing will enable
|
|
572
|
+
* the query or write the cache before it does. So commit the idle
|
|
573
|
+
* value — 'pending' with no data IS a disabled query's settled SSR
|
|
574
|
+
* truth, which is the contract `serverMeta` already honors by not
|
|
575
|
+
* tying its read to this node while disabled, and it is the state the
|
|
576
|
+
* client hydrates to. Committed rather than passed through `wrap`:
|
|
577
|
+
* `select` must not be invoked on absent data.
|
|
578
|
+
*/
|
|
579
|
+
if (isServer) return { value: undefined as TData }
|
|
534
580
|
return NEVER
|
|
535
581
|
}
|
|
536
582
|
|
|
@@ -552,12 +598,10 @@ export function useBaseQueryLayer<
|
|
|
552
598
|
* first-paint exception in `computeData`.
|
|
553
599
|
*
|
|
554
600
|
* Default ('server') hydration semantics: the serialized value owns the
|
|
555
|
-
* node
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
* Requires solid-js > 2.0.0-rc.3 (before the takeover fix, a mid-stream
|
|
560
|
-
* divergence was lost rather than deferred).
|
|
601
|
+
* node while it hydrates. Outside every still-pending streamed boundary,
|
|
602
|
+
* a cache write commits immediately. Under a pending boundary it waits
|
|
603
|
+
* for that boundary to resume, which hydrates against the server
|
|
604
|
+
* snapshot.
|
|
561
605
|
*/
|
|
562
606
|
const dataStore = createProjection<{ value: TData }>(
|
|
563
607
|
(draft) => computeData(draft.value as TData | undefined),
|
|
@@ -603,7 +647,7 @@ export function useBaseQueryLayer<
|
|
|
603
647
|
(draft) => {
|
|
604
648
|
Object.assign(draft, metaFrom(query().state))
|
|
605
649
|
},
|
|
606
|
-
untrack(() => metaFrom(
|
|
650
|
+
untrack(() => metaFrom(lookupQuery().state)),
|
|
607
651
|
)
|
|
608
652
|
const meta = isServer
|
|
609
653
|
? new Proxy({} as MetaState, {
|
|
@@ -627,7 +671,7 @@ export function useBaseQueryLayer<
|
|
|
627
671
|
},
|
|
628
672
|
}
|
|
629
673
|
: untrack(() => {
|
|
630
|
-
const state =
|
|
674
|
+
const state = lookupQuery().state
|
|
631
675
|
return {
|
|
632
676
|
dataUpdateCount: state.dataUpdateCount,
|
|
633
677
|
errorUpdateCount: state.errorUpdateCount,
|