Skip to content

Commit eec0775

Browse files
committed
feat: add ForceClearQueryCacheProvider
1 parent ce8b31a commit eec0775

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ VITE_DEV_PASSWORD=Password12345678
8888
API_BASE_URL=http://localhost:5500
8989
# The number of miliseconds to delay the result of HTTP requests in development
9090
VITE_DEV_NETWORK_LATENCY=0
91+
# Whether to force clear queries when the pathname changes
92+
VITE_DEV_FORCE_CLEAR_QUERY_CACHE=false
9193
# Plausable analytics config (optional, set both to an empty string to disable)
9294
PLAUSIBLE_BASE_URL=
9395
PLAUSIBLE_WEB_DATA_DOMAIN=

apps/web/src/Routes.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { sessionRoute } from './features/session';
1616
import { uploadRoute } from './features/upload';
1717
import { userRoute } from './features/user';
1818
import { DisclaimerProvider } from './providers/DisclaimerProvider';
19+
import { ForceClearQueryCacheProvider } from './providers/ForceClearQueryCacheProvider';
1920
import { WalkthroughProvider } from './providers/WalkthroughProvider';
2021
import { useAppStore } from './store';
2122

@@ -33,7 +34,9 @@ const protectedRoutes: RouteObject[] = [
3334
element: (
3435
<DisclaimerProvider>
3536
<WalkthroughProvider>
36-
<Layout />
37+
<ForceClearQueryCacheProvider>
38+
<Layout />
39+
</ForceClearQueryCacheProvider>
3740
</WalkthroughProvider>
3841
</DisclaimerProvider>
3942
),

apps/web/src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const $Config = z.object({
1111
.optional(),
1212
dev: z.object({
1313
isBypassAuthEnabled: $BooleanString.optional(),
14+
isForceClearQueryCacheEnabled: $BooleanString.optional(),
1415
networkLatency: z.coerce.number().int().nonnegative().optional(),
1516
password: z.string().min(1).optional(),
1617
username: z.string().min(1).optional()
@@ -38,6 +39,7 @@ export const config = await $Config
3839
: undefined,
3940
dev: {
4041
isBypassAuthEnabled: import.meta.env.VITE_DEV_BYPASS_AUTH,
42+
isForceClearQueryCacheEnabled: import.meta.env.VITE_DEV_FORCE_CLEAR_QUERY_CACHE,
4143
networkLatency: import.meta.env.VITE_DEV_NETWORK_LATENCY,
4244
password: import.meta.env.VITE_DEV_PASSWORD,
4345
username: import.meta.env.VITE_DEV_USERNAME
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Fragment, useEffect } from 'react';
2+
3+
import { useQueryClient } from '@tanstack/react-query';
4+
import { useLocation } from 'react-router-dom';
5+
6+
import { config } from '@/config';
7+
8+
let ForceClearQueryCacheProvider: React.FC<{ children: React.ReactNode }>;
9+
10+
if (config.dev.isForceClearQueryCacheEnabled) {
11+
// eslint-disable-next-line react/function-component-definition
12+
ForceClearQueryCacheProvider = function ForceClearQueryCacheProvider({ children }) {
13+
const queryClient = useQueryClient();
14+
const location = useLocation();
15+
16+
useEffect(() => {
17+
//queryClient.clear();
18+
queryClient.clear();
19+
}, [location.pathname]);
20+
21+
return <>{children}</>;
22+
};
23+
} else {
24+
ForceClearQueryCacheProvider = Fragment;
25+
}
26+
27+
export { ForceClearQueryCacheProvider };

apps/web/src/vite-env.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// All of these should be undefined in production
77
interface ImportMetaDevEnv {
88
readonly VITE_DEV_BYPASS_AUTH?: string;
9+
readonly VITE_DEV_FORCE_CLEAR_QUERY_CACHE?: string;
10+
readonly VITE_DEV_NETWORK_LATENCY?: string;
911
readonly VITE_DEV_PASSWORD?: string;
1012
readonly VITE_DEV_USERNAME?: string;
1113
}
@@ -19,7 +21,6 @@ interface ImportMetaEnv extends ImportMetaDevEnv {
1921
readonly LICENSE_URL?: string;
2022
readonly PLAUSIBLE_BASE_URL?: string;
2123
readonly PLAUSIBLE_WEB_DATA_DOMAIN?: string;
22-
readonly VITE_DEV_NETWORK_LATENCY?: string;
2324
}
2425

2526
interface ImportMeta {

0 commit comments

Comments
 (0)