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!0 Replies