网页
综合
更新时间 2026-10-15 00:00:00
use cache: private
自动蜘蛛
nextjs.org
正文约 2507 字
The 'use cache: private' directive allows functions to access runtime request APIs like cookies(), headers(), and searchParams within a cached scope. In production, matching calls within one request can reuse the same result, but Next.js does not store it in a server cache across requests. In development, Next.js stores it in memory, keyed by the request's cookies and headers, and serves it to the next matching request while a fresh result is generated in the background.
The client router can keep the rendered output in browser memory for the stale time configured with cacheLife. This client-side cache does not persist across page reloads.
Reach for 'use cache: private' when:
Private Cache Functions run at request time and are excluded from static shell generation. To start a private Cache Function before a component needs its result, see Preloading data.
It is not possible to configure custom cache handlers for 'use cache: private'.
For a comparison of the different cache directives, see How use cache: remote differs from use cache and use cache: private.
To use 'use cache: private', enable the cacheComponents flag in your next.config.ts file:
Then add 'use cache: private' to your function along with a cacheLife configuration.
In this example, we demonstrate that you can access cookies within a 'use cache: private' scope:
The stale time must be at least 30 seconds for per-link prefetching to work, and at least 5 minutes for the content to be included in the route's App Shell. See cacheLife prerendering behavior for details.
Private Cache Functions contribute their stale time to the route's Client Cache. If a function only needs request-scoped deduplication, use cacheLife({ stale: Infinity }) to keep it from lowering the route's stale time.
Next.js uses the shortest stale time from the route's cache entries, so another cache or route setting can still set a finite value. Setting stale to Infinity does not store the private result on the server across production requests. Use a finite value when the client router should revalidate personalized output after a known interval.
The following request-specific APIs can be used inside 'use cache: private' functions:
The connection() API is prohibited in both use cache and 'use cache: private' as it provides connection-specific information that cannot be safely cached.
View related API references.
Last updated October 15, 2026
Subscribe to our newsletter
Stay updated on new releases and features, guides, and case studies.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。