@@ -7,6 +7,7 @@ import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
77import { createWSClient , httpBatchLink , wsLink , splitLink } from "@trpc/client" ;
88import { createTRPCClient } from "@trpc/client" ;
99import { AppRouter } from "./routers" ;
10+ import { env } from "~/env" ;
1011
1112function makeQueryClient ( ) {
1213 return new QueryClient ( {
@@ -39,27 +40,39 @@ function getQueryClient() {
3940}
4041
4142export function TRPCClientProvider ( { children } : PropsWithChildren ) {
42- const wsClient = createWSClient ( {
43- url : "ws://localhost:3001" ,
44- } ) ;
4543 const queryClient = getQueryClient ( ) ;
46- const [ trpcClient ] = useState ( ( ) =>
47- createTRPCClient < AppRouter > ( {
48- links : [
49- splitLink ( {
50- condition ( op ) {
51- return op . type === "subscription" ;
52- } ,
53- true : wsLink ( {
54- client : wsClient ,
55- } ) ,
56- false : httpBatchLink ( {
57- url : "http://localhost:3000/api/trpc" ,
44+
45+ const [ trpcClient ] = useState ( ( ) => {
46+ if ( env . NEXT_PUBLIC_WS_URL ) {
47+ const wsClient = createWSClient ( {
48+ url : `ws://${ env . NEXT_PUBLIC_WS_URL } ` ,
49+ } ) ;
50+
51+ return createTRPCClient < AppRouter > ( {
52+ links : [
53+ splitLink ( {
54+ condition ( op ) {
55+ return op . type === "subscription" ;
56+ } ,
57+ true : wsLink ( {
58+ client : wsClient ,
59+ } ) ,
60+ false : httpBatchLink ( {
61+ url : `${ env . NEXT_PUBLIC_API_URL } /api/trpc` ,
62+ } ) ,
5863 } ) ,
64+ ] ,
65+ } ) ;
66+ }
67+
68+ return createTRPCClient < AppRouter > ( {
69+ links : [
70+ httpBatchLink ( {
71+ url : `${ env . NEXT_PUBLIC_API_URL } /api/trpc` ,
5972 } ) ,
6073 ] ,
61- } )
62- ) ;
74+ } ) ;
75+ } ) ;
6376
6477 return (
6578 < QueryClientProvider client = { queryClient } >
0 commit comments