Open History to see the last commit
1import {
2 MutationCache,
3 QueryClient,
4 notifyManager,
5} from '@tanstack/react-query'
6import { setupRouterSsrQueryIntegration } from '@tanstack/react-router-ssr-query'
7import toast from 'react-hot-toast'
8import { createRouter } from '@tanstack/react-router'
9import { routeTree } from './routeTree.gen'
10import { DefaultCatchBoundary } from './components/DefaultCatchBoundary'
11import { NotFound } from './components/NotFound'
12β
13export function getRouter() {
14 if (typeof document !== 'undefined') {
15 notifyManager.setScheduler(window.requestAnimationFrame)
16 }
17β
18 const queryClient: QueryClient = new QueryClient({
19 defaultOptions: {
20 queries: {
21 refetchOnReconnect: () => !queryClient.isMutating(),
22 },
23 },
24 mutationCache: new MutationCache({
25 onError: (error) => {
26 toast(error.message, { className: 'bg-red-500 text-white' })
27 },
28 onSettled: () => {
29 if (queryClient.isMutating() === 1) {
30 return queryClient.invalidateQueries()
31 }
32 },
33 }),
34 })
35β
36 const router = createRouter({
37 routeTree,
38 defaultPreload: 'intent',
39 defaultErrorComponent: DefaultCatchBoundary,
40 defaultNotFoundComponent: () => <NotFound />,
41 scrollRestoration: true,
42 context: {
43 queryClient,
44 },
45 })
46 setupRouterSsrQueryIntegration({
47 router,
48 queryClient,
49 })
50β
51 return router
52}
53β
54declare module '@tanstack/react-router' {
55 interface Register {
56 router: ReturnType<typeof getRouter>
57 }
58}