Open History to see the last commit
1import {
2 QueryClient,
3 defaultShouldDehydrateQuery,
4 environmentManager,
5} from '@tanstack/react-query'
6β
7function makeQueryClient() {
8 return new QueryClient({
9 defaultOptions: {
10 queries: {
11 staleTime: 60 * 1000,
12 },
13 dehydrate: {
14 // include pending queries in dehydration
15 shouldDehydrateQuery: (query) =>
16 defaultShouldDehydrateQuery(query) ||
17 query.state.status === 'pending',
18 // Next.js redacts errors itself and needs the originals to detect dynamic pages
19 shouldRedactErrors: () => false,
20 },
21 },
22 })
23}
24β
25let browserQueryClient: QueryClient | undefined = undefined
26β
27export function getQueryClient() {
28 if (environmentManager.isServer()) {
29 // Server: always make a new query client
30 return makeQueryClient()
31 } else {
32 // Browser: make a new query client if we don't already have one
33 // This is very important, so we don't re-make a new client if React
34 // suspends during the initial render. This may not be needed if we
35 // have a suspense boundary BELOW the creation of the query client
36 if (!browserQueryClient) browserQueryClient = makeQueryClient()
37 return browserQueryClient
38 }
39}