Skip to content

Commit

Permalink
fix: base url for trpc provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhenry committed May 17, 2024
1 parent 71932f2 commit 6578d8b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/utils/trpc-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@

import { QueryClientProvider } from '@tanstack/react-query'
import { getFetch, httpBatchLink, loggerLink } from '@trpc/client'
import { signOut, useSession } from 'next-auth/react'
import { useEffect, useState } from 'react'
import superjson from 'superjson'
import queryClient from './query-client'
import { trpc } from './trpc'
import { signOut, useSession } from 'next-auth/react'

export const TrpcProvider = ({ children }: { children: React.ReactNode }) => {
const url = process.env.NEXT_PUBLIC_VERCEL_URL
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: 'http://localhost:3000/api/trpc/'
const getBaseUrl = () => {
if (typeof window !== 'undefined')
// browser should use relative path
return ''
if (process.env.VERCEL_URL)
// reference for vercel.com deployments
return `https://${process.env.VERCEL_URL}`
if (process.env.NEXTAUTH_URL)
// reference for non-vercel providers
return process.env.NEXTAUTH_URL
// assume localhost
return `http://localhost:${process.env.PORT ?? 3000}`
}

export const TrpcProvider = ({ children }: { children: React.ReactNode }) => {
const [trpcClient] = useState(() =>
trpc.createClient({
links: [
loggerLink({
enabled: () => true,
}),
httpBatchLink({
url,
url: `${getBaseUrl()}/api/trpc`,
fetch: async (input, init?) => {
const fetch = getFetch()
return fetch(input, {
Expand Down

0 comments on commit 6578d8b

Please sign in to comment.