Skip to content

Commit b402600

Browse files
committed
feat(bootstrap): Switch to PersistQueryClientProvider
In #89139 @TkDodo recommended we use the PersistQueryClientProvider instead of the isRestoring states it provides. Adds a function that checks if the feature is enabled.
1 parent 9910f62 commit b402600

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

static/app/appQueryClient.tsx

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
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';
33
import {del as removeItem, get as getItem, set as setItem} from 'idb-keyval';
44

55
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';
711

812
/**
913
* 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
1317
* Instead, use `const queryClient = useQueryClient()`.
1418
* @link https://tanstack.com/query/v5/docs/reference/QueryClient
1519
*/
16-
export const appQueryClient = new QueryClient(DEFAULT_QUERY_CLIENT_CONFIG);
20+
const appQueryClient = new QueryClient(DEFAULT_QUERY_CLIENT_CONFIG);
1721
const cacheKey = 'sentry-react-query-cache';
1822

19-
const localStoragePersister = createAsyncStoragePersister({
23+
const indexedDbPersister = createAsyncStoragePersister({
2024
// We're using indexedDB as our storage provider because projects cache can be large
2125
storage: {getItem, setItem, removeItem},
2226
// Reduce the frequency of writes to indexedDB
@@ -32,45 +36,50 @@ const isProjectsCacheEnabled =
3236
);
3337

3438
/**
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
3740
*/
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>;
6744
}
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+
);
6874
}
6975

7076
export async function clearQueryCache() {
7177
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+
});
7483
await removeItem(cacheKey);
7584
}
7685
}

static/app/bootstrap/initializeApp.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import './legacyTwitterBootstrap';
22
import './exportGlobals';
33

4-
import {restoreQueryCache} from 'sentry/appQueryClient';
54
import type {Config} from 'sentry/types/system';
65
import {metric} from 'sentry/utils/analytics';
76

@@ -12,7 +11,6 @@ import {renderMain} from './renderMain';
1211
import {renderOnDomReady} from './renderOnDomReady';
1312

1413
export function initializeApp(config: Config) {
15-
restoreQueryCache();
1614
commonInitialization(config);
1715
initializeSdk(config);
1816

static/app/main.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import {createBrowserRouter, RouterProvider} from 'react-router-dom';
33
import {wrapCreateBrowserRouterV6} from '@sentry/react';
44
import {ReactQueryDevtools} from '@tanstack/react-query-devtools';
55

6-
import {appQueryClient} from 'sentry/appQueryClient';
6+
import {AppQueryClientProvider} from 'sentry/appQueryClient';
77
import {OnboardingContextProvider} from 'sentry/components/onboarding/onboardingContext';
88
import {ThemeAndStyleProvider} from 'sentry/components/themeAndStyleProvider';
99
import {USE_REACT_QUERY_DEVTOOL} from 'sentry/constants';
1010
import {routes} from 'sentry/routes';
1111
import {DANGEROUS_SET_REACT_ROUTER_6_HISTORY} from 'sentry/utils/browserHistory';
12-
import {QueryClientProvider} from 'sentry/utils/queryClient';
1312

1413
import {buildReactRouter6Routes} from './utils/reactRouter6Compat/router';
1514

@@ -25,7 +24,7 @@ function Main() {
2524
const [router] = useState(buildRouter);
2625

2726
return (
28-
<QueryClientProvider client={appQueryClient}>
27+
<AppQueryClientProvider>
2928
<ThemeAndStyleProvider>
3029
<OnboardingContextProvider>
3130
<RouterProvider router={router} />
@@ -34,7 +33,7 @@ function Main() {
3433
<ReactQueryDevtools initialIsOpen={false} buttonPosition="bottom-left" />
3534
)}
3635
</ThemeAndStyleProvider>
37-
</QueryClientProvider>
36+
</AppQueryClientProvider>
3837
);
3938
}
4039

0 commit comments

Comments
 (0)