tRPCttRPC
Powered by
notoriousfeliN
tRPC•2y ago•
1 reply
notoriousfeli

Setting up tRPC for next.js with edge AND serverless functions

tldr: I am trying to build an app that uses both edge AND serverless functions by creating separate endpoints. Has anyone ever done this successfully?

I am building an app that runs openai-API-requests and is connected to a Planetscale mySQL-DB with a prisma-layer (t3-setup). All functions are serverless. I am facing timeout issues on the openai-requests, since they are taking quite long. I am trying to switch all openai-functionalities to run on the edge. This means longer timeout-limits on vercel and the ability to stream the response. All the other functions need to remain serverless, since they use prisma, which isn't available on the edge.

What I have tried so far:
1. Create separate context for serverless and edge
2. Create separate routers for serverless and edge
3. Create separate endpoints /pages/api/trpc/[trpc].ts and /pages/api/trpc/edge.ts that utilize the respective context & router
4. Introduce a rewrite for the routes in vercel.json:
{
"rewrites": [
{ "source": "/api/openai/:path*", "destination": "/api/trpc/edge" }
]
}
5. Created separate variables api and apiEdge in server/utils/api.ts to use in the frontend, but reverted this

I am still not 100% if this is even possible. Has anyone ever done something like this? Am I on the right track?

More specificly:
Is it possible to create two endpoints in /pages/api/trpc?
How do I correctly target these endpoints?
How do I supply the endpoints with the correct context?
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Next.js + tRPC, multitenancy, access Next.js Router when setting tRPC headers
anton.johanssonAanton.johansson / ❓-help
3y ago
setting up trpc in next 14
junior1Jjunior1 / ❓-help
3y ago
Setting up tRPC with Supabase and Drizzle
WezterWWezter / ❓-help
3y ago
Does tRPC work with Clerk and Vercel Edge functions?
XenoXXeno / ❓-help
3y ago