You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
docs: Current cacheing docs are invalid for NextJS App Router #5625
As such - there is no documentation or examples (anywhere, as far as I can tell) for how to manage cacheing of tRPC endpoints running in a NextJS App Router environment.
I was just revisiting this issue and the existing strategy of declaring cache-control headers for API routes within next.config.js works for TRPC endpoints.
Reacted by Jeffrey (Computer Operator), Jens Becker, QArify, Alex Harder, WhiteMind, Jenna Smith, musjj, Johann Pönitz, Ryo Sogawa, Igor Makowski and 1 more
@jeffrey-computers tRPC does batch request, so suppose if I want cache for a single router from multiple routers then I don't think that headers workaround will work! Correct me if I'm wrong!
Here is a summary how I Enabled CDN Caching for Specific tRPC Procedures in Next.js
Problem
tRPC batches all requests by default, making individual endpoint caching impossible
All tRPC calls use POST requests, which many CDNs don't cache
Need selective caching for specific procedures while maintaining batching for others
Solution Overview
Used splitLink to conditionally separate cacheable requests from batched requests,
then set appropriate Cache-Control headers in the API route handler.
Key Changes Made
Client-Side: Configure splitLink
// ApiClientProvider.tsx
splitLink({
condition(op) {
return Boolean(op.context.skipBatch)
},
true: httpLink({...}), // Non-batched GET requests
false: httpBatchLink({...}) // Batched POST requests
})
(Full Example Down below)
API Route: Handle Both GET and POST Requests
// app/api/[trpc]/route.ts
const cacheablePaths = ["lesson.fetchLesson", "lesson.byId",
"lesson.getAllLessons"]
async function wrappedHandler(req: NextRequest) {
// Check GET requests in URL
if (method === "GET" && isCacheable(url)) {
shouldCache = true
}
// Check POST requests in body
else if (method === "POST") {
const body = await req.text()
shouldCache = cacheablePaths.some(path => body.includes(path))
// Reconstruct request since body was consumed
}
if (shouldCache) {
response.headers.set("Cache-Control", "public, s-maxage=864000,
stale-while-revalidate=60")
}
}
GET requests with proper cache headers: cache-control: public, s-maxage=864000,
stale-while-revalidate=60
Selective caching for specific procedures only
Other requests remain batched for performance
CDN now caches lesson data for 10 days
FYI
If you are running your dev environment and looking for Vercel Headers, ex. x-vercel-cache, you won't see any of these in development, only in production will vercel add these headers.
Key Insight
The tricky part was handling both GET and POST requests in the route handler, since
some configurations still use POST even with skipBatch: true, and reading the
request body consumes it (requiring reconstruction).
🤖 The caching docs now have a Next.js App Router section: set Cache-Control in responseMeta on fetchRequestHandler in your route handler, and send cacheable public queries through httpLink with a splitLink so they aren't batched with private ones.
Activity
DawnMD commentedon Sep 19, 2024
@jeffrey-computers tRPC does batch request, so suppose if I want cache for a single router from multiple routers then I don't think that headers workaround will work! Correct me if I'm wrong!
dionne-chelini commentedon Jul 7, 2025
Here is a summary how I Enabled CDN Caching for Specific tRPC Procedures in Next.js
Problem
Solution Overview
Used splitLink to conditionally separate cacheable requests from batched requests,
then set appropriate Cache-Control headers in the API route handler.
Key Changes Made
// ApiClientProvider.tsx
(Full Example Down below)
// app/api/[trpc]/route.ts
// Component making the call
Result
stale-while-revalidate=60
FYI
If you are running your dev environment and looking for Vercel Headers, ex. x-vercel-cache, you won't see any of these in development, only in production will vercel add these headers.
Key Insight
The tricky part was handling both GET and POST requests in the route handler, since
some configurations still use POST even with skipBatch: true, and reading the
request body consumes it (requiring reconstruction).
Full Examples
trpc-bot commentedon Nov 3, 2026
🤖 The caching docs now have a Next.js App Router section: set
Cache-ControlinresponseMetaonfetchRequestHandlerin your route handler, and send cacheable public queries throughhttpLinkwith asplitLinkso they aren't batched with private ones.docs(server): add Next.js App Router section to response caching (trp…