1
1
import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister' ;
2
- import { persistQueryClient } from '@tanstack/react-query-persist-client' ;
2
+ import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client' ;
3
3
import { del as removeItem , get as getItem , set as setItem } from 'idb-keyval' ;
4
4
5
5
import { SENTRY_RELEASE_VERSION } from 'sentry/constants' ;
6
- import { DEFAULT_QUERY_CLIENT_CONFIG , QueryClient } from 'sentry/utils/queryClient' ;
6
+ import {
7
+ DEFAULT_QUERY_CLIENT_CONFIG ,
8
+ QueryClient ,
9
+ QueryClientProvider ,
10
+ } from 'sentry/utils/queryClient' ;
7
11
8
12
/**
9
13
* Named it appQueryClient because we already have a queryClient in sentry/utils/queryClient
@@ -13,10 +17,10 @@ import {DEFAULT_QUERY_CLIENT_CONFIG, QueryClient} from 'sentry/utils/queryClient
13
17
* Instead, use `const queryClient = useQueryClient()`.
14
18
* @link https://tanstack.com/query/v5/docs/reference/QueryClient
15
19
*/
16
- export const appQueryClient = new QueryClient ( DEFAULT_QUERY_CLIENT_CONFIG ) ;
20
+ const appQueryClient = new QueryClient ( DEFAULT_QUERY_CLIENT_CONFIG ) ;
17
21
const cacheKey = 'sentry-react-query-cache' ;
18
22
19
- const localStoragePersister = createAsyncStoragePersister ( {
23
+ const indexedDbPersister = createAsyncStoragePersister ( {
20
24
// We're using indexedDB as our storage provider because projects cache can be large
21
25
storage : { getItem, setItem, removeItem} ,
22
26
// Reduce the frequency of writes to indexedDB
@@ -32,45 +36,50 @@ const isProjectsCacheEnabled =
32
36
) ;
33
37
34
38
/**
35
- * Attach the persister to the query client
36
- * @link https://tanstack.com/query/latest/docs/framework/react/plugins/persistQueryClient
39
+ * Enables the PersistQueryClientProvider when the flag is enabled
37
40
*/
38
- if ( isProjectsCacheEnabled ) {
39
- persistQueryClient ( {
40
- queryClient : appQueryClient ,
41
- persister : localStoragePersister ,
42
- /**
43
- * Clear cache on release version change
44
- * Locally this does nothing, if you need to clear cache locally you can clear indexdb
45
- */
46
- buster : SENTRY_RELEASE_VERSION ?? 'local' ,
47
- dehydrateOptions : {
48
- // Persist a subset of queries to local storage
49
- shouldDehydrateQuery ( query ) {
50
- // This could be extended later to persist other queries
51
- return (
52
- // Query is not pending or failed
53
- query . state . status === 'success' &&
54
- ! query . isStale ( ) &&
55
- Array . isArray ( query . queryKey ) &&
56
- // Currently only bootstrap-projects is persisted
57
- query . queryKey [ 0 ] === 'bootstrap-projects'
58
- ) ;
59
- } ,
60
- } ,
61
- } ) ;
62
- }
63
-
64
- export function restoreQueryCache ( ) {
65
- if ( isProjectsCacheEnabled ) {
66
- localStoragePersister . restoreClient ( ) ;
41
+ export function AppQueryClientProvider ( { children} : { children : React . ReactNode } ) {
42
+ if ( ! isProjectsCacheEnabled ) {
43
+ return < QueryClientProvider client = { appQueryClient } > { children } </ QueryClientProvider > ;
67
44
}
45
+
46
+ return (
47
+ < PersistQueryClientProvider
48
+ client = { appQueryClient }
49
+ persistOptions = { {
50
+ persister : indexedDbPersister ,
51
+ /**
52
+ * Clear cache on release version change
53
+ * Locally this does nothing, if you need to clear cache locally you can clear indexdb
54
+ */
55
+ buster : SENTRY_RELEASE_VERSION ?? 'local' ,
56
+ dehydrateOptions : {
57
+ // Persist a subset of queries to local storage
58
+ shouldDehydrateQuery ( query ) {
59
+ // This could be extended later to persist other queries
60
+ return (
61
+ // Query is not pending or failed
62
+ query . state . status === 'success' &&
63
+ ! query . isStale ( ) &&
64
+ // Currently only bootstrap-projects is persisted
65
+ query . queryKey [ 0 ] === 'bootstrap-projects'
66
+ ) ;
67
+ } ,
68
+ } ,
69
+ } }
70
+ >
71
+ { children }
72
+ </ PersistQueryClientProvider >
73
+ ) ;
68
74
}
69
75
70
76
export async function clearQueryCache ( ) {
71
77
if ( isProjectsCacheEnabled ) {
72
- // Mark queries as stale so they won't be recached
73
- appQueryClient . invalidateQueries ( { queryKey : [ 'bootstrap-projects' ] } ) ;
78
+ // Mark queries as stale so they won't be re-cached
79
+ appQueryClient . invalidateQueries ( {
80
+ queryKey : [ 'bootstrap-projects' ] ,
81
+ refetchType : 'none' ,
82
+ } ) ;
74
83
await removeItem ( cacheKey ) ;
75
84
}
76
85
}
0 commit comments