Liltripple_reid
Liltripple_reid10mo ago

`createCaller` and RSC - with next-auth

I've been upgrading my trpc routers and handlers to support edge+app router with drizzle and next-auth; and something that has come up is the use of createCaller (with createTRPCNext instead of createTRPCReact - and without the TRPCProvider). So I wanted to ask if this is a correct way of using that function
// app/_trpc/server-client.tsx
import { type Session } from "next-auth";
import { appRouter } from "~/server/api/root";

export const serverClient = ({ session }: { session: Session | null }) => {
return appRouter.createCaller({ session });
};
// app/_trpc/server-client.tsx
import { type Session } from "next-auth";
import { appRouter } from "~/server/api/root";

export const serverClient = ({ session }: { session: Session | null }) => {
return appRouter.createCaller({ session });
};
4 Replies
Liltripple_reid
Liltripple_reid10mo ago
so with that I'm using it in RSC pages like this
import { getServerSession } from "next-auth";
import { authOptions } from "~/server/auth";
import { serverClient } from "./_trpc/server-client";

export default async function LandingPage() {
const session = await getServerSession(authOptions);
const data = await serverClient({ session }).example.allUsers();

return (
<section>
<h1>Landing Page</h1>

<pre>{JSON.stringify(data)}</pre>
</section>
);
}
import { getServerSession } from "next-auth";
import { authOptions } from "~/server/auth";
import { serverClient } from "./_trpc/server-client";

export default async function LandingPage() {
const session = await getServerSession(authOptions);
const data = await serverClient({ session }).example.allUsers();

return (
<section>
<h1>Landing Page</h1>

<pre>{JSON.stringify(data)}</pre>
</section>
);
}
I'm wondering if returning/creating the caller on every RSC is a good practice, because I read that it's possible that it may execute all middlewares and validations again
Alex / KATT 🐱
Alex / KATT 🐱10mo ago
It only executes the middlewares on the route you call
Liltripple_reid
Liltripple_reid10mo ago
so it's safe to use like this, right?
Alex / KATT 🐱
Alex / KATT 🐱10mo ago
Yes
More Posts
useQuery always fetching when active on pageHi, I was call my procedure like this, using NextJs (page directory) const { isLoading, isError, isSnextjs app router `fetchRequestHandler`'s createContext doesn't run.Hey there, I have the following code: ```ts // /src/app/api/trpc/[trpc]/route.ts import { fetchRequ"Unable to transform response from server" when 401 error returned from middleware (sometimes)I'm trying to figure out a problem where some (a few, not most) 401 error responses result in an uncHow do pass a 'blob' from frontend to backend?I'm having trouble doing this without converting an audio Blob to base64 and then decrypting it and fetch failed - error on npm start on productionI rebuild my next.js app with the npm run build command and I started it with npm start I get thisType return error when using mongoose.node - v16.15.1 npm I'm somewhat new to trpc. Using it with mongoose. Love it so far althought I douseContext won't infer type from tRPC clientI am calling tRPC endpoints from my app and it's working flawlessly. As you can see on first screensForward client headers with createTRPCProxyClient & Server-Side HelpersWith `createTRPCNext` i had the option to tap into the `context.req` object nextjs passed in, but I I have a websocket + REST project. Should I be using splitLink like this to combine WS and HTTP?I have a backend project that uses REST for queries/mutations and Websockets for subscriptions. I jNested procedures are separated by dots, is there a way to change that to `/`s instead ?currently it looks something like this `/api/trpc/post.byId`, is there a way to have it like `/api/t