Unverified Commit 3c8e9c38 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Support durable use-cache entries with client components (#98140)

The current setup is:
1. A use-cache function returns an RSC value that may contain client
reference proxies. Flight serializes each reference using the client
module `id`, export `name`, client `chunk` list, and `async`. The
resulting Flight payload is stored in the cache entry.
2. When decoding the cache entry, the client module `id` is resolved
through `rscModuleMapping` to obtain the corresponding RSC module `id`
and `export`.

Cache decoding does not need the client chunk list because it does not
load client chunks. The outer RSC serialization emits the client
reference again using the current client reference manifest, including
the current chunks. We can therefore serialize cached references with an
empty chunk list.

The client module `id` is also unstable across builds. Instead, cache
entries can store the stable client reference name. When decoding, we
resolve: client reference name -> current client module ID -> current
RSC module ID
This makes cached client references independent of build-specific module
IDs and chunk filenames.
parent f593b6c8
......@@ -354,7 +354,6 @@ impl Asset for ServerActionManifestAsset {
durability: data.as_ref().map(|d| ActionManifestWorkerEntryDurability {
code_hash: d.ident_code_hash.as_str(),
runtime_env_vars: d.runtime_env_vars.as_slice(),
references_client_component: d.references_client_component,
}),
},
);
......@@ -410,8 +409,6 @@ struct ModulesInformation {
pub ident_code_hash: RcStr,
/// The merged and deduplicated list of all runtime env vars referenced in the subgraph
pub runtime_env_vars: Vec<RcStr>,
/// Whether the subgraph imports any client components
pub references_client_component: bool,
}
#[turbo_tasks::function]
......@@ -430,8 +427,6 @@ async fn compute_subtree_content_hash(
let module_graph_value = module_graph.await?;
let async_module_info = module_graph.async_module_info();
let mut references_client_component = false;
let modules_to_ignore = modules_to_ignore.await?;
let mut modules = FxIndexSet::default();
module_graph_value.traverse_edges_dfs(
......@@ -445,14 +440,12 @@ async fn compute_subtree_content_hash(
.is_some()
{
// Don't include the module at all. There is nothing that executes on the server
references_client_component = true;
Ok(GraphTraversalAction::Exclude)
} else if ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(target)
.is_some()
{
// Include the client reference proxy module, but not the referenced client
// modules themselves.
references_client_component = true;
modules.insert(target);
Ok(GraphTraversalAction::Exclude)
} else {
......@@ -534,7 +527,6 @@ async fn compute_subtree_content_hash(
ModulesInformation {
ident_code_hash: hash,
runtime_env_vars: runtime_env_vars.into_iter().cloned().collect(),
references_client_component,
}
.cell(),
)
......
......@@ -473,8 +473,6 @@ pub struct ActionManifestWorkerEntry<'a> {
pub struct ActionManifestWorkerEntryDurability<'a> {
pub code_hash: &'a str,
pub runtime_env_vars: &'a [RcStr],
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub references_client_component: bool,
}
#[derive(Serialize, Debug, Clone)]
......
......@@ -75,7 +75,6 @@ type Actions = {
durability?: {
codeHash: string
runtimeEnvVars: string[]
referencesClientComponent?: boolean
}
}
}
......
......@@ -8,6 +8,10 @@ import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-pref
import { mightBeServerReferenceId } from '../../shared/lib/server-reference-info'
import { wellKnownProperties } from '../../shared/lib/utils/reflect-utils'
import { workAsyncStorage } from './work-async-storage.external'
import {
workUnitAsyncStorage,
type WorkUnitStore,
} from './work-unit-async-storage.external'
export interface ServerModuleMap {
readonly [name: string]: {
......@@ -71,6 +75,9 @@ interface ManifestsSingleton {
RegisteredClientReferenceManifest
>
readonly proxiedClientReferenceManifest: DeepReadonly<ClientReferenceManifest>
readonly rscModuleMappingForUseCache: DeepReadonly<
ClientReferenceManifest['rscModuleMapping']
>
serverActionsManifest: DeepReadonly<ActionManifest>
serverModuleMap: ServerModuleMap
}
......@@ -88,12 +95,38 @@ type ClientReferenceManifestMappingProp =
const globalThisWithManifests = globalThis as GlobalThisWithManifests
function isUseCacheStore(workUnitStore: WorkUnitStore | undefined): boolean {
if (!workUnitStore) {
return false
}
switch (workUnitStore.type) {
case 'cache':
case 'private-cache':
return true
case 'request':
case 'unstable-cache':
case 'prerender':
case 'prerender-client':
case 'prerender-legacy':
case 'prerender-runtime':
case 'validation-client':
case 'generate-static-params':
return false
default:
return workUnitStore satisfies never
}
}
function createProxiedClientReferenceManifest(
clientReferenceManifestsPerRoute: Map<
string,
RegisteredClientReferenceManifest
>
): DeepReadonly<ClientReferenceManifest> {
): Pick<
ManifestsSingleton,
'proxiedClientReferenceManifest' | 'rscModuleMappingForUseCache'
> {
const createMappingProxy = (prop: ClientReferenceManifestMappingProp) => {
return new Proxy(
{},
......@@ -107,6 +140,40 @@ function createProxiedClientReferenceManifest(
)?.clientReferenceManifest
if (currentManifest?.[prop][id]) {
if (
workStore.durableUseCacheEntries &&
prop === 'clientModules'
) {
const workUnitStore = workUnitAsyncStorage.getStore()
if (isUseCacheStore(workUnitStore)) {
// This creates a mapping so that a given client references with name
// `app/foo/client.tsx` is serialized as
// ```
// { id: `app/foo/client.tsx`, name: `*`, chunks: ["stub"], async: false }
// ```
// instead of the actual
// ```
// { id: 19132, name: `*`, chunks: ["/_next/static/chunks/..."], async: true }
// ```
//
// This means that not the unstable client module id is used for the cache entry,
// but the stable client reference name. The stable client reference name is
// resolved against the current manifest via rscModuleMappingForUseCache when the
// entry is read.
const clientReferenceManifestEntry =
currentManifest.clientModules[id]
return {
// Return `id` instead of `clientReferenceManifestEntry.id` here.
id,
name: clientReferenceManifestEntry.name,
// Set a sentinel value just in case this does ends up being read at some point
// in the future, then at least we'll get "Failed to load chunk stub (404)"
// instead of "Failed to load module 1234".
chunks: ['stub'],
async: false,
}
}
}
return currentManifest[prop][id]
}
......@@ -174,7 +241,7 @@ function createProxiedClientReferenceManifest(
ReturnType<typeof createMappingProxy>
>()
return new Proxy(
const proxiedClientReferenceManifest = new Proxy(
{},
{
get(_, prop) {
......@@ -225,6 +292,47 @@ function createProxiedClientReferenceManifest(
},
}
) as DeepReadonly<ClientReferenceManifest>
// Performs the inverse of the clientModules isUseCacheStore(workUnitStore) special case above:
//
// For cache functions, don't do the usual rscModuleMapping lookup by module ID, but instead look
// up by the stable client reference name. This is because cache entries must not depend on
// build-local module IDs or chunks. So we need a second layer of indirection here to do client
// reference name -> client module id -> rsc module mapping lookup.
//
// This one is created separately (not inside proxiedClientReferenceManifest.rscModuleMapping) as
// we don't necessarily are inside a workUnitStore when we need to access this mapping (as opposed
// to the clientModules mapping, which always has the ALS set).
const rscModuleMappingForUseCache = new Proxy(
{},
{
get(
_,
key: string
): ClientReferenceManifest['rscModuleMapping'][string] | undefined {
const workStore = workAsyncStorage.getStore()
if (workStore && workStore.durableUseCacheEntries) {
const currentManifest = clientReferenceManifestsPerRoute.get(
workStore.route
)?.clientReferenceManifest
const clientReferenceName = key
const clientReferenceManifestEntry =
currentManifest?.clientModules[clientReferenceName]
if (clientReferenceManifestEntry === undefined) {
return undefined
}
const clientModuleId = clientReferenceManifestEntry.id
return currentManifest?.rscModuleMapping[clientModuleId]
} else {
return proxiedClientReferenceManifest.rscModuleMapping[key]
}
},
}
) as DeepReadonly<ClientReferenceManifest['rscModuleMapping']>
return { proxiedClientReferenceManifest, rscModuleMappingForUseCache }
}
/**
......@@ -268,7 +376,6 @@ function createServerModuleMap(): ServerModuleMap {
durability?: {
codeHash: string
runtimeEnvVars: readonly string[]
referencesClientComponent?: boolean
}
}
| undefined
......@@ -384,13 +491,13 @@ export function setManifestsSingleton({
RegisteredClientReferenceManifest
>([[route, { page, clientReferenceManifest }]])
const proxiedClientReferenceManifest = createProxiedClientReferenceManifest(
clientReferenceManifestsPerRoute
)
const { proxiedClientReferenceManifest, rscModuleMappingForUseCache } =
createProxiedClientReferenceManifest(clientReferenceManifestsPerRoute)
globalThisWithManifests[MANIFESTS_SINGLETON] = {
clientReferenceManifestsPerRoute,
proxiedClientReferenceManifest,
rscModuleMappingForUseCache,
serverActionsManifest,
serverModuleMap: createServerModuleMap(),
}
......@@ -407,10 +514,29 @@ function getManifestsSingleton(): ManifestsSingleton {
return manifestSingleton
}
/**
* Returns the usual client_reference_manifest.json.
*
* In dev, it also looks up references of other pages (due to overlapping processing on
* navigations).
*
* Inside a use-cache workUnitStore, clientModules instead map to a stable dummy value (see comments
* above).
*/
export function getClientReferenceManifest(): DeepReadonly<ClientReferenceManifest> {
return getManifestsSingleton().proxiedClientReferenceManifest
}
/**
* A mapping of client reference names to the RSC module id. (So a double lookup of clientModules ->
* rscModuleMapping)
*/
export function getRscModuleMappingForUseCache(): DeepReadonly<
ClientReferenceManifest['rscModuleMapping']
> {
return getManifestsSingleton().rscModuleMappingForUseCache
}
export function getServerActionsManifest(): DeepReadonly<ActionManifest> {
return getManifestsSingleton().serverActionsManifest
}
......
......@@ -12,7 +12,6 @@ import {
createTemporaryReferenceSet as createClientTemporaryReferenceSet,
} from 'react-server-dom-webpack/client'
import { prerender } from 'react-server-dom-webpack/static'
/* eslint-enable import/no-extraneous-dependencies */
import type { WorkStore } from '../app-render/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
......@@ -52,6 +51,7 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight
import {
getClientReferenceManifest,
getRscModuleMappingForUseCache,
getServerActionsManifest,
getServerModuleMap,
normalizeWorkerPageName,
......@@ -3611,7 +3611,7 @@ export async function cache(
// to be added to the consumer. Instead, we'll wait for any ClientReference to be emitted
// which themselves will handle the preloading.
moduleLoading: null,
moduleMap: clientReferenceManifest.rscModuleMapping,
moduleMap: getRscModuleMappingForUseCache(),
serverModuleMap: getServerModuleMap(),
}
......@@ -3648,11 +3648,7 @@ async function computeCacheKeyImplementationPart(
normalizeWorkerPageName(workStore.page)
]?.durability
: undefined
if (
durability &&
// TODO replace this with more granular tracking: a list of all imported client components
durability.referencesClientComponent !== true
) {
if (durability) {
// use cache is only supported in Node.js runtime. So we can use the Node.js crypto module here.
const crypto = require('crypto') as typeof import('crypto')
let runtimeEnvVarStateHash = crypto
......
'use client'
export default function Client({ children }: { children: React.ReactNode }) {
return <span id="data">{children}</span>
}
import { Suspense } from 'react'
import { cacheLife } from 'next/cache'
import { connection } from 'next/server'
import { getDate } from '../logic'
import { Client } from '../import-use-client/client'
async function InnerCache() {
'use cache: remote'
cacheLife('days')
return <Client>{getDate()}</Client>
}
async function OuterCache() {
'use cache: remote'
cacheLife('days')
return InnerCache()
}
export const instant = false
export default async function Page() {
await connection()
return (
<main>
<Suspense>
<OuterCache />
</Suspense>
</main>
)
}
'use client'
import { useState } from 'react'
export function Client({ children }: { children: React.ReactNode }) {
const [text, setText] = useState('')
return (
<div>
<div id="title">Client Component A</div>
<button
onClick={() => {
setText('Button clicked')
}}
>
Click me
</button>
<span id="state">{text}</span>
<div id="data">{children}</div>
</div>
)
}
......@@ -2,9 +2,9 @@ import { Suspense } from 'react'
import { cacheLife } from 'next/cache'
import { connection } from 'next/server'
import { getDate } from '../logic'
import Client from './client'
import { Client } from './client'
async function DynamicCache({ id }: { id: string }) {
async function DynamicCache() {
'use cache: remote'
cacheLife('days')
return <Client>{getDate()}</Client>
......@@ -18,7 +18,7 @@ export default async function Page() {
return (
<main>
<Suspense>
<DynamicCache id="dynamic-cache" />
<DynamicCache />
</Suspense>
</main>
)
......
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