trpc.ts (1308B)
1 import { httpBatchLink } from '@trpc/client'; 2 import { createTRPCNext } from '@trpc/next'; 3 // import superjson from 'superjson'; 4 5 import type { AppRouter } from 'server/routers/_app'; 6 7 function getBaseUrl() { 8 if (typeof window !== 'undefined') 9 // browser should use relative path 10 return ''; 11 if (process.env['VERCEL_URL']) 12 // reference for vercel.com 13 return `https://${process.env['VERCEL_URL']}`; 14 if (process.env['RENDER_INTERNAL_HOSTNAME']) 15 // reference for render.com 16 return `http://${process.env['RENDER_INTERNAL_HOSTNAME']}:${process.env['PORT']}`; 17 // assume localhost 18 return `http://localhost:${process.env['PORT'] ?? 3000}`; 19 } 20 21 export const trpc = createTRPCNext<AppRouter>({ 22 config() { 23 return { 24 links: [ 25 httpBatchLink({ 26 /** 27 * If you want to use SSR, you need to use the server's full URL 28 * @link https://trpc.io/docs/ssr 29 **/ 30 url: `${getBaseUrl()}/api/trpc`, 31 // transformer: superjson, 32 }), 33 ], 34 /** 35 * @link https://tanstack.com/query/v4/docs/reference/QueryClient 36 **/ 37 queryClientConfig: { 38 defaultOptions: { 39 queries: { refetchOnReconnect: false, refetchOnWindowFocus: false }, 40 }, 41 }, 42 abortOnUnmount: true, 43 }; 44 }, 45 /** 46 * @link https://trpc.io/docs/ssr 47 **/ 48 ssr: false, 49 // transformer: superjson, 50 });