This repository reproduces a caching issue with Next.js 16.0.1 "use cache: private" directive.
When using "use cache: private" directive with cookies() API in a cached function, the cache is not being utilized during client-side navigation. The function re-executes on every navigation instead of serving from cache.
// src/app/lib/user.ts
import { cookies } from "next/headers";
import { cacheTag, cacheLife } from "next/cache";
export async function getUser() {
"use cache: private";
cacheTag(`userdata`);
cacheLife({ stale: 60 });
const sessionId = (await cookies()).get('session-id')?.value || 'guest'
console.log("Fetching user data"); // This logs on every navigation
await new Promise((resolve) => setTimeout(resolve, 5000));
const timestamp = new Date().toISOString();
return {
user: {
name: "Ajay",
email: "ajay@example.com"
},
timestamp: timestamp,
}
}// src/app/about/page.tsx
import { Suspense } from "react";
import { getUser } from "../lib/user";
export default async function Page() {
return (
<div>
<h1>About Page</h1>
<Suspense fallback={<p>Loading...</p>}>
<Content />
</Suspense>
</div>
)
}
async function Content() {
const data = await getUser();
return (
<div>
<h1>{data.user.name}</h1>
<p>Fetched at: {data.timestamp}</p>
</div>
);
}// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
cacheComponents: true,
};
export default nextConfig;- Navigate to the page (e.g.,
/about) - Navigate away to another page
- Navigate back using client-side navigation (using
<Link>component)
The cached function should return the cached result during the 30-second stale period, showing:
- The same timestamp on subsequent navigations
- No "Fetching user data" log in the console after initial fetch
The function re-executes on every client-side navigation:
- Shows a new timestamp each time
- Logs "Fetching user data" in the console on every navigation
- The 5-second delay occurs on each navigation
Operating System:
- Platform: darwin
- Version: Darwin 25.0.0
Binaries:
- Node: 22.20.0
- npm: 10.9.3
Relevant Packages:
- next: 16.0.1
- react: 19.2.0
- react-dom: 19.2.0
- typescript: 5.x
- App Router
- Caching (ISR, Data Cache, Full Route Cache)
next dev(local)next build(local)next start(local)
- The issue persists even when explicitly setting
cacheLife({ stale: 60 })instead of using presets - Tried setting
experimental.staleTimes.dynamic: 0but it didn't resolve the issue - The
"use cache: private"directive is supposed to work withcookies()API according to the documentation - This appears to be related to Router Cache interaction with function-level caching during client-side navigation
# Install dependencies
npm install
# Run development server
npm run dev
# Or build and run production
npm run build
npm startNavigate to the About page to observe the caching behavior.