I'm using Next.js 16 with Cache Components (cache() from React) to deduplicate and cache database queries across layouts and pages.
Here's my setup:
// lib/data.ts
import { cache } from 'react';
import db from '@/lib/db';
export const getUser = cache(async (userId: string) => {
console.log('Fetching user from DB...', userId);
return await db.user.findUnique({
where: { id: userId },
next: { revalidate: 3600, tags: ['user'] } // 1 hour cache
});
});
// app/layout.tsx
import { getUser } from '@/lib/data';
export default async function RootLayout({ children }) {
const user = await getUser('123'); // Should be cached
return (
<html>
<body>
<Header user={user} />
{children}
</body>
</html>
);
}
// app/dashboard/page.tsx
import { getUser } from '@/lib/data';
export default async function DashboardPage() {
const user = await getUser('123'); // Same ID — should reuse cache
return <Dashboard user={user} />;
}
Expected behavior:
- First request: DB hit + log
- Subsequent requests (within 1 hour): NoDB hit, same user object from cache
Actual behavior:
Every request logs "Fetching user from DB..."
Cache seems to be ignored completely
What I've tried:
Verified
next: { revalidate: 3600 }is supported in Prisma + Next.js Data CacheConfirmed
cache()is imported from'react'No
unstable_cacheorno-storeRunning in production mode (
next start), not devNo dynamic functions or
cookies()in the call chain
Question:
Why is the Data Cache not respecting revalidate when using cache()?
Is there a known limitation when combining cache() + next: { revalidate } in Prisma queries?
How can I debug if the cache key is being generated correctly?
Next.js: 16.0.0
React: 19.0.0
Prisma: 5.15.0
Any help or workaround would be appreciated!