Files
supabase/apps/studio/data/query-client.ts
2025-01-20 14:03:28 +08:00

55 lines
1.3 KiB
TypeScript

import { QueryClient, onlineManager } from '@tanstack/react-query'
import { IS_PLATFORM } from 'lib/constants'
import { useState } from 'react'
import { ResponseError } from 'types'
// When running locally we don't need the internet
// so we can pretend we're online all the time
if (!IS_PLATFORM) {
onlineManager.setOnline(true)
}
let queryClient: QueryClient | undefined
export function getQueryClient() {
const _queryClient =
queryClient ??
new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 1000, // 1 minute
retry(failureCount, error) {
// Don't retry on 4xx errors
if (
error instanceof ResponseError &&
error.code !== undefined &&
error.code >= 400 &&
error.code < 500
) {
return false
}
if (failureCount < 3) {
return true
}
return false
},
},
},
})
// For SSG and SSR always create a new queryClient
if (typeof window === 'undefined') return _queryClient
// Create the queryClient once in the client
if (!queryClient) queryClient = _queryClient
return queryClient
}
export function useRootQueryClient() {
const [_queryClient] = useState(() => getQueryClient())
return _queryClient
}