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 { ...@@ -354,7 +354,6 @@ impl Asset for ServerActionManifestAsset {
durability: data.as_ref().map(|d| ActionManifestWorkerEntryDurability { durability: data.as_ref().map(|d| ActionManifestWorkerEntryDurability {
code_hash: d.ident_code_hash.as_str(), code_hash: d.ident_code_hash.as_str(),
runtime_env_vars: d.runtime_env_vars.as_slice(), runtime_env_vars: d.runtime_env_vars.as_slice(),
references_client_component: d.references_client_component,
}), }),
}, },
); );
...@@ -410,8 +409,6 @@ struct ModulesInformation { ...@@ -410,8 +409,6 @@ struct ModulesInformation {
pub ident_code_hash: RcStr, pub ident_code_hash: RcStr,
/// The merged and deduplicated list of all runtime env vars referenced in the subgraph /// The merged and deduplicated list of all runtime env vars referenced in the subgraph
pub runtime_env_vars: Vec<RcStr>, pub runtime_env_vars: Vec<RcStr>,
/// Whether the subgraph imports any client components
pub references_client_component: bool,
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -430,8 +427,6 @@ async fn compute_subtree_content_hash( ...@@ -430,8 +427,6 @@ async fn compute_subtree_content_hash(
let module_graph_value = module_graph.await?; let module_graph_value = module_graph.await?;
let async_module_info = module_graph.async_module_info(); let async_module_info = module_graph.async_module_info();
let mut references_client_component = false;
let modules_to_ignore = modules_to_ignore.await?; let modules_to_ignore = modules_to_ignore.await?;
let mut modules = FxIndexSet::default(); let mut modules = FxIndexSet::default();
module_graph_value.traverse_edges_dfs( module_graph_value.traverse_edges_dfs(
...@@ -445,14 +440,12 @@ async fn compute_subtree_content_hash( ...@@ -445,14 +440,12 @@ async fn compute_subtree_content_hash(
.is_some() .is_some()
{ {
// Don't include the module at all. There is nothing that executes on the server // Don't include the module at all. There is nothing that executes on the server
references_client_component = true;
Ok(GraphTraversalAction::Exclude) Ok(GraphTraversalAction::Exclude)
} else if ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(target) } else if ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(target)
.is_some() .is_some()
{ {
// Include the client reference proxy module, but not the referenced client // Include the client reference proxy module, but not the referenced client
// modules themselves. // modules themselves.
references_client_component = true;
modules.insert(target); modules.insert(target);
Ok(GraphTraversalAction::Exclude) Ok(GraphTraversalAction::Exclude)
} else { } else {
...@@ -534,7 +527,6 @@ async fn compute_subtree_content_hash( ...@@ -534,7 +527,6 @@ async fn compute_subtree_content_hash(
ModulesInformation { ModulesInformation {
ident_code_hash: hash, ident_code_hash: hash,
runtime_env_vars: runtime_env_vars.into_iter().cloned().collect(), runtime_env_vars: runtime_env_vars.into_iter().cloned().collect(),
references_client_component,
} }
.cell(), .cell(),
) )
......
...@@ -473,8 +473,6 @@ pub struct ActionManifestWorkerEntry<'a> { ...@@ -473,8 +473,6 @@ pub struct ActionManifestWorkerEntry<'a> {
pub struct ActionManifestWorkerEntryDurability<'a> { pub struct ActionManifestWorkerEntryDurability<'a> {
pub code_hash: &'a str, pub code_hash: &'a str,
pub runtime_env_vars: &'a [RcStr], pub runtime_env_vars: &'a [RcStr],
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub references_client_component: bool,
} }
#[derive(Serialize, Debug, Clone)] #[derive(Serialize, Debug, Clone)]
......
...@@ -75,7 +75,6 @@ type Actions = { ...@@ -75,7 +75,6 @@ type Actions = {
durability?: { durability?: {
codeHash: string codeHash: string
runtimeEnvVars: string[] runtimeEnvVars: string[]
referencesClientComponent?: boolean
} }
} }
} }
......
...@@ -8,6 +8,10 @@ import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-pref ...@@ -8,6 +8,10 @@ import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-pref
import { mightBeServerReferenceId } from '../../shared/lib/server-reference-info' import { mightBeServerReferenceId } from '../../shared/lib/server-reference-info'
import { wellKnownProperties } from '../../shared/lib/utils/reflect-utils' import { wellKnownProperties } from '../../shared/lib/utils/reflect-utils'
import { workAsyncStorage } from './work-async-storage.external' import { workAsyncStorage } from './work-async-storage.external'
import {
workUnitAsyncStorage,
type WorkUnitStore,
} from './work-unit-async-storage.external'
export interface ServerModuleMap { export interface ServerModuleMap {
readonly [name: string]: { readonly [name: string]: {
...@@ -71,6 +75,9 @@ interface ManifestsSingleton { ...@@ -71,6 +75,9 @@ interface ManifestsSingleton {
RegisteredClientReferenceManifest RegisteredClientReferenceManifest
> >
readonly proxiedClientReferenceManifest: DeepReadonly<ClientReferenceManifest> readonly proxiedClientReferenceManifest: DeepReadonly<ClientReferenceManifest>
readonly rscModuleMappingForUseCache: DeepReadonly<
ClientReferenceManifest['rscModuleMapping']
>
serverActionsManifest: DeepReadonly<ActionManifest> serverActionsManifest: DeepReadonly<ActionManifest>
serverModuleMap: ServerModuleMap serverModuleMap: ServerModuleMap
} }
...@@ -88,12 +95,38 @@ type ClientReferenceManifestMappingProp = ...@@ -88,12 +95,38 @@ type ClientReferenceManifestMappingProp =
const globalThisWithManifests = globalThis as GlobalThisWithManifests 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( function createProxiedClientReferenceManifest(
clientReferenceManifestsPerRoute: Map< clientReferenceManifestsPerRoute: Map<
string, string,
RegisteredClientReferenceManifest RegisteredClientReferenceManifest
> >
): DeepReadonly<ClientReferenceManifest> { ): Pick<
ManifestsSingleton,
'proxiedClientReferenceManifest' | 'rscModuleMappingForUseCache'
> {
const createMappingProxy = (prop: ClientReferenceManifestMappingProp) => { const createMappingProxy = (prop: ClientReferenceManifestMappingProp) => {
return new Proxy( return new Proxy(
{}, {},
...@@ -107,6 +140,40 @@ function createProxiedClientReferenceManifest( ...@@ -107,6 +140,40 @@ function createProxiedClientReferenceManifest(
)?.clientReferenceManifest )?.clientReferenceManifest
if (currentManifest?.[prop][id]) { 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] return currentManifest[prop][id]
} }
...@@ -174,7 +241,7 @@ function createProxiedClientReferenceManifest( ...@@ -174,7 +241,7 @@ function createProxiedClientReferenceManifest(
ReturnType<typeof createMappingProxy> ReturnType<typeof createMappingProxy>
>() >()
return new Proxy( const proxiedClientReferenceManifest = new Proxy(
{}, {},
{ {
get(_, prop) { get(_, prop) {
...@@ -225,6 +292,47 @@ function createProxiedClientReferenceManifest( ...@@ -225,6 +292,47 @@ function createProxiedClientReferenceManifest(
}, },
} }
) as DeepReadonly<ClientReferenceManifest> ) 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 { ...@@ -268,7 +376,6 @@ function createServerModuleMap(): ServerModuleMap {
durability?: { durability?: {
codeHash: string codeHash: string
runtimeEnvVars: readonly string[] runtimeEnvVars: readonly string[]
referencesClientComponent?: boolean
} }
} }
| undefined | undefined
...@@ -384,13 +491,13 @@ export function setManifestsSingleton({ ...@@ -384,13 +491,13 @@ export function setManifestsSingleton({
RegisteredClientReferenceManifest RegisteredClientReferenceManifest
>([[route, { page, clientReferenceManifest }]]) >([[route, { page, clientReferenceManifest }]])
const proxiedClientReferenceManifest = createProxiedClientReferenceManifest( const { proxiedClientReferenceManifest, rscModuleMappingForUseCache } =
clientReferenceManifestsPerRoute createProxiedClientReferenceManifest(clientReferenceManifestsPerRoute)
)
globalThisWithManifests[MANIFESTS_SINGLETON] = { globalThisWithManifests[MANIFESTS_SINGLETON] = {
clientReferenceManifestsPerRoute, clientReferenceManifestsPerRoute,
proxiedClientReferenceManifest, proxiedClientReferenceManifest,
rscModuleMappingForUseCache,
serverActionsManifest, serverActionsManifest,
serverModuleMap: createServerModuleMap(), serverModuleMap: createServerModuleMap(),
} }
...@@ -407,10 +514,29 @@ function getManifestsSingleton(): ManifestsSingleton { ...@@ -407,10 +514,29 @@ function getManifestsSingleton(): ManifestsSingleton {
return manifestSingleton 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> { export function getClientReferenceManifest(): DeepReadonly<ClientReferenceManifest> {
return getManifestsSingleton().proxiedClientReferenceManifest 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> { export function getServerActionsManifest(): DeepReadonly<ActionManifest> {
return getManifestsSingleton().serverActionsManifest return getManifestsSingleton().serverActionsManifest
} }
......
...@@ -12,7 +12,6 @@ import { ...@@ -12,7 +12,6 @@ import {
createTemporaryReferenceSet as createClientTemporaryReferenceSet, createTemporaryReferenceSet as createClientTemporaryReferenceSet,
} from 'react-server-dom-webpack/client' } from 'react-server-dom-webpack/client'
import { prerender } from 'react-server-dom-webpack/static' 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 type { WorkStore } from '../app-render/work-async-storage.external'
import { workAsyncStorage } 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 ...@@ -52,6 +51,7 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight
import { import {
getClientReferenceManifest, getClientReferenceManifest,
getRscModuleMappingForUseCache,
getServerActionsManifest, getServerActionsManifest,
getServerModuleMap, getServerModuleMap,
normalizeWorkerPageName, normalizeWorkerPageName,
...@@ -3611,7 +3611,7 @@ export async function cache( ...@@ -3611,7 +3611,7 @@ export async function cache(
// to be added to the consumer. Instead, we'll wait for any ClientReference to be emitted // to be added to the consumer. Instead, we'll wait for any ClientReference to be emitted
// which themselves will handle the preloading. // which themselves will handle the preloading.
moduleLoading: null, moduleLoading: null,
moduleMap: clientReferenceManifest.rscModuleMapping, moduleMap: getRscModuleMappingForUseCache(),
serverModuleMap: getServerModuleMap(), serverModuleMap: getServerModuleMap(),
} }
...@@ -3648,11 +3648,7 @@ async function computeCacheKeyImplementationPart( ...@@ -3648,11 +3648,7 @@ async function computeCacheKeyImplementationPart(
normalizeWorkerPageName(workStore.page) normalizeWorkerPageName(workStore.page)
]?.durability ]?.durability
: undefined : undefined
if ( if (durability) {
durability &&
// TODO replace this with more granular tracking: a list of all imported client components
durability.referencesClientComponent !== true
) {
// use cache is only supported in Node.js runtime. So we can use the Node.js crypto module here. // 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') const crypto = require('crypto') as typeof import('crypto')
let runtimeEnvVarStateHash = 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' ...@@ -2,9 +2,9 @@ import { Suspense } from 'react'
import { cacheLife } from 'next/cache' import { cacheLife } from 'next/cache'
import { connection } from 'next/server' import { connection } from 'next/server'
import { getDate } from '../logic' import { getDate } from '../logic'
import Client from './client' import { Client } from './client'
async function DynamicCache({ id }: { id: string }) { async function DynamicCache() {
'use cache: remote' 'use cache: remote'
cacheLife('days') cacheLife('days')
return <Client>{getDate()}</Client> return <Client>{getDate()}</Client>
...@@ -18,7 +18,7 @@ export default async function Page() { ...@@ -18,7 +18,7 @@ export default async function Page() {
return ( return (
<main> <main>
<Suspense> <Suspense>
<DynamicCache id="dynamic-cache" /> <DynamicCache />
</Suspense> </Suspense>
</main> </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