Something went wrong. Try again.
A social knowledge tool for research trails, built on ATProto semble.so
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import { QueryClient, QueryCache, MutationCache, defaultShouldDehydrateQuery, environmentManager,} from '@tanstack/react-query';import { isNotFoundApiError, isUnauthorizedApiError,} from '@/api-client/errors';
type ErrorHandler = (error: unknown) => void;
/** * Creates a QueryClient. Kept free of `'use client'` and of any browser-only * imports so Server Components can build one too — the auth error handler is * injected by the client provider rather than imported here. */export function makeQueryClient(onError?: ErrorHandler) { return new QueryClient({ queryCache: new QueryCache({ onError: (error, query) => { // A query erroring during SSR corrupts the streaming render (errored // suspense boundaries can wedge the response stream open until the // function times out), and the injected auth handler must never run // there — logoutUser() is browser-only. Log the key so the offending // query is identifiable in Vercel logs, and skip the handler. if (environmentManager.isServer()) { console.error( '[SSR] query error:', JSON.stringify(query.queryKey), error, ); return; } onError?.(error); }, }), mutationCache: new MutationCache({ onError: (error, _variables, _context, mutation) => { if (environmentManager.isServer()) { console.error( '[SSR] mutation error:', JSON.stringify(mutation.options.mutationKey ?? null), error, ); return; } onError?.(error); }, }), defaultOptions: { queries: { staleTime: 60 * 1000, gcTime: 5 * 60 * 1000, refetchOnWindowFocus: false, // 404 and 401 are definitive answers — retrying only doubles the // round-trips before the error surfaces (and a retried 401 delays the // logout redirect). Everything else still gets one retry. retry: (failureCount, error) => !isNotFoundApiError(error) && !isUnauthorizedApiError(error) && failureCount < 1, }, dehydrate: { // include pending queries in dehydration shouldDehydrateQuery: (query) => defaultShouldDehydrateQuery(query) || query.state.status === 'pending', }, }, });}
/** * A throwaway QueryClient for one server-side prefetch boundary. * * Each Server Component that prefetches makes its own, dehydrates it, and * renders its own HydrationBoundary. Hydration merges them into the single * browser client, so separate boundaries cost nothing and — unlike a shared * request-scoped client — never serialize the same query into the payload twice. * * Server Components only. The browser singleton lives in providers/tanstack.tsx. */export function makeServerQueryClient() { return makeQueryClient();}