main.tsx40 lines, 1.1 KB
| 1 | import { |
| 2 | QueryCache, |
| 3 | QueryClient, |
| 4 | QueryClientProvider, |
| 5 | } from '@tanstack/react-query' |
| 6 | import { StrictMode } from 'react' |
| 7 | import { createRoot } from 'react-dom/client' |
| 8 | import { RouterProvider } from 'react-router' |
| 9 | import './index.css' |
| 10 | import { ApiError, signOut } from './lib/api' |
| 11 | import { router } from './routes' |
| 12 | |
| 13 | const client = new QueryClient({ |
| 14 | // A token the server doesn't know any more: start again. |
| 15 | queryCache: new QueryCache({ |
| 16 | onError: (e) => { |
| 17 | if (e instanceof ApiError && e.status === 401) signOut() |
| 18 | }, |
| 19 | }), |
| 20 | defaultOptions: { |
| 21 | queries: { |
| 22 | // 60 requests a minute is the budget. The stream says what |
| 23 | // changed, so a read stays good for a minute, and coming back |
| 24 | // to the tab is no reason to ask again. |
| 25 | staleTime: 60_000, |
| 26 | refetchOnWindowFocus: false, |
| 27 | // A 4xx won't get better by asking again. |
| 28 | retry: (n, e) => |
| 29 | n < 2 && !(e instanceof ApiError && e.status < 500), |
| 30 | }, |
| 31 | }, |
| 32 | }) |
| 33 | |
| 34 | createRoot(document.getElementById('root')!).render( |
| 35 | <StrictMode> |
| 36 | <QueryClientProvider client={client}> |
| 37 | <RouterProvider router={router} /> |
| 38 | </QueryClientProvider> |
| 39 | </StrictMode>, |
| 40 | ) |