Open History to see the last commit
1import { createRouter as createTanStackRouter } from '@tanstack/react-router'
2import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
3import { createTRPCClient, httpBatchLink } from '@trpc/client'
4import { createTRPCOptionsProxy } from '@trpc/tanstack-react-query'
5β
6// Import the generated route tree
7import { routeTree } from './routeTree.gen'
8β
9import { Spinner } from './routes/-components/spinner'
10import type { AppRouter } from './server/trpc'
11β
12export const queryClient = new QueryClient()
13β
14export const trpc = createTRPCOptionsProxy<AppRouter>({
15 client: createTRPCClient({
16 links: [
17 httpBatchLink({
18 // since we are using Vite, the server is running on the same port,
19 // this means in dev the url is `http://localhost:3000/trpc`
20 // and since its from the same origin, we don't need to explicitly set the full URL
21 url: '/trpc',
22 }),
23 ],
24 }),
25 queryClient,
26})
27β
28export function createRouter() {
29 const router = createTanStackRouter({
30 routeTree,
31 scrollRestoration: true,
32 defaultPreload: 'intent',
33 context: {
34 trpc,
35 queryClient,
36 },
37 defaultPendingComponent: () => (
38 <div className={`p-2 text-2xl`}>
39 <Spinner />
40 </div>
41 ),
42 Wrap: function WrapComponent({ children }) {
43 return (
44 <QueryClientProvider client={queryClient}>
45 {children}
46 </QueryClientProvider>
47 )
48 },
49 })
50β
51 return router
52}
53β
54// Register the router instance for type safety
55declare module '@tanstack/react-router' {
56 interface Register {
57 router: ReturnType<typeof createRouter>
58 }
59}