You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/framework/vue/plugins/createPersister.md
+72-5Lines changed: 72 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,10 +33,11 @@ bun add @tanstack/query-persist-client-core
33
33
34
34
- Import the `experimental_createQueryPersister` function
35
35
- Create a new `experimental_createQueryPersister`
36
-
- you can pass any `storage` to it that adheres to the `AsyncStorage`or `Storage`interface
36
+
- you can pass any `storage` to it that adheres to the `AsyncStorage` interface
37
37
- Pass that `persister` as an option to your Query. This can be done either by passing it to the `defaultOptions` of the `QueryClient` or to any `useQuery` hook instance.
38
38
- If you pass this `persister` as `defaultOptions`, all queries will be persisted to the provided `storage`. You can additionally narrow this down by passing `filters`. In contrast to the `persistClient` plugin, this will not persist the whole query client as a single item, but each query separately. As a key, the query hash is used.
39
39
- If you provide this `persister` to a single `useQuery` hook, only this Query will be persisted.
40
+
- Note: `queryClient.setQueryData()` operations are not persisted, this means that if you perform an optimistic update and refresh the page before the query has been invalidated, your changes to the query data will be lost. See https://github.com/TanStack/query/issues/6310
40
41
41
42
This way, you do not need to store whole `QueryClient`, but choose what is worth to be persisted in your application. Each query is lazily restored (when the Query is first used) and persisted (after each run of the `queryFn`), so it does not need to be throttled. `staleTime` is also respected after restoring the Query, so if data is considered `stale`, it will be refetched immediately after restoring. If data is `fresh`, the `queryFn` will not run.
42
43
@@ -65,6 +66,63 @@ const queryClient = new QueryClient({
65
66
66
67
The `createPersister` plugin technically wraps the `queryFn`, so it doesn't restore if the `queryFn` doesn't run. In that way, it acts as a caching layer between the Query and the network. Thus, the `networkMode` defaults to `'offlineFirst'` when a persister is used, so that restoring from the persistent storage can also happen even if there is no network connection.
67
68
69
+
## Additional utilities
70
+
71
+
Invoking `experimental_createQueryPersister` returns additional utilities in addition to `persisterFn` for easier implementation of userland functionalities.
This function can be used to restore queries that are currently stored by persister.
114
+
For example when your app is starting up in offline mode, or you want all or only specific data from previous session to be immediately available without intermediate `loading` state.
115
+
116
+
The filter object supports the following properties:
117
+
118
+
-`queryKey?: QueryKey`
119
+
- Set this property to define a query key to match on.
120
+
-`exact?: boolean`
121
+
- If you don't want to search queries inclusively by query key, you can pass the `exact: true` option to return only the query with the exact query key you have passed.
122
+
123
+
For this function to work, your storage must expose `entries` method that would return a `key-value tuple array`.
124
+
For example `Object.entries(localStorage)` for `localStorage` or `entries` from `idb-keyval`.
0 commit comments