Skip to content

Commit

Permalink
switch link, add headers for server side
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Oct 11, 2023
1 parent 0159aea commit 5323d84
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* We also create a few inference helpers for input and output types
*/
import { httpBatchLink, loggerLink } from '@trpc/client'
import { unstable_httpBatchStreamLink as httpBatchStreamLink, loggerLink } from '@trpc/client'
import { createTRPCNext } from '@trpc/next'
import { type inferRouterInputs, type inferRouterOutputs } from '@trpc/server'
import superjson from 'superjson'
Expand All @@ -17,10 +17,11 @@ const getBaseUrl = () => {
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}` // SSR should use vercel url
return `http://localhost:${process.env.PORT ?? 3000}` // dev SSR should use localhost
}

const isServer = typeof window === 'undefined'
/** A set of typesafe react-query hooks for your tRPC API */
export const api = createTRPCNext<AppRouter>({
config() {
config(opts) {
const { ctx } = opts
return {
/**
* Transformer used for data de-serialization from the server
Expand All @@ -40,8 +41,20 @@ export const api = createTRPCNext<AppRouter>({
process.env.NODE_ENV === 'development' ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
httpBatchLink({
httpBatchStreamLink({
url: `${getBaseUrl()}/api/trpc`,
...(isServer
? {
headers() {
if (!ctx?.req?.headers) {
return {}
}
return {
cookie: ctx.req.headers.cookie,
}
},
}
: {}),
}),
],
}
Expand Down

0 comments on commit 5323d84

Please sign in to comment.