レスポンスキャッシュ
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
以下の例では、Vercelのエッジキャッシュを使用して、ユーザーにできるだけ速くデータを提供します。
⚠️ 注意点 ⚠️
キャッシュには常に注意してください。特に個人情報を扱う場合は注意が必要です。
バッチ処理はデフォルトで有効になっているため、responseMeta関数でキャッシュヘッダーを設定し、個人データを含む可能性のある同時呼び出しがないことを確認することをお勧めします。あるいは、認証ヘッダーやクッキーがある場合はキャッシュヘッダーを完全に省略してください。
また、splitLinkを使用して、公開するリクエストと非公開でキャッシュしないリクエストを分割することもできます。
アプリケーションキャッシュ
アプリでSSRを有効にすると、例えばVercel上でアプリの読み込みが遅くなる場合がありますが、SSGを使用せずにアプリ全体を静的にレンダリングすることもできます。詳細についてはこのTwitterスレッドを参照してください。
コード例
pages/_app.tsxtsxexport default withTRPC({config(config) {if (typeof window !== 'undefined') {return {url: '/api/trpc',};}const url = process.env.VERCEL_URL? `https://${process.env.VERCEL_URL}/api/trpc`: 'http://localhost:3000/api/trpc';return {url,};},ssr: true,responseMeta(config) {if (config.clientErrors.length) {// propagate http first error from API callsreturn {status: config.clientErrors[0].data?.httpStatus ?? 500,};}// cache request for 1 day + revalidate once every secondconst ONE_DAY_IN_SECONDS = 60 * 60 * 24;return {headers: {'cache-control': `s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,},};},})(MyApp);
pages/_app.tsxtsxexport default withTRPC({config(config) {if (typeof window !== 'undefined') {return {url: '/api/trpc',};}const url = process.env.VERCEL_URL? `https://${process.env.VERCEL_URL}/api/trpc`: 'http://localhost:3000/api/trpc';return {url,};},ssr: true,responseMeta(config) {if (config.clientErrors.length) {// propagate http first error from API callsreturn {status: config.clientErrors[0].data?.httpStatus ?? 500,};}// cache request for 1 day + revalidate once every secondconst ONE_DAY_IN_SECONDS = 60 * 60 * 24;return {headers: {'cache-control': `s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,},};},})(MyApp);
APIレスポンスのキャッシュ
すべてのクエリは通常のHTTP GETリクエストであるため、標準的なHTTPヘッダーを使用してレスポンスをキャッシュできます。これによりレスポンスを高速化し、データベースの負荷を軽減し、APIを大規模なユーザーベースに簡単にスケールできます。
responseMetaを使ったレスポンスのキャッシュ
前提として、APIをVercelのようにstale-while-revalidateキャッシュヘッダーを処理できる環境にデプロイしているものとします。
server.tstsximport * as trpc from '@trpc/server';import { inferAsyncReturnType } 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 = inferAsyncReturnType<typeof createContext>;export function createRouter() {return trpc.router<Context>();}const waitFor = async (ms: number) =>new Promise((resolve) => setTimeout(resolve, ms));export const appRouter = createRouter().query('public.slow-query-cached', {async resolve({ ctx }) {await waitFor(5000); // wait for 5sreturn {lastUpdated: new Date().toJSON(),};},});// Exporting type _type_ AppRouter only exposes types that can be used for inference// https://www.typescriptlang.org/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({ ctx, paths, type, errors }) {// 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: {'cache-control': `s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,},};}return {};},});
server.tstsximport * as trpc from '@trpc/server';import { inferAsyncReturnType } 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 = inferAsyncReturnType<typeof createContext>;export function createRouter() {return trpc.router<Context>();}const waitFor = async (ms: number) =>new Promise((resolve) => setTimeout(resolve, ms));export const appRouter = createRouter().query('public.slow-query-cached', {async resolve({ ctx }) {await waitFor(5000); // wait for 5sreturn {lastUpdated: new Date().toJSON(),};},});// Exporting type _type_ AppRouter only exposes types that can be used for inference// https://www.typescriptlang.org/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({ ctx, paths, type, errors }) {// 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: {'cache-control': `s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`,},};}return {};},});