Replies: 1 comment
-
|
Hi @ellemedit! Great catch on this behavior difference. The issue you're experiencing with 🔍 Why It Doesn't Work in Route HandlersRoute Handlers vs React Server ComponentsThe key difference:
✅ Solutions for Route HandlersSolution 1: Use
|
| Cache Type | RSC Components | Route Handlers | Use Case |
|---|---|---|---|
use cache |
✅ Yes | Global caching | |
use cache: remote |
✅ Yes | ✅ Yes | Distributed cache |
use cache: private |
✅ Yes | ❌ No | Request-scoped (React only) |
unstable_cache |
✅ Yes | ✅ Yes | Data Cache API |
cache() from React |
✅ Yes | ✅ Yes | Request deduplication |
🎯 Why This Happens
From the Next.js caching architecture:
use cache: privaterelies on React's request context (AsyncLocalStorage)- Route Handlers run outside React's render tree
- The "private" cache scope needs the React rendering context to track the request boundary
- Without this context, each call is treated as a new request
🔧 Recommended Pattern for Your Use Case
Based on your code, here's the best approach:
// app/api/your-route/route.ts
import { unstable_cache } from 'next/cache'
import { cacheLife, cacheTag } from 'next/cache'
export async function GET() {
// Define cached function
const getCachedValue = unstable_cache(
async (id: number) => {
console.log('Cache miss for id:', id)
// Your expensive operation here
return performExpensiveOperation(id)
},
['cache-private-value'], // Unique cache key
{
tags: ['useCachePrivateCheck'],
revalidate: 60,
}
)
// This will properly cache and dedupe
for (let i = 0; i < 10; i++) {
await getCachedValue(1) // Only called once
}
const count = await getCachedValue(1)
return Response.json({ count })
}
async function performExpensiveOperation(id: number) {
// Your logic here
return id
}📚 Additional Resources
- Next.js Caching Deep Dive
unstable_cacheAPI Reference- React
cache()Documentation - Next.js 15 Caching Improvements
🤔 Is This a Bug?
No, this is expected behavior. The use cache: private directive is specifically for React Server Components, not Route Handlers. For Route Handlers, use unstable_cache or React's cache().
If you believe Route Handlers should support use cache: private, you could open a feature request on the Next.js GitHub repo explaining your use case!
Hope this helps clarify the behavior! Let me know if you need help implementing any of these solutions. 🚀
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
both
use cacheanduse cache: remotework in route handler butuse cache: privatedoesn't work in route handler. it doesn't dedupe invocations and memoization during a request.Additional information
I tested in local development.
RSC works:
it prints [1, 1, 1, ..., 1] or [2, 2, 2, ..., 2] at the same time.
but route handler doesn't:
it prints 11, 22 or 33
all tests were validated at [email protected] canary
Beta Was this translation helpful? Give feedback.
All reactions