レスポンスキャッシング
以下の例ではVercelのエッジキャッシングを使用して、可能な限り高速にユーザーにデータを提供します。
情報
キャッシングには特に注意してください。とくに個人情報を取り扱う場合に限らず。
デフォルトでバッチ処理が有効になっているので、キャッシュヘッダーをresponseMeta
関数に設定し、個人データを含む可能性がある同時実行呼び出しがないことを確認するか、認証ヘッダーまたはCookieがある場合はキャッシュヘッダーを完全に省略することをお勧めします。
splitLink
を使用して、公開リクエストとプライベートにしてキャッシュしないリクエストを分割することもできます。
アプリのキャッシング
アプリでSSRを有効にすると、たとえばVercelではアプリの読み込みが遅くなることがわかりますが、SSGを使用せずにアプリ全体を静的にレンダリングできます。このTwitterのスレッドで詳細を確認してください。
サンプルコード
utils/trpc.tsxtsx
import { httpBatchLink } from '@trpc/client';import { createTRPCNext } from '@trpc/next';import type { AppRouter } from '../server/routers/_app';export const trpc = createTRPCNext<AppRouter>({config(opts) {if (typeof window !== 'undefined') {return {links: [httpBatchLink({url: '/api/trpc',}),],};}const url = process.env.VERCEL_URL? `https://${process.env.VERCEL_URL}/api/trpc`: 'http://localhost:3000/api/trpc';return {links: {http: httpBatchLink({url,}),},};},ssr: true,responseMeta(opts) {const { clientErrors } = opts;if (clientErrors.length) {// propagate http first error from API callsreturn {status: clientErrors[0].data?.httpStatus ?? 500,};}// cache request for 1 day + revalidate once every secondconst ONE_DAY_IN_SECONDS = 60 * 60 * 24;return {headers: new Headers([['cache-control',`s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,],]),};},});
utils/trpc.tsxtsx
import { httpBatchLink } from '@trpc/client';import { createTRPCNext } from '@trpc/next';import type { AppRouter } from '../server/routers/_app';export const trpc = createTRPCNext<AppRouter>({config(opts) {if (typeof window !== 'undefined') {return {links: [httpBatchLink({url: '/api/trpc',}),],};}const url = process.env.VERCEL_URL? `https://${process.env.VERCEL_URL}/api/trpc`: 'http://localhost:3000/api/trpc';return {links: {http: httpBatchLink({url,}),},};},ssr: true,responseMeta(opts) {const { clientErrors } = opts;if (clientErrors.length) {// propagate http first error from API callsreturn {status: clientErrors[0].data?.httpStatus ?? 500,};}// cache request for 1 day + revalidate once every secondconst ONE_DAY_IN_SECONDS = 60 * 60 * 24;return {headers: new Headers([['cache-control',`s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,],]),};},});
APIのレスポンスキャッシング
すべてのクエリは通常のHTTP GET
であるため、通常のHTTPヘッダーを使用してレスポンスをキャッシュし、レスポンスを迅速にし、データベースを休息させ、APIを簡単に何十億ものユーザーに拡大できます。
responseMeta
を使用してレスポンスをキャッシュする
Vercel のような陳腐化していても検証する間はキャッシュできるキャッシュヘッダーを処理できる場所に API をデプロイしていると想定します。
server.tstsx
import { initTRPC } from '@trpc/server';import * as trpcNext from '@trpc/server/adapters/next';export const createContext = async ({req,res,}: trpcNext.CreateNextContextOptions) => {return {req,res,prisma,};};type Context = Awaited<ReturnType<typeof createContext>>;export const t = initTRPC.context<Context>().create();const waitFor = async (ms: number) =>new Promise((resolve) => setTimeout(resolve, ms));export const appRouter = t.router({public: t.router({slowQueryCached: t.procedure.query(async (opts) => {await waitFor(5000); // wait for 5sreturn {lastUpdated: new Date().toJSON(),};}),}),});// Exporting type _type_ AppRouter only exposes types that can be used for inference// https://typescript.dokyumento.jp/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-exportexport type AppRouter = typeof appRouter;// export API handlerexport default trpcNext.createNextApiHandler({router: appRouter,createContext,responseMeta(opts) {const { ctx, paths, errors, type } = opts;// assuming you have all your public routes with the keyword `public` in themconst allPublic = paths && paths.every((path) => path.includes('public'));// checking that no procedures erroredconst allOk = errors.length === 0;// checking we're doing a query requestconst isQuery = type === 'query';if (ctx?.res && allPublic && allOk && isQuery) {// cache request for 1 day + revalidate once every secondconst ONE_DAY_IN_SECONDS = 60 * 60 * 24;return {headers: new Headers([['cache-control',`s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,],]),};}return {};},});
server.tstsx
import { initTRPC } from '@trpc/server';import * as trpcNext from '@trpc/server/adapters/next';export const createContext = async ({req,res,}: trpcNext.CreateNextContextOptions) => {return {req,res,prisma,};};type Context = Awaited<ReturnType<typeof createContext>>;export const t = initTRPC.context<Context>().create();const waitFor = async (ms: number) =>new Promise((resolve) => setTimeout(resolve, ms));export const appRouter = t.router({public: t.router({slowQueryCached: t.procedure.query(async (opts) => {await waitFor(5000); // wait for 5sreturn {lastUpdated: new Date().toJSON(),};}),}),});// Exporting type _type_ AppRouter only exposes types that can be used for inference// https://typescript.dokyumento.jp/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-exportexport type AppRouter = typeof appRouter;// export API handlerexport default trpcNext.createNextApiHandler({router: appRouter,createContext,responseMeta(opts) {const { ctx, paths, errors, type } = opts;// assuming you have all your public routes with the keyword `public` in themconst allPublic = paths && paths.every((path) => path.includes('public'));// checking that no procedures erroredconst allOk = errors.length === 0;// checking we're doing a query requestconst isQuery = type === 'query';if (ctx?.res && allPublic && allOk && isQuery) {// cache request for 1 day + revalidate once every secondconst ONE_DAY_IN_SECONDS = 60 * 60 * 24;return {headers: new Headers([['cache-control',`s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,],]),};}return {};},});