SimoAmiS
tRPC16mo ago
1 reply
SimoAmi

Context parameters in server side calls

Hello, I using trpc 11.0.0-rc.633 and Next.js 15.0.3 and have the following questions:
1. when a
createContext()
function is defined with the
FetchCreateContextFnOptions
parameter signature, how do you fill that information out when using server side api calls, for example via
createCaller
? The example in the docs is inaccurate in that it defines a
createContext
function to require some parameters:
export const createContext = async (opts: CreateNextContextOptions) => { ... }

and then runs it without passing any parameters:
const caller = createCaller(await createContext());

2. My context needs access to the requesting url query string to get a token (used for shared links). How do I get the url or query parameters in a server call where I don't have implicit access to the request object?
3. In the Next.js main layout, where a
<RootLayout />
is defined, I need to fetch the current user to pass it along to react components. I have a
getUser()
procedure for this. Do I use the client side api defined with
createTRPCReact
to call the procedure? or can I use the server side api created with a
createCaller
since I assume the root layout will be rendered on the server side?
Your context holds data that all of your tRPC procedures will have access to, and is a great place to put things like database connections or authentication information.
Context | tRPC
Was this page helpful?