Skip to content

Commit 2752ceb

Browse files
committed
example: add custom QueryClient with optimized caching
- Configure a custom QueryClient with specific caching strategies - Pass the queryClient instance to StacApiProvider via prop This change clarifies how to set up and use a custom QueryClient, and investigates the impact of multiple QueryClientProviders on app behavior.
1 parent dc9c226 commit 2752ceb

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

example/src/App.jsx

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
import { StacApiProvider } from 'stac-react';
2+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
23
import Header from './layout/Header';
34
import Main from './pages/Main';
45

6+
// Create a QueryClient with custom cache configuration
7+
// IMPORTANT: Must be created outside the component to maintain cache across renders
8+
const queryClient = new QueryClient({
9+
defaultOptions: {
10+
queries: {
11+
// STAC data doesn't change frequently, so we can cache it for 5 minutes
12+
staleTime: 5 * 60 * 1000, // 5 minutes
13+
// Keep unused data in cache for 10 minutes
14+
gcTime: 10 * 60 * 1000, // 10 minutes
15+
retry: 1,
16+
// Disable automatic refetching since STAC data is static
17+
refetchOnWindowFocus: false,
18+
refetchOnMount: false,
19+
refetchOnReconnect: false,
20+
},
21+
},
22+
});
23+
524
function App() {
625
const apiUrl = process.env.REACT_APP_STAC_API;
726
const isDevelopment = process.env.NODE_ENV === 'development';
827

28+
// Debug: Verify QueryClient configuration
29+
if (isDevelopment && typeof window !== 'undefined') {
30+
console.log('[App] QueryClient defaults:', queryClient.getDefaultOptions());
31+
}
32+
933
return (
10-
<StacApiProvider apiUrl={apiUrl} enableDevTools={isDevelopment}>
11-
<div className="App grid grid-rows-[min-content_1fr]">
12-
<Header />
13-
<main className="flex items-stretch">
14-
<Main />
15-
</main>
16-
</div>
17-
</StacApiProvider>
34+
<QueryClientProvider client={queryClient}>
35+
<StacApiProvider apiUrl={apiUrl} enableDevTools={isDevelopment} queryClient={queryClient}>
36+
<div className="App grid grid-rows-[min-content_1fr]">
37+
<Header />
38+
<main className="flex items-stretch">
39+
<Main />
40+
</main>
41+
</div>
42+
</StacApiProvider>
43+
</QueryClientProvider>
1844
);
1945
}
2046

0 commit comments

Comments
 (0)