-
Summaryboth Additional informationI tested in local development. RSC works: import { cacheLife, cacheTag } from "next/cache";
import { headers } from "next/headers";
import { Suspense } from "react";
let cacheValue = 0;
async function getCachePrivateValue() {
"use cache: private";
cacheTag("test-private");
cacheLife({ stale: 60 });
const auth = (await headers()).get('Authorization') || 'guest';
console.log('Called:', auth, cacheValue);
cacheValue += 1;
return cacheValue;
}
export default async function TestCachePage() {
const results = [];
for (let i = 0; i < 11; i++) {
results.push(getCachePrivateValue());
}
return (
<div>
<h1>Cache Test</h1>
<Suspense>
{Promise.all(results).then(results => results.join(", "))}
</Suspense>
</div>
);
}it prints [1, 1, 1, ..., 1] or [2, 2, 2, ..., 2] at the same time. but route handler doesn't: import { cacheLife, cacheTag } from "next/cache";
export async function GET() {
for (let i = 0; i < 10; i++) {
await getCachePrivateValue(1);
}
const count = await getCachePrivateValue(1);
return Response.json({ count });
}
let cacheValue = 0;
export async function getCachePrivateValue(id: number) {
"use cache: private";
cacheTag(`useCachePrivateCheck-${id}`);
cacheLife({ stale: 60, revalidate: 60, expire: Infinity });
cacheValue += 1;
return cacheValue;
}it prints 11, 22 or 33 all tests were validated at [email protected] canary |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
@ellemedit sorry for the confusion here, and the AI answer clearly not having done any checks.
|
Beta Was this translation helpful? Give feedback.

@ellemedit sorry for the confusion here, and the AI answer clearly not having done any checks.
use cache: privateworks only on a browser, the calculation is done server side and sent to the client, which stores it in a client side ephemeral cache. While you can use the other directives, the key difference withuse cache: privateis that it is meant to not store things server side. So yeah, it won't work the way you are trying to use it.