-
Notifications
You must be signed in to change notification settings - Fork 30k
Description
Link to the code that reproduces this issue
https://github.com/ajaykarthikr/next-16-use-cache-private-bug
To Reproduce
-
Create a cached function with "use cache: private" that uses cookies():
// 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: 30 }); 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, } }
-
Use this function in a Server Component page:
// 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> ); }
-
Configure next.config.ts:
import type { NextConfig } from "next"; const nextConfig: NextConfig = { cacheComponents: true, }; export default nextConfig;
-
Navigate to the page (e.g., /about), then navigate away, then navigate back using client-side navigation (using component)
Current vs. Expected behavior
Expected: The cached function should return the cached result during the 30-second stale period, showing the same timestamp and not logging "Fetching user data" on subsequent navigations.
Actual: The function re-executes on every client-side navigation, showing a new timestamp each time and logging "Fetching user data" in the console.
Demo: https://next-16-use-cache-private-bug.vercel.app/
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 25.0.0: Wed Sep 17 21:42:08 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T8132
Available memory (MB): 24576
Available CPU cores: 10
Binaries:
Node: 22.20.0
npm: 10.9.3
Yarn: N/A
pnpm: N/A
Relevant Packages:
next: 16.0.2-canary.3 // Latest available version is detected (16.0.2-canary.3).
eslint-config-next: N/A
react: 19.2.0
react-dom: 19.2.0
typescript: 5.9.3
Next.js Config:
output: N/AWhich area(s) are affected? (Select all that apply)
Use Cache
Which stage(s) are affected? (Select all that apply)
next start (local), Vercel (Deployed)
Additional context
- The "use cache: private" directive is supposed to work with cookies() API according to the documentation
- This appears to be related to Router Cache interaction with function-level caching during client-side navigation
- The 2-second artificial delay in the reproduction makes it easy to observe when the function re-executes