Unverified Commit 9cde9169 authored by Joseph's avatar Joseph Committed by GitHub

docs: clarify use cache persistence across deploys (#93554)

Clarify caching directives non-persistence across deploys.

- [x] reinforce that in serverless deploys, the `use cache` directive
doesn't persist between requests/different instances either
parent 83eea78b
......@@ -87,6 +87,15 @@ Next.js provides three caching directives, each designed for different use cases
| **Server cache utilization** | May be low outside static shell | High (shared across instances) | N/A |
| **Additional costs** | None | Infrastructure (storage, network) | None |
| **Latency impact** | None | Cache handler lookup | None |
| **Persists across deploys** | No | No | N/A |
### Persistence across deploys
Remote cache entries do not persist across deploys. The cache key includes the `deploymentId` (when configured) or the `buildId`, so a new build produces new keys and the previous build's entries are no longer reachable. See [Cache keys](/docs/app/api-reference/directives/use-cache#cache-keys) for the full key composition.
This is intentional. Between builds, the function's identity hash or the shape of its return value can change. Upgrading a CMS client, refactoring a cached function, or changing a dependency could produce a value that doesn't match what older callers expect, so reusing entries across deploys risks serving stale or malformed data.
If you need entries that persist across deploys, use [`unstable_cache`](/docs/app/api-reference/functions/unstable_cache) for non-`fetch` functions, or rely on the [`fetch`](/docs/app/api-reference/functions/fetch) cache.
### Caching with runtime data
......
......@@ -75,7 +75,7 @@ export async function getData() {
A cache entry's key is generated using a serialized version of its inputs, which includes:
1. **Build ID** - Unique per build, changing this invalidates all cache entries
1. **Build ID** - Unique per build, changing this invalidates all cache entries. If [`deploymentId`](/docs/app/api-reference/config/next-config-js/deploymentId) is configured, it overrides the build ID for cache key purposes.
2. **Function ID** - A secure hash of the function's location and signature in the codebase
3. **Serializable arguments** - Props (for components) or function arguments
4. **HMR refresh hash** (development only) - Invalidates cache on hot module replacement
......@@ -199,7 +199,7 @@ Cached functions and components **cannot** directly access runtime APIs like `co
While `use cache` is designed primarily to include uncached data in the static shell, it can also cache data at runtime using in-memory LRU (Least Recently Used) storage.
Runtime cache behavior depends on your hosting environment:
With the default in-memory handler, runtime cache behavior depends on your hosting environment:
| Environment | Runtime Caching Behavior |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
......@@ -210,6 +210,11 @@ For example, in a serverless environment, a cached function shared by two pages
If the default in-memory cache isn't enough, consider **[`use cache: remote`](/docs/app/api-reference/directives/use-cache-remote)** which allows platforms to provide a dedicated cache handler (like Redis or KV database). This helps reduce hits against data sources not scaled to your total traffic, though it comes with costs (storage, network latency, platform fees).
> [!NOTE]
> With the default in-memory handler, serverless instances are ephemeral, so entries may not be reused between requests, unlike with `use cache: remote`. Neither caching directive carries over to a new deploy, because the [cache key](#cache-keys) includes the build (or `deploymentId`) ID.
>
> For data that needs to persist across deploys, use [`unstable_cache`](/docs/app/api-reference/functions/unstable_cache) for non-`fetch` functions or the [`fetch`](/docs/app/api-reference/functions/fetch) cache.
Very rarely, for compliance requirements or when you can't refactor your code to pass runtime data as arguments to a `use cache` scope, you might need [`use cache: private`](/docs/app/api-reference/directives/use-cache-private).
### Draft Mode
......
......@@ -29,6 +29,7 @@ When a `deploymentId` is configured, Next.js:
2. Adds an `x-deployment-id` header to client-side navigation requests
3. Adds an `x-nextjs-deployment-id` header to navigation responses
4. Injects a `data-dpl-id` attribute on the `<html>` element
5. Includes the `deploymentId` in the [`'use cache'` cache key](/docs/app/api-reference/directives/use-cache#cache-keys), invalidating cache entries when the deployment ID changes
When the client detects a mismatch between its deployment ID and the server's (via the response header), it triggers a hard navigation (full page reload) instead of a client-side navigation. This ensures users always receive assets <AppOnly>and Server Functions</AppOnly> from a consistent deployment version.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment