HTTP 批次連結
httpBatchLink
是 終止連結,它會將陣列中的個別 tRPC 作業批次處理成單一 HTTP 要求,並傳送至單一 tRPC 程序。
用法
您可以匯入並將 httpBatchLink
加入 links
陣列,如下所示
client/index.tsts
import { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpBatchLink({url: 'http://localhost:3000',}),// transformer,],});
client/index.tsts
import { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpBatchLink({url: 'http://localhost:3000',}),// transformer,],});
之後,您可以透過將所有程序設定在 Promise.all
中來使用批次處理。下列程式碼將產生 一個 HTTP 要求,而伺服器上將產生 一個 資料庫查詢
ts
const somePosts = await Promise.all([trpc.post.byId.query(1),trpc.post.byId.query(2),trpc.post.byId.query(3),]);
ts
const somePosts = await Promise.all([trpc.post.byId.query(1),trpc.post.byId.query(2),trpc.post.byId.query(3),]);
httpBatchLink
選項
httpBatchLink
函式會接收一個選項物件,其具有 HTTPBatchLinkOptions
形狀。
ts
export interface HTTPBatchLinkOptions extends HTTPLinkOptions {maxURLLength?: number;}export interface HTTPLinkOptions {url: string;/*** Add ponyfill for fetch*/fetch?: typeof fetch;/*** Add ponyfill for AbortController*/AbortController?: typeof AbortController | null;/*** Data transformer* @link https://trpc.dev.org.tw/docs/data-transformers**/transformer?: DataTransformerOptions;/*** Headers to be set on outgoing requests or a callback that of said headers* @link https://trpc.dev.org.tw/docs/header*/headers?:| HTTPHeaders| ((opts: { opList: Operation[] }) => HTTPHeaders | Promise<HTTPHeaders>);}
ts
export interface HTTPBatchLinkOptions extends HTTPLinkOptions {maxURLLength?: number;}export interface HTTPLinkOptions {url: string;/*** Add ponyfill for fetch*/fetch?: typeof fetch;/*** Add ponyfill for AbortController*/AbortController?: typeof AbortController | null;/*** Data transformer* @link https://trpc.dev.org.tw/docs/data-transformers**/transformer?: DataTransformerOptions;/*** Headers to be set on outgoing requests or a callback that of said headers* @link https://trpc.dev.org.tw/docs/header*/headers?:| HTTPHeaders| ((opts: { opList: Operation[] }) => HTTPHeaders | Promise<HTTPHeaders>);}
設定最大 URL 長度
在發送批次要求時,有時 URL 可能會過大,導致 HTTP 錯誤,例如 413 Payload Too Large
、414 URI Too Long
和 404 Not Found
。maxURLLength
選項將限制可以在批次中一起發送的要求數量。
執行此操作的另一種方法是
client/index.tsts
import { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = 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.tsts
import { createTRPCClient, httpBatchLink } from '@trpc/client';import type { AppRouter } from '../server';const client = 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',}),],});
停用要求批次處理
1. 在伺服器上停用 batching
:
server.tsts
import { createHTTPServer } from '@trpc/server/adapters/standalone';createHTTPServer({// [...]// 👇 disable batchingallowBatching: false,});
server.tsts
import { createHTTPServer } from '@trpc/server/adapters/standalone';createHTTPServer({// [...]// 👇 disable batchingallowBatching: false,});
或者,如果您使用 Next.js
pages/api/trpc/[trpc].tsts
export default trpcNext.createNextApiHandler({// [...]// 👇 disable batchingallowBatching: false,});
pages/api/trpc/[trpc].tsts
export default trpcNext.createNextApiHandler({// [...]// 👇 disable batchingallowBatching: false,});
2. 在您的 tRPC Client 中將 httpBatchLink
替換為 httpLink
client/index.tsts
import { createTRPCClient, httpLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',}),],});
client/index.tsts
import { createTRPCClient, httpLink } from '@trpc/client';import type { AppRouter } from '../server';const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',}),],});
或者,如果您使用 Next.js
utils/trpc.tstsx
import type { AppRouter } from '@/server/routers/app';import { httpLink } from '@trpc/client';import { createTRPCNext } from '@trpc/next';export const trpc = createTRPCNext<AppRouter>({config() {return {links: [httpLink({url: '/api/trpc',}),],};},});
utils/trpc.tstsx
import type { AppRouter } from '@/server/routers/app';import { httpLink } from '@trpc/client';import { createTRPCNext } from '@trpc/next';export const trpc = createTRPCNext<AppRouter>({config() {return {links: [httpLink({url: '/api/trpc',}),],};},});