@tanstack/query-persist-client-core 5.0.0-beta.23 → 5.0.0-beta.28
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/legacy/createPersister.cjs +100 -0
- package/build/legacy/createPersister.cjs.map +1 -0
- package/build/legacy/createPersister.d.cts +78 -0
- package/build/legacy/createPersister.d.ts +78 -0
- package/build/legacy/createPersister.js +74 -0
- package/build/legacy/createPersister.js.map +1 -0
- package/build/legacy/index.cjs +3 -1
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +1 -0
- package/build/legacy/index.d.ts +1 -0
- package/build/legacy/index.js +1 -0
- package/build/legacy/index.js.map +1 -1
- package/build/modern/createPersister.cjs +100 -0
- package/build/modern/createPersister.cjs.map +1 -0
- package/build/modern/createPersister.d.cts +78 -0
- package/build/modern/createPersister.d.ts +78 -0
- package/build/modern/createPersister.js +74 -0
- package/build/modern/createPersister.js.map +1 -0
- package/build/modern/index.cjs +3 -1
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +1 -0
- package/build/modern/index.d.ts +1 -0
- package/build/modern/index.js +1 -0
- package/build/modern/index.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/createPersister.test.ts +312 -0
- package/src/createPersister.ts +160 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/createPersister.ts
|
|
21
|
+
var createPersister_exports = {};
|
|
22
|
+
__export(createPersister_exports, {
|
|
23
|
+
PERSISTER_KEY_PREFIX: () => PERSISTER_KEY_PREFIX,
|
|
24
|
+
experimental_createPersister: () => experimental_createPersister
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(createPersister_exports);
|
|
27
|
+
var import_query_core = require("@tanstack/query-core");
|
|
28
|
+
var PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
29
|
+
function experimental_createPersister({
|
|
30
|
+
storage,
|
|
31
|
+
buster = "",
|
|
32
|
+
maxAge = 1e3 * 60 * 60 * 24,
|
|
33
|
+
serialize = JSON.stringify,
|
|
34
|
+
deserialize = JSON.parse,
|
|
35
|
+
prefix = PERSISTER_KEY_PREFIX,
|
|
36
|
+
filters
|
|
37
|
+
}) {
|
|
38
|
+
return async function persisterFn(queryFn, context, query) {
|
|
39
|
+
const storageKey = `${prefix}-${query.queryHash}`;
|
|
40
|
+
const matchesFilter = filters ? (0, import_query_core.matchQuery)(filters, query) : true;
|
|
41
|
+
if (matchesFilter && query.state.data === void 0 && storage != null) {
|
|
42
|
+
try {
|
|
43
|
+
const storedData = await storage.getItem(storageKey);
|
|
44
|
+
if (storedData) {
|
|
45
|
+
const persistedQuery = deserialize(storedData);
|
|
46
|
+
if (persistedQuery.state.dataUpdatedAt) {
|
|
47
|
+
const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt;
|
|
48
|
+
const expired = queryAge > maxAge;
|
|
49
|
+
const busted = persistedQuery.buster !== buster;
|
|
50
|
+
if (expired || busted) {
|
|
51
|
+
await storage.removeItem(storageKey);
|
|
52
|
+
} else {
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
query.setState({
|
|
55
|
+
dataUpdatedAt: persistedQuery.state.dataUpdatedAt,
|
|
56
|
+
errorUpdatedAt: persistedQuery.state.errorUpdatedAt
|
|
57
|
+
});
|
|
58
|
+
if (query.isStale()) {
|
|
59
|
+
query.fetch();
|
|
60
|
+
}
|
|
61
|
+
}, 0);
|
|
62
|
+
return Promise.resolve(persistedQuery.state.data);
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
await storage.removeItem(storageKey);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} catch (err) {
|
|
69
|
+
if (process.env.NODE_ENV === "development") {
|
|
70
|
+
console.error(err);
|
|
71
|
+
console.warn(
|
|
72
|
+
"Encountered an error attempting to restore query cache from persisted location."
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
await storage.removeItem(storageKey);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const queryFnResult = await queryFn(context);
|
|
79
|
+
if (matchesFilter && storage != null) {
|
|
80
|
+
setTimeout(() => {
|
|
81
|
+
storage.setItem(
|
|
82
|
+
storageKey,
|
|
83
|
+
serialize({
|
|
84
|
+
state: query.state,
|
|
85
|
+
queryKey: query.queryKey,
|
|
86
|
+
queryHash: query.queryHash,
|
|
87
|
+
buster
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
}, 0);
|
|
91
|
+
}
|
|
92
|
+
return Promise.resolve(queryFnResult);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
+
0 && (module.exports = {
|
|
97
|
+
PERSISTER_KEY_PREFIX,
|
|
98
|
+
experimental_createPersister
|
|
99
|
+
});
|
|
100
|
+
//# sourceMappingURL=createPersister.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/createPersister.ts"],"sourcesContent":["import { matchQuery } from '@tanstack/query-core'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type {\n Query,\n QueryFunctionContext,\n QueryKey,\n QueryState,\n} from '@tanstack/query-core'\n\nexport interface PersistedQuery {\n buster: string\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | undefined | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport interface StoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`.\n */\n storage: AsyncStorage | Storage | undefined | null\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (persistedQuery: PersistedQuery) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedQuery\n /**\n * A unique string that can be used to forcefully invalidate existing caches,\n * if they do not share the same buster string\n */\n buster?: string\n /**\n * The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded\n * @default 24 hours\n */\n maxAge?: number\n /**\n * Prefix to be used for storage key.\n * Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.\n * @default 'tanstack-query'\n */\n prefix?: string\n /**\n * Filters to narrow down which Queries should be persisted.\n */\n filters?: QueryFilters\n}\n\nexport const PERSISTER_KEY_PREFIX = 'tanstack-query'\n\n/**\n * Warning: experimental feature.\n * This utility function enables fine-grained query persistance.\n * Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.\n *\n * ```\n * useQuery({\n queryKey: ['myKey'],\n queryFn: fetcher,\n persister: createPersister({\n storage: localStorage,\n }),\n })\n ```\n */\nexport function experimental_createPersister({\n storage,\n buster = '',\n maxAge = 1000 * 60 * 60 * 24,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n prefix = PERSISTER_KEY_PREFIX,\n filters,\n}: StoragePersisterOptions) {\n return async function persisterFn<T, TQueryKey extends QueryKey>(\n queryFn: (context: QueryFunctionContext<TQueryKey>) => T | Promise<T>,\n context: QueryFunctionContext<TQueryKey>,\n query: Query,\n ) {\n const storageKey = `${prefix}-${query.queryHash}`\n const matchesFilter = filters ? matchQuery(filters, query) : true\n\n // Try to restore only if we do not have any data in the cache and we have persister defined\n if (matchesFilter && query.state.data === undefined && storage != null) {\n try {\n const storedData = await storage.getItem(storageKey)\n if (storedData) {\n const persistedQuery = deserialize(storedData)\n\n if (persistedQuery.state.dataUpdatedAt) {\n const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt\n const expired = queryAge > maxAge\n const busted = persistedQuery.buster !== buster\n if (expired || busted) {\n await storage.removeItem(storageKey)\n } else {\n // Just after restoring we want to get fresh data from the server if it's stale\n setTimeout(() => {\n // Set proper updatedAt, since resolving in the first pass overrides those values\n query.setState({\n dataUpdatedAt: persistedQuery.state.dataUpdatedAt,\n errorUpdatedAt: persistedQuery.state.errorUpdatedAt,\n })\n\n if (query.isStale()) {\n query.fetch()\n }\n }, 0)\n // We must resolve the promise here, as otherwise we will have `loading` state in the app until `queryFn` resolves\n return Promise.resolve(persistedQuery.state.data as T)\n }\n } else {\n await storage.removeItem(storageKey)\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV === 'development') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore query cache from persisted location.',\n )\n }\n await storage.removeItem(storageKey)\n }\n }\n\n // If we did not restore, or restoration failed - fetch\n const queryFnResult = await queryFn(context)\n\n if (matchesFilter && storage != null) {\n // Persist if we have storage defined, we use timeout to get proper state to be persisted\n setTimeout(() => {\n storage.setItem(\n storageKey,\n serialize({\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n buster: buster,\n }),\n )\n }, 0)\n }\n\n return Promise.resolve(queryFnResult)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAA2B;AA6DpB,IAAM,uBAAuB;AAiB7B,SAAS,6BAA6B;AAAA,EAC3C;AAAA,EACA,SAAS;AAAA,EACT,SAAS,MAAO,KAAK,KAAK;AAAA,EAC1B,YAAY,KAAK;AAAA,EACjB,cAAc,KAAK;AAAA,EACnB,SAAS;AAAA,EACT;AACF,GAA4B;AAC1B,SAAO,eAAe,YACpB,SACA,SACA,OACA;AACA,UAAM,aAAa,GAAG,MAAM,IAAI,MAAM,SAAS;AAC/C,UAAM,gBAAgB,cAAU,8BAAW,SAAS,KAAK,IAAI;AAG7D,QAAI,iBAAiB,MAAM,MAAM,SAAS,UAAa,WAAW,MAAM;AACtE,UAAI;AACF,cAAM,aAAa,MAAM,QAAQ,QAAQ,UAAU;AACnD,YAAI,YAAY;AACd,gBAAM,iBAAiB,YAAY,UAAU;AAE7C,cAAI,eAAe,MAAM,eAAe;AACtC,kBAAM,WAAW,KAAK,IAAI,IAAI,eAAe,MAAM;AACnD,kBAAM,UAAU,WAAW;AAC3B,kBAAM,SAAS,eAAe,WAAW;AACzC,gBAAI,WAAW,QAAQ;AACrB,oBAAM,QAAQ,WAAW,UAAU;AAAA,YACrC,OAAO;AAEL,yBAAW,MAAM;AAEf,sBAAM,SAAS;AAAA,kBACb,eAAe,eAAe,MAAM;AAAA,kBACpC,gBAAgB,eAAe,MAAM;AAAA,gBACvC,CAAC;AAED,oBAAI,MAAM,QAAQ,GAAG;AACnB,wBAAM,MAAM;AAAA,gBACd;AAAA,cACF,GAAG,CAAC;AAEJ,qBAAO,QAAQ,QAAQ,eAAe,MAAM,IAAS;AAAA,YACvD;AAAA,UACF,OAAO;AACL,kBAAM,QAAQ,WAAW,UAAU;AAAA,UACrC;AAAA,QACF;AAAA,MACF,SAAS,KAAK;AACZ,YAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,kBAAQ,MAAM,GAAG;AACjB,kBAAQ;AAAA,YACN;AAAA,UACF;AAAA,QACF;AACA,cAAM,QAAQ,WAAW,UAAU;AAAA,MACrC;AAAA,IACF;AAGA,UAAM,gBAAgB,MAAM,QAAQ,OAAO;AAE3C,QAAI,iBAAiB,WAAW,MAAM;AAEpC,iBAAW,MAAM;AACf,gBAAQ;AAAA,UACN;AAAA,UACA,UAAU;AAAA,YACR,OAAO,MAAM;AAAA,YACb,UAAU,MAAM;AAAA,YAChB,WAAW,MAAM;AAAA,YACjB;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MACF,GAAG,CAAC;AAAA,IACN;AAEA,WAAO,QAAQ,QAAQ,aAAa;AAAA,EACtC;AACF;","names":[]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { QueryKey, QueryState, QueryFilters, Query } from '@tanstack/query-core';
|
|
2
|
+
|
|
3
|
+
interface PersistedQuery {
|
|
4
|
+
buster: string;
|
|
5
|
+
queryHash: string;
|
|
6
|
+
queryKey: QueryKey;
|
|
7
|
+
state: QueryState;
|
|
8
|
+
}
|
|
9
|
+
interface AsyncStorage {
|
|
10
|
+
getItem: (key: string) => Promise<string | undefined | null>;
|
|
11
|
+
setItem: (key: string, value: string) => Promise<unknown>;
|
|
12
|
+
removeItem: (key: string) => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
interface StoragePersisterOptions {
|
|
15
|
+
/** The storage client used for setting and retrieving items from cache.
|
|
16
|
+
* For SSR pass in `undefined`.
|
|
17
|
+
*/
|
|
18
|
+
storage: AsyncStorage | Storage | undefined | null;
|
|
19
|
+
/**
|
|
20
|
+
* How to serialize the data to storage.
|
|
21
|
+
* @default `JSON.stringify`
|
|
22
|
+
*/
|
|
23
|
+
serialize?: (persistedQuery: PersistedQuery) => string;
|
|
24
|
+
/**
|
|
25
|
+
* How to deserialize the data from storage.
|
|
26
|
+
* @default `JSON.parse`
|
|
27
|
+
*/
|
|
28
|
+
deserialize?: (cachedString: string) => PersistedQuery;
|
|
29
|
+
/**
|
|
30
|
+
* A unique string that can be used to forcefully invalidate existing caches,
|
|
31
|
+
* if they do not share the same buster string
|
|
32
|
+
*/
|
|
33
|
+
buster?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The max-allowed age of the cache in milliseconds.
|
|
36
|
+
* If a persisted cache is found that is older than this
|
|
37
|
+
* time, it will be discarded
|
|
38
|
+
* @default 24 hours
|
|
39
|
+
*/
|
|
40
|
+
maxAge?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Prefix to be used for storage key.
|
|
43
|
+
* Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.
|
|
44
|
+
* @default 'tanstack-query'
|
|
45
|
+
*/
|
|
46
|
+
prefix?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Filters to narrow down which Queries should be persisted.
|
|
49
|
+
*/
|
|
50
|
+
filters?: QueryFilters;
|
|
51
|
+
}
|
|
52
|
+
declare const PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
53
|
+
/**
|
|
54
|
+
* Warning: experimental feature.
|
|
55
|
+
* This utility function enables fine-grained query persistance.
|
|
56
|
+
* Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.
|
|
57
|
+
*
|
|
58
|
+
* ```
|
|
59
|
+
* useQuery({
|
|
60
|
+
queryKey: ['myKey'],
|
|
61
|
+
queryFn: fetcher,
|
|
62
|
+
persister: createPersister({
|
|
63
|
+
storage: localStorage,
|
|
64
|
+
}),
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
*/
|
|
68
|
+
declare function experimental_createPersister({ storage, buster, maxAge, serialize, deserialize, prefix, filters, }: StoragePersisterOptions): <T, TQueryKey extends QueryKey>(queryFn: (context: {
|
|
69
|
+
queryKey: TQueryKey;
|
|
70
|
+
signal: AbortSignal;
|
|
71
|
+
meta: Record<string, unknown> | undefined;
|
|
72
|
+
}) => T | Promise<T>, context: {
|
|
73
|
+
queryKey: TQueryKey;
|
|
74
|
+
signal: AbortSignal;
|
|
75
|
+
meta: Record<string, unknown> | undefined;
|
|
76
|
+
}, query: Query) => Promise<T>;
|
|
77
|
+
|
|
78
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { QueryKey, QueryState, QueryFilters, Query } from '@tanstack/query-core';
|
|
2
|
+
|
|
3
|
+
interface PersistedQuery {
|
|
4
|
+
buster: string;
|
|
5
|
+
queryHash: string;
|
|
6
|
+
queryKey: QueryKey;
|
|
7
|
+
state: QueryState;
|
|
8
|
+
}
|
|
9
|
+
interface AsyncStorage {
|
|
10
|
+
getItem: (key: string) => Promise<string | undefined | null>;
|
|
11
|
+
setItem: (key: string, value: string) => Promise<unknown>;
|
|
12
|
+
removeItem: (key: string) => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
interface StoragePersisterOptions {
|
|
15
|
+
/** The storage client used for setting and retrieving items from cache.
|
|
16
|
+
* For SSR pass in `undefined`.
|
|
17
|
+
*/
|
|
18
|
+
storage: AsyncStorage | Storage | undefined | null;
|
|
19
|
+
/**
|
|
20
|
+
* How to serialize the data to storage.
|
|
21
|
+
* @default `JSON.stringify`
|
|
22
|
+
*/
|
|
23
|
+
serialize?: (persistedQuery: PersistedQuery) => string;
|
|
24
|
+
/**
|
|
25
|
+
* How to deserialize the data from storage.
|
|
26
|
+
* @default `JSON.parse`
|
|
27
|
+
*/
|
|
28
|
+
deserialize?: (cachedString: string) => PersistedQuery;
|
|
29
|
+
/**
|
|
30
|
+
* A unique string that can be used to forcefully invalidate existing caches,
|
|
31
|
+
* if they do not share the same buster string
|
|
32
|
+
*/
|
|
33
|
+
buster?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The max-allowed age of the cache in milliseconds.
|
|
36
|
+
* If a persisted cache is found that is older than this
|
|
37
|
+
* time, it will be discarded
|
|
38
|
+
* @default 24 hours
|
|
39
|
+
*/
|
|
40
|
+
maxAge?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Prefix to be used for storage key.
|
|
43
|
+
* Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.
|
|
44
|
+
* @default 'tanstack-query'
|
|
45
|
+
*/
|
|
46
|
+
prefix?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Filters to narrow down which Queries should be persisted.
|
|
49
|
+
*/
|
|
50
|
+
filters?: QueryFilters;
|
|
51
|
+
}
|
|
52
|
+
declare const PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
53
|
+
/**
|
|
54
|
+
* Warning: experimental feature.
|
|
55
|
+
* This utility function enables fine-grained query persistance.
|
|
56
|
+
* Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.
|
|
57
|
+
*
|
|
58
|
+
* ```
|
|
59
|
+
* useQuery({
|
|
60
|
+
queryKey: ['myKey'],
|
|
61
|
+
queryFn: fetcher,
|
|
62
|
+
persister: createPersister({
|
|
63
|
+
storage: localStorage,
|
|
64
|
+
}),
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
*/
|
|
68
|
+
declare function experimental_createPersister({ storage, buster, maxAge, serialize, deserialize, prefix, filters, }: StoragePersisterOptions): <T, TQueryKey extends QueryKey>(queryFn: (context: {
|
|
69
|
+
queryKey: TQueryKey;
|
|
70
|
+
signal: AbortSignal;
|
|
71
|
+
meta: Record<string, unknown> | undefined;
|
|
72
|
+
}) => T | Promise<T>, context: {
|
|
73
|
+
queryKey: TQueryKey;
|
|
74
|
+
signal: AbortSignal;
|
|
75
|
+
meta: Record<string, unknown> | undefined;
|
|
76
|
+
}, query: Query) => Promise<T>;
|
|
77
|
+
|
|
78
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/createPersister.ts
|
|
2
|
+
import { matchQuery } from "@tanstack/query-core";
|
|
3
|
+
var PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
4
|
+
function experimental_createPersister({
|
|
5
|
+
storage,
|
|
6
|
+
buster = "",
|
|
7
|
+
maxAge = 1e3 * 60 * 60 * 24,
|
|
8
|
+
serialize = JSON.stringify,
|
|
9
|
+
deserialize = JSON.parse,
|
|
10
|
+
prefix = PERSISTER_KEY_PREFIX,
|
|
11
|
+
filters
|
|
12
|
+
}) {
|
|
13
|
+
return async function persisterFn(queryFn, context, query) {
|
|
14
|
+
const storageKey = `${prefix}-${query.queryHash}`;
|
|
15
|
+
const matchesFilter = filters ? matchQuery(filters, query) : true;
|
|
16
|
+
if (matchesFilter && query.state.data === void 0 && storage != null) {
|
|
17
|
+
try {
|
|
18
|
+
const storedData = await storage.getItem(storageKey);
|
|
19
|
+
if (storedData) {
|
|
20
|
+
const persistedQuery = deserialize(storedData);
|
|
21
|
+
if (persistedQuery.state.dataUpdatedAt) {
|
|
22
|
+
const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt;
|
|
23
|
+
const expired = queryAge > maxAge;
|
|
24
|
+
const busted = persistedQuery.buster !== buster;
|
|
25
|
+
if (expired || busted) {
|
|
26
|
+
await storage.removeItem(storageKey);
|
|
27
|
+
} else {
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
query.setState({
|
|
30
|
+
dataUpdatedAt: persistedQuery.state.dataUpdatedAt,
|
|
31
|
+
errorUpdatedAt: persistedQuery.state.errorUpdatedAt
|
|
32
|
+
});
|
|
33
|
+
if (query.isStale()) {
|
|
34
|
+
query.fetch();
|
|
35
|
+
}
|
|
36
|
+
}, 0);
|
|
37
|
+
return Promise.resolve(persistedQuery.state.data);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
await storage.removeItem(storageKey);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} catch (err) {
|
|
44
|
+
if (process.env.NODE_ENV === "development") {
|
|
45
|
+
console.error(err);
|
|
46
|
+
console.warn(
|
|
47
|
+
"Encountered an error attempting to restore query cache from persisted location."
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
await storage.removeItem(storageKey);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const queryFnResult = await queryFn(context);
|
|
54
|
+
if (matchesFilter && storage != null) {
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
storage.setItem(
|
|
57
|
+
storageKey,
|
|
58
|
+
serialize({
|
|
59
|
+
state: query.state,
|
|
60
|
+
queryKey: query.queryKey,
|
|
61
|
+
queryHash: query.queryHash,
|
|
62
|
+
buster
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
}, 0);
|
|
66
|
+
}
|
|
67
|
+
return Promise.resolve(queryFnResult);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
PERSISTER_KEY_PREFIX,
|
|
72
|
+
experimental_createPersister
|
|
73
|
+
};
|
|
74
|
+
//# sourceMappingURL=createPersister.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/createPersister.ts"],"sourcesContent":["import { matchQuery } from '@tanstack/query-core'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type {\n Query,\n QueryFunctionContext,\n QueryKey,\n QueryState,\n} from '@tanstack/query-core'\n\nexport interface PersistedQuery {\n buster: string\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | undefined | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport interface StoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`.\n */\n storage: AsyncStorage | Storage | undefined | null\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (persistedQuery: PersistedQuery) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedQuery\n /**\n * A unique string that can be used to forcefully invalidate existing caches,\n * if they do not share the same buster string\n */\n buster?: string\n /**\n * The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded\n * @default 24 hours\n */\n maxAge?: number\n /**\n * Prefix to be used for storage key.\n * Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.\n * @default 'tanstack-query'\n */\n prefix?: string\n /**\n * Filters to narrow down which Queries should be persisted.\n */\n filters?: QueryFilters\n}\n\nexport const PERSISTER_KEY_PREFIX = 'tanstack-query'\n\n/**\n * Warning: experimental feature.\n * This utility function enables fine-grained query persistance.\n * Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.\n *\n * ```\n * useQuery({\n queryKey: ['myKey'],\n queryFn: fetcher,\n persister: createPersister({\n storage: localStorage,\n }),\n })\n ```\n */\nexport function experimental_createPersister({\n storage,\n buster = '',\n maxAge = 1000 * 60 * 60 * 24,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n prefix = PERSISTER_KEY_PREFIX,\n filters,\n}: StoragePersisterOptions) {\n return async function persisterFn<T, TQueryKey extends QueryKey>(\n queryFn: (context: QueryFunctionContext<TQueryKey>) => T | Promise<T>,\n context: QueryFunctionContext<TQueryKey>,\n query: Query,\n ) {\n const storageKey = `${prefix}-${query.queryHash}`\n const matchesFilter = filters ? matchQuery(filters, query) : true\n\n // Try to restore only if we do not have any data in the cache and we have persister defined\n if (matchesFilter && query.state.data === undefined && storage != null) {\n try {\n const storedData = await storage.getItem(storageKey)\n if (storedData) {\n const persistedQuery = deserialize(storedData)\n\n if (persistedQuery.state.dataUpdatedAt) {\n const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt\n const expired = queryAge > maxAge\n const busted = persistedQuery.buster !== buster\n if (expired || busted) {\n await storage.removeItem(storageKey)\n } else {\n // Just after restoring we want to get fresh data from the server if it's stale\n setTimeout(() => {\n // Set proper updatedAt, since resolving in the first pass overrides those values\n query.setState({\n dataUpdatedAt: persistedQuery.state.dataUpdatedAt,\n errorUpdatedAt: persistedQuery.state.errorUpdatedAt,\n })\n\n if (query.isStale()) {\n query.fetch()\n }\n }, 0)\n // We must resolve the promise here, as otherwise we will have `loading` state in the app until `queryFn` resolves\n return Promise.resolve(persistedQuery.state.data as T)\n }\n } else {\n await storage.removeItem(storageKey)\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV === 'development') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore query cache from persisted location.',\n )\n }\n await storage.removeItem(storageKey)\n }\n }\n\n // If we did not restore, or restoration failed - fetch\n const queryFnResult = await queryFn(context)\n\n if (matchesFilter && storage != null) {\n // Persist if we have storage defined, we use timeout to get proper state to be persisted\n setTimeout(() => {\n storage.setItem(\n storageKey,\n serialize({\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n buster: buster,\n }),\n )\n }, 0)\n }\n\n return Promise.resolve(queryFnResult)\n }\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AA6DpB,IAAM,uBAAuB;AAiB7B,SAAS,6BAA6B;AAAA,EAC3C;AAAA,EACA,SAAS;AAAA,EACT,SAAS,MAAO,KAAK,KAAK;AAAA,EAC1B,YAAY,KAAK;AAAA,EACjB,cAAc,KAAK;AAAA,EACnB,SAAS;AAAA,EACT;AACF,GAA4B;AAC1B,SAAO,eAAe,YACpB,SACA,SACA,OACA;AACA,UAAM,aAAa,GAAG,MAAM,IAAI,MAAM,SAAS;AAC/C,UAAM,gBAAgB,UAAU,WAAW,SAAS,KAAK,IAAI;AAG7D,QAAI,iBAAiB,MAAM,MAAM,SAAS,UAAa,WAAW,MAAM;AACtE,UAAI;AACF,cAAM,aAAa,MAAM,QAAQ,QAAQ,UAAU;AACnD,YAAI,YAAY;AACd,gBAAM,iBAAiB,YAAY,UAAU;AAE7C,cAAI,eAAe,MAAM,eAAe;AACtC,kBAAM,WAAW,KAAK,IAAI,IAAI,eAAe,MAAM;AACnD,kBAAM,UAAU,WAAW;AAC3B,kBAAM,SAAS,eAAe,WAAW;AACzC,gBAAI,WAAW,QAAQ;AACrB,oBAAM,QAAQ,WAAW,UAAU;AAAA,YACrC,OAAO;AAEL,yBAAW,MAAM;AAEf,sBAAM,SAAS;AAAA,kBACb,eAAe,eAAe,MAAM;AAAA,kBACpC,gBAAgB,eAAe,MAAM;AAAA,gBACvC,CAAC;AAED,oBAAI,MAAM,QAAQ,GAAG;AACnB,wBAAM,MAAM;AAAA,gBACd;AAAA,cACF,GAAG,CAAC;AAEJ,qBAAO,QAAQ,QAAQ,eAAe,MAAM,IAAS;AAAA,YACvD;AAAA,UACF,OAAO;AACL,kBAAM,QAAQ,WAAW,UAAU;AAAA,UACrC;AAAA,QACF;AAAA,MACF,SAAS,KAAK;AACZ,YAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,kBAAQ,MAAM,GAAG;AACjB,kBAAQ;AAAA,YACN;AAAA,UACF;AAAA,QACF;AACA,cAAM,QAAQ,WAAW,UAAU;AAAA,MACrC;AAAA,IACF;AAGA,UAAM,gBAAgB,MAAM,QAAQ,OAAO;AAE3C,QAAI,iBAAiB,WAAW,MAAM;AAEpC,iBAAW,MAAM;AACf,gBAAQ;AAAA,UACN;AAAA,UACA,UAAU;AAAA,YACR,OAAO,MAAM;AAAA,YACb,UAAU,MAAM;AAAA,YAChB,WAAW,MAAM;AAAA,YACjB;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MACF,GAAG,CAAC;AAAA,IACN;AAEA,WAAO,QAAQ,QAAQ,aAAa;AAAA,EACtC;AACF;","names":[]}
|
package/build/legacy/index.cjs
CHANGED
|
@@ -19,9 +19,11 @@ var src_exports = {};
|
|
|
19
19
|
module.exports = __toCommonJS(src_exports);
|
|
20
20
|
__reExport(src_exports, require("./persist.cjs"), module.exports);
|
|
21
21
|
__reExport(src_exports, require("./retryStrategies.cjs"), module.exports);
|
|
22
|
+
__reExport(src_exports, require("./createPersister.cjs"), module.exports);
|
|
22
23
|
// Annotate the CommonJS export names for ESM import in node:
|
|
23
24
|
0 && (module.exports = {
|
|
24
25
|
...require("./persist.cjs"),
|
|
25
|
-
...require("./retryStrategies.cjs")
|
|
26
|
+
...require("./retryStrategies.cjs"),
|
|
27
|
+
...require("./createPersister.cjs")
|
|
26
28
|
});
|
|
27
29
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAEA,wBAAc,0BAFd;AAGA,wBAAc,kCAHd;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\nexport * from './createPersister'\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAEA,wBAAc,0BAFd;AAGA,wBAAc,kCAHd;AAIA,wBAAc,kCAJd;","names":[]}
|
package/build/legacy/index.d.cts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
|
|
2
2
|
export { PersistRetryer, removeOldestQuery } from './retryStrategies.cjs';
|
|
3
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.cjs';
|
|
3
4
|
import '@tanstack/query-core';
|
package/build/legacy/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
|
|
2
2
|
export { PersistRetryer, removeOldestQuery } from './retryStrategies.js';
|
|
3
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.js';
|
|
3
4
|
import '@tanstack/query-core';
|
package/build/legacy/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":";AAEA,cAAc;AACd,cAAc;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\nexport * from './createPersister'\n"],"mappings":";AAEA,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/createPersister.ts
|
|
21
|
+
var createPersister_exports = {};
|
|
22
|
+
__export(createPersister_exports, {
|
|
23
|
+
PERSISTER_KEY_PREFIX: () => PERSISTER_KEY_PREFIX,
|
|
24
|
+
experimental_createPersister: () => experimental_createPersister
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(createPersister_exports);
|
|
27
|
+
var import_query_core = require("@tanstack/query-core");
|
|
28
|
+
var PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
29
|
+
function experimental_createPersister({
|
|
30
|
+
storage,
|
|
31
|
+
buster = "",
|
|
32
|
+
maxAge = 1e3 * 60 * 60 * 24,
|
|
33
|
+
serialize = JSON.stringify,
|
|
34
|
+
deserialize = JSON.parse,
|
|
35
|
+
prefix = PERSISTER_KEY_PREFIX,
|
|
36
|
+
filters
|
|
37
|
+
}) {
|
|
38
|
+
return async function persisterFn(queryFn, context, query) {
|
|
39
|
+
const storageKey = `${prefix}-${query.queryHash}`;
|
|
40
|
+
const matchesFilter = filters ? (0, import_query_core.matchQuery)(filters, query) : true;
|
|
41
|
+
if (matchesFilter && query.state.data === void 0 && storage != null) {
|
|
42
|
+
try {
|
|
43
|
+
const storedData = await storage.getItem(storageKey);
|
|
44
|
+
if (storedData) {
|
|
45
|
+
const persistedQuery = deserialize(storedData);
|
|
46
|
+
if (persistedQuery.state.dataUpdatedAt) {
|
|
47
|
+
const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt;
|
|
48
|
+
const expired = queryAge > maxAge;
|
|
49
|
+
const busted = persistedQuery.buster !== buster;
|
|
50
|
+
if (expired || busted) {
|
|
51
|
+
await storage.removeItem(storageKey);
|
|
52
|
+
} else {
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
query.setState({
|
|
55
|
+
dataUpdatedAt: persistedQuery.state.dataUpdatedAt,
|
|
56
|
+
errorUpdatedAt: persistedQuery.state.errorUpdatedAt
|
|
57
|
+
});
|
|
58
|
+
if (query.isStale()) {
|
|
59
|
+
query.fetch();
|
|
60
|
+
}
|
|
61
|
+
}, 0);
|
|
62
|
+
return Promise.resolve(persistedQuery.state.data);
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
await storage.removeItem(storageKey);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} catch (err) {
|
|
69
|
+
if (process.env.NODE_ENV === "development") {
|
|
70
|
+
console.error(err);
|
|
71
|
+
console.warn(
|
|
72
|
+
"Encountered an error attempting to restore query cache from persisted location."
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
await storage.removeItem(storageKey);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const queryFnResult = await queryFn(context);
|
|
79
|
+
if (matchesFilter && storage != null) {
|
|
80
|
+
setTimeout(() => {
|
|
81
|
+
storage.setItem(
|
|
82
|
+
storageKey,
|
|
83
|
+
serialize({
|
|
84
|
+
state: query.state,
|
|
85
|
+
queryKey: query.queryKey,
|
|
86
|
+
queryHash: query.queryHash,
|
|
87
|
+
buster
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
}, 0);
|
|
91
|
+
}
|
|
92
|
+
return Promise.resolve(queryFnResult);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
+
0 && (module.exports = {
|
|
97
|
+
PERSISTER_KEY_PREFIX,
|
|
98
|
+
experimental_createPersister
|
|
99
|
+
});
|
|
100
|
+
//# sourceMappingURL=createPersister.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/createPersister.ts"],"sourcesContent":["import { matchQuery } from '@tanstack/query-core'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type {\n Query,\n QueryFunctionContext,\n QueryKey,\n QueryState,\n} from '@tanstack/query-core'\n\nexport interface PersistedQuery {\n buster: string\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | undefined | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport interface StoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`.\n */\n storage: AsyncStorage | Storage | undefined | null\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (persistedQuery: PersistedQuery) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedQuery\n /**\n * A unique string that can be used to forcefully invalidate existing caches,\n * if they do not share the same buster string\n */\n buster?: string\n /**\n * The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded\n * @default 24 hours\n */\n maxAge?: number\n /**\n * Prefix to be used for storage key.\n * Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.\n * @default 'tanstack-query'\n */\n prefix?: string\n /**\n * Filters to narrow down which Queries should be persisted.\n */\n filters?: QueryFilters\n}\n\nexport const PERSISTER_KEY_PREFIX = 'tanstack-query'\n\n/**\n * Warning: experimental feature.\n * This utility function enables fine-grained query persistance.\n * Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.\n *\n * ```\n * useQuery({\n queryKey: ['myKey'],\n queryFn: fetcher,\n persister: createPersister({\n storage: localStorage,\n }),\n })\n ```\n */\nexport function experimental_createPersister({\n storage,\n buster = '',\n maxAge = 1000 * 60 * 60 * 24,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n prefix = PERSISTER_KEY_PREFIX,\n filters,\n}: StoragePersisterOptions) {\n return async function persisterFn<T, TQueryKey extends QueryKey>(\n queryFn: (context: QueryFunctionContext<TQueryKey>) => T | Promise<T>,\n context: QueryFunctionContext<TQueryKey>,\n query: Query,\n ) {\n const storageKey = `${prefix}-${query.queryHash}`\n const matchesFilter = filters ? matchQuery(filters, query) : true\n\n // Try to restore only if we do not have any data in the cache and we have persister defined\n if (matchesFilter && query.state.data === undefined && storage != null) {\n try {\n const storedData = await storage.getItem(storageKey)\n if (storedData) {\n const persistedQuery = deserialize(storedData)\n\n if (persistedQuery.state.dataUpdatedAt) {\n const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt\n const expired = queryAge > maxAge\n const busted = persistedQuery.buster !== buster\n if (expired || busted) {\n await storage.removeItem(storageKey)\n } else {\n // Just after restoring we want to get fresh data from the server if it's stale\n setTimeout(() => {\n // Set proper updatedAt, since resolving in the first pass overrides those values\n query.setState({\n dataUpdatedAt: persistedQuery.state.dataUpdatedAt,\n errorUpdatedAt: persistedQuery.state.errorUpdatedAt,\n })\n\n if (query.isStale()) {\n query.fetch()\n }\n }, 0)\n // We must resolve the promise here, as otherwise we will have `loading` state in the app until `queryFn` resolves\n return Promise.resolve(persistedQuery.state.data as T)\n }\n } else {\n await storage.removeItem(storageKey)\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV === 'development') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore query cache from persisted location.',\n )\n }\n await storage.removeItem(storageKey)\n }\n }\n\n // If we did not restore, or restoration failed - fetch\n const queryFnResult = await queryFn(context)\n\n if (matchesFilter && storage != null) {\n // Persist if we have storage defined, we use timeout to get proper state to be persisted\n setTimeout(() => {\n storage.setItem(\n storageKey,\n serialize({\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n buster: buster,\n }),\n )\n }, 0)\n }\n\n return Promise.resolve(queryFnResult)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAA2B;AA6DpB,IAAM,uBAAuB;AAiB7B,SAAS,6BAA6B;AAAA,EAC3C;AAAA,EACA,SAAS;AAAA,EACT,SAAS,MAAO,KAAK,KAAK;AAAA,EAC1B,YAAY,KAAK;AAAA,EACjB,cAAc,KAAK;AAAA,EACnB,SAAS;AAAA,EACT;AACF,GAA4B;AAC1B,SAAO,eAAe,YACpB,SACA,SACA,OACA;AACA,UAAM,aAAa,GAAG,MAAM,IAAI,MAAM,SAAS;AAC/C,UAAM,gBAAgB,cAAU,8BAAW,SAAS,KAAK,IAAI;AAG7D,QAAI,iBAAiB,MAAM,MAAM,SAAS,UAAa,WAAW,MAAM;AACtE,UAAI;AACF,cAAM,aAAa,MAAM,QAAQ,QAAQ,UAAU;AACnD,YAAI,YAAY;AACd,gBAAM,iBAAiB,YAAY,UAAU;AAE7C,cAAI,eAAe,MAAM,eAAe;AACtC,kBAAM,WAAW,KAAK,IAAI,IAAI,eAAe,MAAM;AACnD,kBAAM,UAAU,WAAW;AAC3B,kBAAM,SAAS,eAAe,WAAW;AACzC,gBAAI,WAAW,QAAQ;AACrB,oBAAM,QAAQ,WAAW,UAAU;AAAA,YACrC,OAAO;AAEL,yBAAW,MAAM;AAEf,sBAAM,SAAS;AAAA,kBACb,eAAe,eAAe,MAAM;AAAA,kBACpC,gBAAgB,eAAe,MAAM;AAAA,gBACvC,CAAC;AAED,oBAAI,MAAM,QAAQ,GAAG;AACnB,wBAAM,MAAM;AAAA,gBACd;AAAA,cACF,GAAG,CAAC;AAEJ,qBAAO,QAAQ,QAAQ,eAAe,MAAM,IAAS;AAAA,YACvD;AAAA,UACF,OAAO;AACL,kBAAM,QAAQ,WAAW,UAAU;AAAA,UACrC;AAAA,QACF;AAAA,MACF,SAAS,KAAK;AACZ,YAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,kBAAQ,MAAM,GAAG;AACjB,kBAAQ;AAAA,YACN;AAAA,UACF;AAAA,QACF;AACA,cAAM,QAAQ,WAAW,UAAU;AAAA,MACrC;AAAA,IACF;AAGA,UAAM,gBAAgB,MAAM,QAAQ,OAAO;AAE3C,QAAI,iBAAiB,WAAW,MAAM;AAEpC,iBAAW,MAAM;AACf,gBAAQ;AAAA,UACN;AAAA,UACA,UAAU;AAAA,YACR,OAAO,MAAM;AAAA,YACb,UAAU,MAAM;AAAA,YAChB,WAAW,MAAM;AAAA,YACjB;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MACF,GAAG,CAAC;AAAA,IACN;AAEA,WAAO,QAAQ,QAAQ,aAAa;AAAA,EACtC;AACF;","names":[]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { QueryKey, QueryState, QueryFilters, Query } from '@tanstack/query-core';
|
|
2
|
+
|
|
3
|
+
interface PersistedQuery {
|
|
4
|
+
buster: string;
|
|
5
|
+
queryHash: string;
|
|
6
|
+
queryKey: QueryKey;
|
|
7
|
+
state: QueryState;
|
|
8
|
+
}
|
|
9
|
+
interface AsyncStorage {
|
|
10
|
+
getItem: (key: string) => Promise<string | undefined | null>;
|
|
11
|
+
setItem: (key: string, value: string) => Promise<unknown>;
|
|
12
|
+
removeItem: (key: string) => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
interface StoragePersisterOptions {
|
|
15
|
+
/** The storage client used for setting and retrieving items from cache.
|
|
16
|
+
* For SSR pass in `undefined`.
|
|
17
|
+
*/
|
|
18
|
+
storage: AsyncStorage | Storage | undefined | null;
|
|
19
|
+
/**
|
|
20
|
+
* How to serialize the data to storage.
|
|
21
|
+
* @default `JSON.stringify`
|
|
22
|
+
*/
|
|
23
|
+
serialize?: (persistedQuery: PersistedQuery) => string;
|
|
24
|
+
/**
|
|
25
|
+
* How to deserialize the data from storage.
|
|
26
|
+
* @default `JSON.parse`
|
|
27
|
+
*/
|
|
28
|
+
deserialize?: (cachedString: string) => PersistedQuery;
|
|
29
|
+
/**
|
|
30
|
+
* A unique string that can be used to forcefully invalidate existing caches,
|
|
31
|
+
* if they do not share the same buster string
|
|
32
|
+
*/
|
|
33
|
+
buster?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The max-allowed age of the cache in milliseconds.
|
|
36
|
+
* If a persisted cache is found that is older than this
|
|
37
|
+
* time, it will be discarded
|
|
38
|
+
* @default 24 hours
|
|
39
|
+
*/
|
|
40
|
+
maxAge?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Prefix to be used for storage key.
|
|
43
|
+
* Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.
|
|
44
|
+
* @default 'tanstack-query'
|
|
45
|
+
*/
|
|
46
|
+
prefix?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Filters to narrow down which Queries should be persisted.
|
|
49
|
+
*/
|
|
50
|
+
filters?: QueryFilters;
|
|
51
|
+
}
|
|
52
|
+
declare const PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
53
|
+
/**
|
|
54
|
+
* Warning: experimental feature.
|
|
55
|
+
* This utility function enables fine-grained query persistance.
|
|
56
|
+
* Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.
|
|
57
|
+
*
|
|
58
|
+
* ```
|
|
59
|
+
* useQuery({
|
|
60
|
+
queryKey: ['myKey'],
|
|
61
|
+
queryFn: fetcher,
|
|
62
|
+
persister: createPersister({
|
|
63
|
+
storage: localStorage,
|
|
64
|
+
}),
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
*/
|
|
68
|
+
declare function experimental_createPersister({ storage, buster, maxAge, serialize, deserialize, prefix, filters, }: StoragePersisterOptions): <T, TQueryKey extends QueryKey>(queryFn: (context: {
|
|
69
|
+
queryKey: TQueryKey;
|
|
70
|
+
signal: AbortSignal;
|
|
71
|
+
meta: Record<string, unknown> | undefined;
|
|
72
|
+
}) => T | Promise<T>, context: {
|
|
73
|
+
queryKey: TQueryKey;
|
|
74
|
+
signal: AbortSignal;
|
|
75
|
+
meta: Record<string, unknown> | undefined;
|
|
76
|
+
}, query: Query) => Promise<T>;
|
|
77
|
+
|
|
78
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { QueryKey, QueryState, QueryFilters, Query } from '@tanstack/query-core';
|
|
2
|
+
|
|
3
|
+
interface PersistedQuery {
|
|
4
|
+
buster: string;
|
|
5
|
+
queryHash: string;
|
|
6
|
+
queryKey: QueryKey;
|
|
7
|
+
state: QueryState;
|
|
8
|
+
}
|
|
9
|
+
interface AsyncStorage {
|
|
10
|
+
getItem: (key: string) => Promise<string | undefined | null>;
|
|
11
|
+
setItem: (key: string, value: string) => Promise<unknown>;
|
|
12
|
+
removeItem: (key: string) => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
interface StoragePersisterOptions {
|
|
15
|
+
/** The storage client used for setting and retrieving items from cache.
|
|
16
|
+
* For SSR pass in `undefined`.
|
|
17
|
+
*/
|
|
18
|
+
storage: AsyncStorage | Storage | undefined | null;
|
|
19
|
+
/**
|
|
20
|
+
* How to serialize the data to storage.
|
|
21
|
+
* @default `JSON.stringify`
|
|
22
|
+
*/
|
|
23
|
+
serialize?: (persistedQuery: PersistedQuery) => string;
|
|
24
|
+
/**
|
|
25
|
+
* How to deserialize the data from storage.
|
|
26
|
+
* @default `JSON.parse`
|
|
27
|
+
*/
|
|
28
|
+
deserialize?: (cachedString: string) => PersistedQuery;
|
|
29
|
+
/**
|
|
30
|
+
* A unique string that can be used to forcefully invalidate existing caches,
|
|
31
|
+
* if they do not share the same buster string
|
|
32
|
+
*/
|
|
33
|
+
buster?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The max-allowed age of the cache in milliseconds.
|
|
36
|
+
* If a persisted cache is found that is older than this
|
|
37
|
+
* time, it will be discarded
|
|
38
|
+
* @default 24 hours
|
|
39
|
+
*/
|
|
40
|
+
maxAge?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Prefix to be used for storage key.
|
|
43
|
+
* Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.
|
|
44
|
+
* @default 'tanstack-query'
|
|
45
|
+
*/
|
|
46
|
+
prefix?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Filters to narrow down which Queries should be persisted.
|
|
49
|
+
*/
|
|
50
|
+
filters?: QueryFilters;
|
|
51
|
+
}
|
|
52
|
+
declare const PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
53
|
+
/**
|
|
54
|
+
* Warning: experimental feature.
|
|
55
|
+
* This utility function enables fine-grained query persistance.
|
|
56
|
+
* Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.
|
|
57
|
+
*
|
|
58
|
+
* ```
|
|
59
|
+
* useQuery({
|
|
60
|
+
queryKey: ['myKey'],
|
|
61
|
+
queryFn: fetcher,
|
|
62
|
+
persister: createPersister({
|
|
63
|
+
storage: localStorage,
|
|
64
|
+
}),
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
*/
|
|
68
|
+
declare function experimental_createPersister({ storage, buster, maxAge, serialize, deserialize, prefix, filters, }: StoragePersisterOptions): <T, TQueryKey extends QueryKey>(queryFn: (context: {
|
|
69
|
+
queryKey: TQueryKey;
|
|
70
|
+
signal: AbortSignal;
|
|
71
|
+
meta: Record<string, unknown> | undefined;
|
|
72
|
+
}) => T | Promise<T>, context: {
|
|
73
|
+
queryKey: TQueryKey;
|
|
74
|
+
signal: AbortSignal;
|
|
75
|
+
meta: Record<string, unknown> | undefined;
|
|
76
|
+
}, query: Query) => Promise<T>;
|
|
77
|
+
|
|
78
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/createPersister.ts
|
|
2
|
+
import { matchQuery } from "@tanstack/query-core";
|
|
3
|
+
var PERSISTER_KEY_PREFIX = "tanstack-query";
|
|
4
|
+
function experimental_createPersister({
|
|
5
|
+
storage,
|
|
6
|
+
buster = "",
|
|
7
|
+
maxAge = 1e3 * 60 * 60 * 24,
|
|
8
|
+
serialize = JSON.stringify,
|
|
9
|
+
deserialize = JSON.parse,
|
|
10
|
+
prefix = PERSISTER_KEY_PREFIX,
|
|
11
|
+
filters
|
|
12
|
+
}) {
|
|
13
|
+
return async function persisterFn(queryFn, context, query) {
|
|
14
|
+
const storageKey = `${prefix}-${query.queryHash}`;
|
|
15
|
+
const matchesFilter = filters ? matchQuery(filters, query) : true;
|
|
16
|
+
if (matchesFilter && query.state.data === void 0 && storage != null) {
|
|
17
|
+
try {
|
|
18
|
+
const storedData = await storage.getItem(storageKey);
|
|
19
|
+
if (storedData) {
|
|
20
|
+
const persistedQuery = deserialize(storedData);
|
|
21
|
+
if (persistedQuery.state.dataUpdatedAt) {
|
|
22
|
+
const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt;
|
|
23
|
+
const expired = queryAge > maxAge;
|
|
24
|
+
const busted = persistedQuery.buster !== buster;
|
|
25
|
+
if (expired || busted) {
|
|
26
|
+
await storage.removeItem(storageKey);
|
|
27
|
+
} else {
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
query.setState({
|
|
30
|
+
dataUpdatedAt: persistedQuery.state.dataUpdatedAt,
|
|
31
|
+
errorUpdatedAt: persistedQuery.state.errorUpdatedAt
|
|
32
|
+
});
|
|
33
|
+
if (query.isStale()) {
|
|
34
|
+
query.fetch();
|
|
35
|
+
}
|
|
36
|
+
}, 0);
|
|
37
|
+
return Promise.resolve(persistedQuery.state.data);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
await storage.removeItem(storageKey);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} catch (err) {
|
|
44
|
+
if (process.env.NODE_ENV === "development") {
|
|
45
|
+
console.error(err);
|
|
46
|
+
console.warn(
|
|
47
|
+
"Encountered an error attempting to restore query cache from persisted location."
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
await storage.removeItem(storageKey);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const queryFnResult = await queryFn(context);
|
|
54
|
+
if (matchesFilter && storage != null) {
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
storage.setItem(
|
|
57
|
+
storageKey,
|
|
58
|
+
serialize({
|
|
59
|
+
state: query.state,
|
|
60
|
+
queryKey: query.queryKey,
|
|
61
|
+
queryHash: query.queryHash,
|
|
62
|
+
buster
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
}, 0);
|
|
66
|
+
}
|
|
67
|
+
return Promise.resolve(queryFnResult);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
PERSISTER_KEY_PREFIX,
|
|
72
|
+
experimental_createPersister
|
|
73
|
+
};
|
|
74
|
+
//# sourceMappingURL=createPersister.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/createPersister.ts"],"sourcesContent":["import { matchQuery } from '@tanstack/query-core'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type {\n Query,\n QueryFunctionContext,\n QueryKey,\n QueryState,\n} from '@tanstack/query-core'\n\nexport interface PersistedQuery {\n buster: string\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface AsyncStorage {\n getItem: (key: string) => Promise<string | undefined | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport interface StoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`.\n */\n storage: AsyncStorage | Storage | undefined | null\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (persistedQuery: PersistedQuery) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedQuery\n /**\n * A unique string that can be used to forcefully invalidate existing caches,\n * if they do not share the same buster string\n */\n buster?: string\n /**\n * The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded\n * @default 24 hours\n */\n maxAge?: number\n /**\n * Prefix to be used for storage key.\n * Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.\n * @default 'tanstack-query'\n */\n prefix?: string\n /**\n * Filters to narrow down which Queries should be persisted.\n */\n filters?: QueryFilters\n}\n\nexport const PERSISTER_KEY_PREFIX = 'tanstack-query'\n\n/**\n * Warning: experimental feature.\n * This utility function enables fine-grained query persistance.\n * Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.\n *\n * ```\n * useQuery({\n queryKey: ['myKey'],\n queryFn: fetcher,\n persister: createPersister({\n storage: localStorage,\n }),\n })\n ```\n */\nexport function experimental_createPersister({\n storage,\n buster = '',\n maxAge = 1000 * 60 * 60 * 24,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n prefix = PERSISTER_KEY_PREFIX,\n filters,\n}: StoragePersisterOptions) {\n return async function persisterFn<T, TQueryKey extends QueryKey>(\n queryFn: (context: QueryFunctionContext<TQueryKey>) => T | Promise<T>,\n context: QueryFunctionContext<TQueryKey>,\n query: Query,\n ) {\n const storageKey = `${prefix}-${query.queryHash}`\n const matchesFilter = filters ? matchQuery(filters, query) : true\n\n // Try to restore only if we do not have any data in the cache and we have persister defined\n if (matchesFilter && query.state.data === undefined && storage != null) {\n try {\n const storedData = await storage.getItem(storageKey)\n if (storedData) {\n const persistedQuery = deserialize(storedData)\n\n if (persistedQuery.state.dataUpdatedAt) {\n const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt\n const expired = queryAge > maxAge\n const busted = persistedQuery.buster !== buster\n if (expired || busted) {\n await storage.removeItem(storageKey)\n } else {\n // Just after restoring we want to get fresh data from the server if it's stale\n setTimeout(() => {\n // Set proper updatedAt, since resolving in the first pass overrides those values\n query.setState({\n dataUpdatedAt: persistedQuery.state.dataUpdatedAt,\n errorUpdatedAt: persistedQuery.state.errorUpdatedAt,\n })\n\n if (query.isStale()) {\n query.fetch()\n }\n }, 0)\n // We must resolve the promise here, as otherwise we will have `loading` state in the app until `queryFn` resolves\n return Promise.resolve(persistedQuery.state.data as T)\n }\n } else {\n await storage.removeItem(storageKey)\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV === 'development') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore query cache from persisted location.',\n )\n }\n await storage.removeItem(storageKey)\n }\n }\n\n // If we did not restore, or restoration failed - fetch\n const queryFnResult = await queryFn(context)\n\n if (matchesFilter && storage != null) {\n // Persist if we have storage defined, we use timeout to get proper state to be persisted\n setTimeout(() => {\n storage.setItem(\n storageKey,\n serialize({\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n buster: buster,\n }),\n )\n }, 0)\n }\n\n return Promise.resolve(queryFnResult)\n }\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AA6DpB,IAAM,uBAAuB;AAiB7B,SAAS,6BAA6B;AAAA,EAC3C;AAAA,EACA,SAAS;AAAA,EACT,SAAS,MAAO,KAAK,KAAK;AAAA,EAC1B,YAAY,KAAK;AAAA,EACjB,cAAc,KAAK;AAAA,EACnB,SAAS;AAAA,EACT;AACF,GAA4B;AAC1B,SAAO,eAAe,YACpB,SACA,SACA,OACA;AACA,UAAM,aAAa,GAAG,MAAM,IAAI,MAAM,SAAS;AAC/C,UAAM,gBAAgB,UAAU,WAAW,SAAS,KAAK,IAAI;AAG7D,QAAI,iBAAiB,MAAM,MAAM,SAAS,UAAa,WAAW,MAAM;AACtE,UAAI;AACF,cAAM,aAAa,MAAM,QAAQ,QAAQ,UAAU;AACnD,YAAI,YAAY;AACd,gBAAM,iBAAiB,YAAY,UAAU;AAE7C,cAAI,eAAe,MAAM,eAAe;AACtC,kBAAM,WAAW,KAAK,IAAI,IAAI,eAAe,MAAM;AACnD,kBAAM,UAAU,WAAW;AAC3B,kBAAM,SAAS,eAAe,WAAW;AACzC,gBAAI,WAAW,QAAQ;AACrB,oBAAM,QAAQ,WAAW,UAAU;AAAA,YACrC,OAAO;AAEL,yBAAW,MAAM;AAEf,sBAAM,SAAS;AAAA,kBACb,eAAe,eAAe,MAAM;AAAA,kBACpC,gBAAgB,eAAe,MAAM;AAAA,gBACvC,CAAC;AAED,oBAAI,MAAM,QAAQ,GAAG;AACnB,wBAAM,MAAM;AAAA,gBACd;AAAA,cACF,GAAG,CAAC;AAEJ,qBAAO,QAAQ,QAAQ,eAAe,MAAM,IAAS;AAAA,YACvD;AAAA,UACF,OAAO;AACL,kBAAM,QAAQ,WAAW,UAAU;AAAA,UACrC;AAAA,QACF;AAAA,MACF,SAAS,KAAK;AACZ,YAAI,QAAQ,IAAI,aAAa,eAAe;AAC1C,kBAAQ,MAAM,GAAG;AACjB,kBAAQ;AAAA,YACN;AAAA,UACF;AAAA,QACF;AACA,cAAM,QAAQ,WAAW,UAAU;AAAA,MACrC;AAAA,IACF;AAGA,UAAM,gBAAgB,MAAM,QAAQ,OAAO;AAE3C,QAAI,iBAAiB,WAAW,MAAM;AAEpC,iBAAW,MAAM;AACf,gBAAQ;AAAA,UACN;AAAA,UACA,UAAU;AAAA,YACR,OAAO,MAAM;AAAA,YACb,UAAU,MAAM;AAAA,YAChB,WAAW,MAAM;AAAA,YACjB;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MACF,GAAG,CAAC;AAAA,IACN;AAEA,WAAO,QAAQ,QAAQ,aAAa;AAAA,EACtC;AACF;","names":[]}
|
package/build/modern/index.cjs
CHANGED
|
@@ -19,9 +19,11 @@ var src_exports = {};
|
|
|
19
19
|
module.exports = __toCommonJS(src_exports);
|
|
20
20
|
__reExport(src_exports, require("./persist.cjs"), module.exports);
|
|
21
21
|
__reExport(src_exports, require("./retryStrategies.cjs"), module.exports);
|
|
22
|
+
__reExport(src_exports, require("./createPersister.cjs"), module.exports);
|
|
22
23
|
// Annotate the CommonJS export names for ESM import in node:
|
|
23
24
|
0 && (module.exports = {
|
|
24
25
|
...require("./persist.cjs"),
|
|
25
|
-
...require("./retryStrategies.cjs")
|
|
26
|
+
...require("./retryStrategies.cjs"),
|
|
27
|
+
...require("./createPersister.cjs")
|
|
26
28
|
});
|
|
27
29
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAEA,wBAAc,0BAFd;AAGA,wBAAc,kCAHd;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\nexport * from './createPersister'\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAEA,wBAAc,0BAFd;AAGA,wBAAc,kCAHd;AAIA,wBAAc,kCAJd;","names":[]}
|
package/build/modern/index.d.cts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
|
|
2
2
|
export { PersistRetryer, removeOldestQuery } from './retryStrategies.cjs';
|
|
3
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.cjs';
|
|
3
4
|
import '@tanstack/query-core';
|
package/build/modern/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
|
|
2
2
|
export { PersistRetryer, removeOldestQuery } from './retryStrategies.js';
|
|
3
|
+
export { AsyncStorage, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.js';
|
|
3
4
|
import '@tanstack/query-core';
|
package/build/modern/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":";AAEA,cAAc;AACd,cAAc;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\nexport * from './createPersister'\n"],"mappings":";AAEA,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-persist-client-core",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.28",
|
|
4
4
|
"description": "Set of utilities for interacting with persisters, which can save your queryClient for later use",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"src"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@tanstack/query-core": "5.0.0-beta.
|
|
40
|
+
"@tanstack/query-core": "5.0.0-beta.28"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"clean": "rimraf ./build && rimraf ./coverage",
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { Query, QueryCache, hashKey } from '@tanstack/query-core'
|
|
2
|
+
import { vi } from 'vitest'
|
|
3
|
+
import {
|
|
4
|
+
PERSISTER_KEY_PREFIX,
|
|
5
|
+
experimental_createPersister,
|
|
6
|
+
} from '../createPersister'
|
|
7
|
+
import { sleep } from './utils'
|
|
8
|
+
import type { StoragePersisterOptions } from '../createPersister'
|
|
9
|
+
import type { QueryKey } from '@tanstack/query-core'
|
|
10
|
+
|
|
11
|
+
function getFreshStorage() {
|
|
12
|
+
const storage = new Map()
|
|
13
|
+
return {
|
|
14
|
+
getItem: (key: string) => Promise.resolve(storage.get(key)),
|
|
15
|
+
setItem: async (key: string, value: unknown) => {
|
|
16
|
+
storage.set(key, value)
|
|
17
|
+
},
|
|
18
|
+
removeItem: async (key: string) => {
|
|
19
|
+
storage.delete(key)
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function setupPersister(
|
|
25
|
+
queryKey: QueryKey,
|
|
26
|
+
persisterOptions: StoragePersisterOptions,
|
|
27
|
+
) {
|
|
28
|
+
const context = {
|
|
29
|
+
meta: { foo: 'bar' },
|
|
30
|
+
queryKey,
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
signal: undefined as AbortSignal,
|
|
33
|
+
}
|
|
34
|
+
const queryHash = hashKey(queryKey)
|
|
35
|
+
const storageKey = `${PERSISTER_KEY_PREFIX}-${queryHash}`
|
|
36
|
+
|
|
37
|
+
const queryFn = vi.fn()
|
|
38
|
+
|
|
39
|
+
const persisterFn = experimental_createPersister(persisterOptions)
|
|
40
|
+
|
|
41
|
+
const query = new Query({
|
|
42
|
+
cache: new QueryCache(),
|
|
43
|
+
queryHash,
|
|
44
|
+
queryKey,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
context,
|
|
49
|
+
persisterFn,
|
|
50
|
+
query,
|
|
51
|
+
queryFn,
|
|
52
|
+
queryHash,
|
|
53
|
+
queryKey,
|
|
54
|
+
storageKey,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe('createPersister', () => {
|
|
59
|
+
test('should fetch if storage is not provided', async () => {
|
|
60
|
+
const { context, persisterFn, query, queryFn } = setupPersister(['foo'], {
|
|
61
|
+
storage: undefined,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
await persisterFn(queryFn, context, query)
|
|
65
|
+
|
|
66
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
67
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('should fetch if there is no stored data', async () => {
|
|
71
|
+
const storage = getFreshStorage()
|
|
72
|
+
const { context, persisterFn, query, queryFn } = setupPersister(['foo'], {
|
|
73
|
+
storage,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
await persisterFn(queryFn, context, query)
|
|
77
|
+
|
|
78
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
79
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('should fetch if query already has data', async () => {
|
|
83
|
+
const storage = getFreshStorage()
|
|
84
|
+
const { context, persisterFn, query, queryFn } = setupPersister(['foo'], {
|
|
85
|
+
storage,
|
|
86
|
+
})
|
|
87
|
+
query.state.data = 'baz'
|
|
88
|
+
|
|
89
|
+
await persisterFn(queryFn, context, query)
|
|
90
|
+
|
|
91
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
92
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('should fetch if deserialization fails', async () => {
|
|
96
|
+
const storage = getFreshStorage()
|
|
97
|
+
const { context, persisterFn, query, queryFn, storageKey } = setupPersister(
|
|
98
|
+
['foo'],
|
|
99
|
+
{
|
|
100
|
+
storage,
|
|
101
|
+
},
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
await storage.setItem(storageKey, '{invalid[item')
|
|
105
|
+
|
|
106
|
+
await persisterFn(queryFn, context, query)
|
|
107
|
+
|
|
108
|
+
expect(await storage.getItem(storageKey)).toBeUndefined()
|
|
109
|
+
|
|
110
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
111
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('should remove stored item if `dataUpdatedAt` is empty', async () => {
|
|
115
|
+
const storage = getFreshStorage()
|
|
116
|
+
const { context, persisterFn, query, queryFn, storageKey } = setupPersister(
|
|
117
|
+
['foo'],
|
|
118
|
+
{
|
|
119
|
+
storage,
|
|
120
|
+
},
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
await storage.setItem(
|
|
124
|
+
storageKey,
|
|
125
|
+
JSON.stringify({
|
|
126
|
+
buster: '',
|
|
127
|
+
state: { dataUpdatedAt: undefined },
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
await persisterFn(queryFn, context, query)
|
|
132
|
+
|
|
133
|
+
expect(await storage.getItem(storageKey)).toBeUndefined()
|
|
134
|
+
|
|
135
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
136
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
test('should remove stored item if its expired', async () => {
|
|
140
|
+
const storage = getFreshStorage()
|
|
141
|
+
const { context, persisterFn, query, queryFn, storageKey } = setupPersister(
|
|
142
|
+
['foo'],
|
|
143
|
+
{
|
|
144
|
+
storage,
|
|
145
|
+
maxAge: 100,
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
await storage.setItem(
|
|
150
|
+
storageKey,
|
|
151
|
+
JSON.stringify({
|
|
152
|
+
buster: '',
|
|
153
|
+
state: { dataUpdatedAt: Date.now() - 200 },
|
|
154
|
+
}),
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
await persisterFn(queryFn, context, query)
|
|
158
|
+
|
|
159
|
+
expect(await storage.getItem(storageKey)).toBeUndefined()
|
|
160
|
+
|
|
161
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
162
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('should remove stored item if its busted', async () => {
|
|
166
|
+
const storage = getFreshStorage()
|
|
167
|
+
const { context, persisterFn, query, queryFn, storageKey } = setupPersister(
|
|
168
|
+
['foo'],
|
|
169
|
+
{
|
|
170
|
+
storage,
|
|
171
|
+
},
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
await storage.setItem(
|
|
175
|
+
storageKey,
|
|
176
|
+
JSON.stringify({
|
|
177
|
+
buster: 'bust',
|
|
178
|
+
state: { dataUpdatedAt: Date.now() },
|
|
179
|
+
}),
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
await persisterFn(queryFn, context, query)
|
|
183
|
+
|
|
184
|
+
expect(await storage.getItem(storageKey)).toBeUndefined()
|
|
185
|
+
|
|
186
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
187
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('should restore item from the storage and set proper `updatedAt` values', async () => {
|
|
191
|
+
const storage = getFreshStorage()
|
|
192
|
+
const { context, persisterFn, query, queryFn, storageKey } = setupPersister(
|
|
193
|
+
['foo'],
|
|
194
|
+
{
|
|
195
|
+
storage,
|
|
196
|
+
},
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
const dataUpdatedAt = Date.now()
|
|
200
|
+
|
|
201
|
+
await storage.setItem(
|
|
202
|
+
storageKey,
|
|
203
|
+
JSON.stringify({
|
|
204
|
+
buster: '',
|
|
205
|
+
state: { dataUpdatedAt },
|
|
206
|
+
}),
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
await persisterFn(queryFn, context, query)
|
|
210
|
+
query.state.dataUpdatedAt = 0
|
|
211
|
+
query.fetch = vi.fn()
|
|
212
|
+
expect(query.state.dataUpdatedAt).toEqual(0)
|
|
213
|
+
|
|
214
|
+
await sleep(0)
|
|
215
|
+
|
|
216
|
+
expect(queryFn).toHaveBeenCalledTimes(0)
|
|
217
|
+
expect(query.fetch).toHaveBeenCalledTimes(0)
|
|
218
|
+
expect(query.state.dataUpdatedAt).toEqual(dataUpdatedAt)
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('should restore item from the storage and refetch when `stale`', async () => {
|
|
222
|
+
const storage = getFreshStorage()
|
|
223
|
+
const { context, persisterFn, query, queryFn, storageKey } = setupPersister(
|
|
224
|
+
['foo'],
|
|
225
|
+
{
|
|
226
|
+
storage,
|
|
227
|
+
},
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
await storage.setItem(
|
|
231
|
+
storageKey,
|
|
232
|
+
JSON.stringify({
|
|
233
|
+
buster: '',
|
|
234
|
+
state: { dataUpdatedAt: Date.now() },
|
|
235
|
+
}),
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
await persisterFn(queryFn, context, query)
|
|
239
|
+
query.state.isInvalidated = true
|
|
240
|
+
query.fetch = vi.fn()
|
|
241
|
+
|
|
242
|
+
await sleep(0)
|
|
243
|
+
|
|
244
|
+
expect(queryFn).toHaveBeenCalledTimes(0)
|
|
245
|
+
expect(query.fetch).toHaveBeenCalledTimes(1)
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test('should store item after successfull fetch', async () => {
|
|
249
|
+
const storage = getFreshStorage()
|
|
250
|
+
const {
|
|
251
|
+
context,
|
|
252
|
+
persisterFn,
|
|
253
|
+
query,
|
|
254
|
+
queryFn,
|
|
255
|
+
queryHash,
|
|
256
|
+
queryKey,
|
|
257
|
+
storageKey,
|
|
258
|
+
} = setupPersister(['foo'], {
|
|
259
|
+
storage,
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
await persisterFn(queryFn, context, query)
|
|
263
|
+
query.setData('baz')
|
|
264
|
+
|
|
265
|
+
await sleep(0)
|
|
266
|
+
|
|
267
|
+
expect(queryFn).toHaveBeenCalledOnce()
|
|
268
|
+
expect(queryFn).toHaveBeenCalledWith(context)
|
|
269
|
+
|
|
270
|
+
expect(JSON.parse(await storage.getItem(storageKey))).toMatchObject({
|
|
271
|
+
buster: '',
|
|
272
|
+
queryHash,
|
|
273
|
+
queryKey,
|
|
274
|
+
state: {
|
|
275
|
+
data: 'baz',
|
|
276
|
+
},
|
|
277
|
+
})
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
test('should skip stored item if not matched by filters', async () => {
|
|
281
|
+
const storage = getFreshStorage()
|
|
282
|
+
const { context, persisterFn, query, queryFn, storageKey } = setupPersister(
|
|
283
|
+
['foo'],
|
|
284
|
+
{
|
|
285
|
+
storage,
|
|
286
|
+
filters: {
|
|
287
|
+
predicate: () => {
|
|
288
|
+
return false
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
const dataUpdatedAt = Date.now()
|
|
295
|
+
|
|
296
|
+
await storage.setItem(
|
|
297
|
+
storageKey,
|
|
298
|
+
JSON.stringify({
|
|
299
|
+
buster: '',
|
|
300
|
+
state: { dataUpdatedAt },
|
|
301
|
+
}),
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
await persisterFn(queryFn, context, query)
|
|
305
|
+
query.fetch = vi.fn()
|
|
306
|
+
|
|
307
|
+
await sleep(0)
|
|
308
|
+
|
|
309
|
+
expect(queryFn).toHaveBeenCalledTimes(1)
|
|
310
|
+
expect(query.fetch).toHaveBeenCalledTimes(0)
|
|
311
|
+
})
|
|
312
|
+
})
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { matchQuery } from '@tanstack/query-core'
|
|
2
|
+
import type { QueryFilters } from '@tanstack/query-core'
|
|
3
|
+
import type {
|
|
4
|
+
Query,
|
|
5
|
+
QueryFunctionContext,
|
|
6
|
+
QueryKey,
|
|
7
|
+
QueryState,
|
|
8
|
+
} from '@tanstack/query-core'
|
|
9
|
+
|
|
10
|
+
export interface PersistedQuery {
|
|
11
|
+
buster: string
|
|
12
|
+
queryHash: string
|
|
13
|
+
queryKey: QueryKey
|
|
14
|
+
state: QueryState
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface AsyncStorage {
|
|
18
|
+
getItem: (key: string) => Promise<string | undefined | null>
|
|
19
|
+
setItem: (key: string, value: string) => Promise<unknown>
|
|
20
|
+
removeItem: (key: string) => Promise<void>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface StoragePersisterOptions {
|
|
24
|
+
/** The storage client used for setting and retrieving items from cache.
|
|
25
|
+
* For SSR pass in `undefined`.
|
|
26
|
+
*/
|
|
27
|
+
storage: AsyncStorage | Storage | undefined | null
|
|
28
|
+
/**
|
|
29
|
+
* How to serialize the data to storage.
|
|
30
|
+
* @default `JSON.stringify`
|
|
31
|
+
*/
|
|
32
|
+
serialize?: (persistedQuery: PersistedQuery) => string
|
|
33
|
+
/**
|
|
34
|
+
* How to deserialize the data from storage.
|
|
35
|
+
* @default `JSON.parse`
|
|
36
|
+
*/
|
|
37
|
+
deserialize?: (cachedString: string) => PersistedQuery
|
|
38
|
+
/**
|
|
39
|
+
* A unique string that can be used to forcefully invalidate existing caches,
|
|
40
|
+
* if they do not share the same buster string
|
|
41
|
+
*/
|
|
42
|
+
buster?: string
|
|
43
|
+
/**
|
|
44
|
+
* The max-allowed age of the cache in milliseconds.
|
|
45
|
+
* If a persisted cache is found that is older than this
|
|
46
|
+
* time, it will be discarded
|
|
47
|
+
* @default 24 hours
|
|
48
|
+
*/
|
|
49
|
+
maxAge?: number
|
|
50
|
+
/**
|
|
51
|
+
* Prefix to be used for storage key.
|
|
52
|
+
* Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.
|
|
53
|
+
* @default 'tanstack-query'
|
|
54
|
+
*/
|
|
55
|
+
prefix?: string
|
|
56
|
+
/**
|
|
57
|
+
* Filters to narrow down which Queries should be persisted.
|
|
58
|
+
*/
|
|
59
|
+
filters?: QueryFilters
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const PERSISTER_KEY_PREFIX = 'tanstack-query'
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Warning: experimental feature.
|
|
66
|
+
* This utility function enables fine-grained query persistance.
|
|
67
|
+
* Simple add it as a `persister` parameter to `useQuery` or `defaultOptions` on `queryClient`.
|
|
68
|
+
*
|
|
69
|
+
* ```
|
|
70
|
+
* useQuery({
|
|
71
|
+
queryKey: ['myKey'],
|
|
72
|
+
queryFn: fetcher,
|
|
73
|
+
persister: createPersister({
|
|
74
|
+
storage: localStorage,
|
|
75
|
+
}),
|
|
76
|
+
})
|
|
77
|
+
```
|
|
78
|
+
*/
|
|
79
|
+
export function experimental_createPersister({
|
|
80
|
+
storage,
|
|
81
|
+
buster = '',
|
|
82
|
+
maxAge = 1000 * 60 * 60 * 24,
|
|
83
|
+
serialize = JSON.stringify,
|
|
84
|
+
deserialize = JSON.parse,
|
|
85
|
+
prefix = PERSISTER_KEY_PREFIX,
|
|
86
|
+
filters,
|
|
87
|
+
}: StoragePersisterOptions) {
|
|
88
|
+
return async function persisterFn<T, TQueryKey extends QueryKey>(
|
|
89
|
+
queryFn: (context: QueryFunctionContext<TQueryKey>) => T | Promise<T>,
|
|
90
|
+
context: QueryFunctionContext<TQueryKey>,
|
|
91
|
+
query: Query,
|
|
92
|
+
) {
|
|
93
|
+
const storageKey = `${prefix}-${query.queryHash}`
|
|
94
|
+
const matchesFilter = filters ? matchQuery(filters, query) : true
|
|
95
|
+
|
|
96
|
+
// Try to restore only if we do not have any data in the cache and we have persister defined
|
|
97
|
+
if (matchesFilter && query.state.data === undefined && storage != null) {
|
|
98
|
+
try {
|
|
99
|
+
const storedData = await storage.getItem(storageKey)
|
|
100
|
+
if (storedData) {
|
|
101
|
+
const persistedQuery = deserialize(storedData)
|
|
102
|
+
|
|
103
|
+
if (persistedQuery.state.dataUpdatedAt) {
|
|
104
|
+
const queryAge = Date.now() - persistedQuery.state.dataUpdatedAt
|
|
105
|
+
const expired = queryAge > maxAge
|
|
106
|
+
const busted = persistedQuery.buster !== buster
|
|
107
|
+
if (expired || busted) {
|
|
108
|
+
await storage.removeItem(storageKey)
|
|
109
|
+
} else {
|
|
110
|
+
// Just after restoring we want to get fresh data from the server if it's stale
|
|
111
|
+
setTimeout(() => {
|
|
112
|
+
// Set proper updatedAt, since resolving in the first pass overrides those values
|
|
113
|
+
query.setState({
|
|
114
|
+
dataUpdatedAt: persistedQuery.state.dataUpdatedAt,
|
|
115
|
+
errorUpdatedAt: persistedQuery.state.errorUpdatedAt,
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
if (query.isStale()) {
|
|
119
|
+
query.fetch()
|
|
120
|
+
}
|
|
121
|
+
}, 0)
|
|
122
|
+
// We must resolve the promise here, as otherwise we will have `loading` state in the app until `queryFn` resolves
|
|
123
|
+
return Promise.resolve(persistedQuery.state.data as T)
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
await storage.removeItem(storageKey)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} catch (err) {
|
|
130
|
+
if (process.env.NODE_ENV === 'development') {
|
|
131
|
+
console.error(err)
|
|
132
|
+
console.warn(
|
|
133
|
+
'Encountered an error attempting to restore query cache from persisted location.',
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
await storage.removeItem(storageKey)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// If we did not restore, or restoration failed - fetch
|
|
141
|
+
const queryFnResult = await queryFn(context)
|
|
142
|
+
|
|
143
|
+
if (matchesFilter && storage != null) {
|
|
144
|
+
// Persist if we have storage defined, we use timeout to get proper state to be persisted
|
|
145
|
+
setTimeout(() => {
|
|
146
|
+
storage.setItem(
|
|
147
|
+
storageKey,
|
|
148
|
+
serialize({
|
|
149
|
+
state: query.state,
|
|
150
|
+
queryKey: query.queryKey,
|
|
151
|
+
queryHash: query.queryHash,
|
|
152
|
+
buster: buster,
|
|
153
|
+
}),
|
|
154
|
+
)
|
|
155
|
+
}, 0)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return Promise.resolve(queryFnResult)
|
|
159
|
+
}
|
|
160
|
+
}
|
package/src/index.ts
CHANGED