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.tstsimport {createTRPCClient ,httpBatchLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [httpBatchLink ({url : 'http://localhost:3000',// transformer,}),],});
client/index.tstsimport {createTRPCClient ,httpBatchLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [httpBatchLink ({url : 'http://localhost:3000',// transformer,}),],});
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:
tsconstsomePosts = awaitPromise .all ([trpc .post .byId .query (1),trpc .post .byId .query (2),trpc .post .byId .query (3),]);
tsconstsomePosts = awaitPromise .all ([trpc .post .byId .query (1),trpc .post .byId .query (2),trpc .post .byId .query (3),]);
httpBatchLink Options
The httpBatchLink function takes an options object that has the HTTPBatchLinkOptions shape.
tsexport interfaceHTTPBatchLinkOptions extendsHTTPLinkOptions {/*** Maximum length of HTTP URL allowed before operations are split into multiple requests* @default Infinity*/maxURLLength ?: number;/*** Maximum number of operations allowed in a single batch request* @default Infinity*/maxItems ?: number;}export interfaceHTTPLinkOptions {url : string |URL ;/*** Add ponyfill for fetch*/fetch ?: typeoffetch ;/*** Data transformer* @see https://trpc.io/docs/server/data-transformers**/transformer ?:DataTransformerOptions ;/*** Headers to be set on outgoing requests or a callback that of said headers* @see https://trpc.io/docs/client/headers*/headers ?:|HTTPHeaders | ((opts : {opList :NonEmptyArray <Operation > }) =>HTTPHeaders |Promise <HTTPHeaders >);}
tsexport interfaceHTTPBatchLinkOptions extendsHTTPLinkOptions {/*** Maximum length of HTTP URL allowed before operations are split into multiple requests* @default Infinity*/maxURLLength ?: number;/*** Maximum number of operations allowed in a single batch request* @default Infinity*/maxItems ?: number;}export interfaceHTTPLinkOptions {url : string |URL ;/*** Add ponyfill for fetch*/fetch ?: typeoffetch ;/*** Data transformer* @see https://trpc.io/docs/server/data-transformers**/transformer ?:DataTransformerOptions ;/*** Headers to be set on outgoing requests or a callback that of said headers* @see https://trpc.io/docs/client/headers*/headers ?:|HTTPHeaders | ((opts : {opList :NonEmptyArray <Operation > }) =>HTTPHeaders |Promise <HTTPHeaders >);}
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.tstsimport {createTRPCClient ,httpBatchLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [httpBatchLink ({url : 'http://localhost:3000',maxURLLength : 2083, // a suitable size// alternatively, you can make all RPC-calls to be called with POST// methodOverride: 'POST',}),],});
client/index.tstsimport {createTRPCClient ,httpBatchLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [httpBatchLink ({url : 'http://localhost:3000',maxURLLength : 2083, // a suitable size// alternatively, you can make all RPC-calls to be called with POST// methodOverride: 'POST',}),],});
Disabling request batching
1. Disable batching on your server:
server.tstsimport {createHTTPServer } from '@trpc/server/adapters/standalone';import {appRouter } from './router';createHTTPServer ({router :appRouter ,// 👇 disable batchingallowBatching : false,});
server.tstsimport {createHTTPServer } from '@trpc/server/adapters/standalone';import {appRouter } from './router';createHTTPServer ({router :appRouter ,// 👇 disable batchingallowBatching : false,});
or, if you're using Next.js:
pages/api/trpc/[trpc].tstsimport {createNextApiHandler } from '@trpc/server/adapters/next';import {appRouter } from '../../../router';export defaultcreateNextApiHandler ({router :appRouter ,// 👇 disable batchingallowBatching : false,});
pages/api/trpc/[trpc].tstsimport {createNextApiHandler } from '@trpc/server/adapters/next';import {appRouter } from '../../../router';export defaultcreateNextApiHandler ({router :appRouter ,// 👇 disable batchingallowBatching : false,});
2. Replace httpBatchLink with httpLink in your tRPC Client
client/index.tstsimport {createTRPCClient ,httpLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [httpLink ({url : 'http://localhost:3000',}),],});
client/index.tstsimport {createTRPCClient ,httpLink } from '@trpc/client';import type {AppRouter } from './server';constclient =createTRPCClient <AppRouter >({links : [httpLink ({url : 'http://localhost:3000',}),],});
or, if you're using Next.js:
utils/trpc.tstsximport type {AppRouter } from '../server';import {httpLink } from '@trpc/client';import {createTRPCNext } from '@trpc/next';export consttrpc =createTRPCNext <AppRouter >({config () {return {links : [httpLink ({url : '/api/trpc',}),],};},});
utils/trpc.tstsximport type {AppRouter } from '../server';import {httpLink } from '@trpc/client';import {createTRPCNext } from '@trpc/next';export consttrpc =createTRPCNext <AppRouter >({config () {return {links : [httpLink ({url : '/api/trpc',}),],};},});