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:
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 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!4 Replies
we don't deduplicate by default
however, we have an old
dedupeLink internally that we have used for testingit's not exposed publicly, but you can copy/pasta the code
https://github.com/trpc/trpc/blob/main/packages/client/src/links/internals/dedupeLink.ts
GitHub
trpc/packages/client/src/links/internals/dedupeLink.ts at main · t...
🧙♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/trpc
wow great! thanks Alex, i will take a look
@webdevisfun Any luck with this? I'm in research phase right now to solve the same exact issue in my nextjs 16 project.