HTTP Batch Link
httpBatchLink is a terminating link that batches an array of individual tRPC operations into a single HTTP request that's sent to a single tRPC procedure.
Usage
You can import and add the httpBatchLink to the links array as such:
client/index.tsts
client/index.tsts
After that, you can make use of batching by setting all your procedures in a Promise.all. The code below will produce exactly one HTTP request and on the server exactly one database query:
ts
ts
httpBatchLink Options
The httpBatchLink function takes an options object that has the HTTPBatchLinkOptions shape.
ts
ts
Setting a maximum URL length
When sending batch requests, sometimes the URL can become too large causing HTTP errors like 413 Payload Too Large, 414 URI Too Long, and 404 Not Found. The maxURLLength option will limit the number of requests that can be sent together in a batch.
client/index.tsts
client/index.tsts
Disabling request batching
1. Disable batching on your server:
server.tsts
server.tsts
or, if you're using Next.js:
pages/api/trpc/[trpc].tsts
pages/api/trpc/[trpc].tsts
2. Replace httpBatchLink with httpLink in your tRPC Client
client/index.tsts
client/index.tsts
or, if you're using Next.js:
utils/trpc.tstsx
utils/trpc.tstsx
Reference
You can check out the source code for this link on GitHub.