tRPCttRPC
Powered by
webdevisfunW
tRPC•6mo ago•
6 replies
webdevisfun

Deduplicating identical queries in tRPC httpBatchLink (standalone server + Next.js server-side)

Hey folks, I'm running into an issue with tRPC v11 where my standalone tRPC server is called from Next.js 15 server-side using a tRPCProxyClient with httpBatchLink. Batching works great for multiple different procedures, but if the same procedure with identical inputs is called multiple times in the same request (e.g., from nested components), it gets batched but not deduplicated—meaning the server still processes and responds to duplicates, which is inefficient.

Right now, I've got a simple (kinda dumb) workaround: wrapping individual procedure calls in React's cache like this:
const api = /* my tRPCProxyClient with httpBatchLink */;
const userGetInfoCached = cache(api.user.getInfo);
const api = /* my tRPCProxyClient with httpBatchLink */;
const userGetInfoCached = cache(api.user.getInfo);

This dedups per request since the cache shares the promise, but it feels manual and I'd have to do it for every procedure.

I was thinking about a custom
httpBatchDedupLink
httpBatchDedupLink
that "compresses" the batch by removing duplicates (based on path + input hash), sends only uniques to the server, then "uncompresses" by remapping responses to the original ops/order. Maybe even checking React's cache per request for pending responses before sending.

I also tried generically wrapping the whole tRPC proxy client with a JS Proxy to auto-cache each query call, but I couldn't get it working.

How do you all handle deduping identical queries in batched tRPC calls on the server-side? Any custom links, or better patterns you've used?

Insights appreciated!
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

tRPC Client within Next.js but with external standalone server?
boneyBboney / ❓-help
3y ago
tRPC standalone server in monorepo
HuzefHHuzef / ❓-help
3y ago
tRPC Express server with Next.js
sumatokenSsumatoken / ❓-help
3y ago