Unverified Commit 3265c95d authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Fix race condition when setting client reference manifests (#71741)

Similar to how #71669 fixed a race condition when setting the manifests
singleton regarding the server action manifests, this PR fixes the same
race condition for the client reference manifests.
parent 627a5ffb
...@@ -17,6 +17,7 @@ const rscServerManifest = maybeJSONParse(self.__RSC_SERVER_MANIFEST) ...@@ -17,6 +17,7 @@ const rscServerManifest = maybeJSONParse(self.__RSC_SERVER_MANIFEST)
if (rscManifest && rscServerManifest) { if (rscManifest && rscServerManifest) {
setReferenceManifestsSingleton({ setReferenceManifestsSingleton({
page: 'VAR_PAGE',
clientReferenceManifest: rscManifest, clientReferenceManifest: rscManifest,
serverActionsManifest: rscServerManifest, serverActionsManifest: rscServerManifest,
serverModuleMap: createServerModuleMap({ serverModuleMap: createServerModuleMap({
......
...@@ -56,6 +56,7 @@ const interceptionRouteRewrites = ...@@ -56,6 +56,7 @@ const interceptionRouteRewrites =
if (rscManifest && rscServerManifest) { if (rscManifest && rscServerManifest) {
setReferenceManifestsSingleton({ setReferenceManifestsSingleton({
page: 'VAR_PAGE',
clientReferenceManifest: rscManifest, clientReferenceManifest: rscManifest,
serverActionsManifest: rscServerManifest, serverActionsManifest: rscServerManifest,
serverModuleMap: createServerModuleMap({ serverModuleMap: createServerModuleMap({
......
...@@ -72,12 +72,21 @@ export interface ManifestNode { ...@@ -72,12 +72,21 @@ export interface ManifestNode {
} }
} }
export type ClientReferenceManifest = { export interface ClientReferenceManifestForRsc {
clientModules: ManifestNode
rscModuleMapping: {
[moduleId: string]: ManifestNode
}
edgeRscModuleMapping: {
[moduleId: string]: ManifestNode
}
}
export interface ClientReferenceManifest extends ClientReferenceManifestForRsc {
readonly moduleLoading: { readonly moduleLoading: {
prefix: string prefix: string
crossOrigin: string | null crossOrigin: string | null
} }
clientModules: ManifestNode
ssrModuleMapping: { ssrModuleMapping: {
[moduleId: string]: ManifestNode [moduleId: string]: ManifestNode
} }
...@@ -90,12 +99,6 @@ export type ClientReferenceManifest = { ...@@ -90,12 +99,6 @@ export type ClientReferenceManifest = {
entryJSFiles?: { entryJSFiles?: {
[entry: string]: string[] [entry: string]: string[]
} }
rscModuleMapping: {
[moduleId: string]: ManifestNode
}
edgeRscModuleMapping: {
[moduleId: string]: ManifestNode
}
} }
function getAppPathRequiredChunks( function getAppPathRequiredChunks(
......
...@@ -1060,6 +1060,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1060,6 +1060,7 @@ async function renderToHTMLOrFlightImpl(
const serverModuleMap = createServerModuleMap({ serverActionsManifest }) const serverModuleMap = createServerModuleMap({ serverActionsManifest })
setReferenceManifestsSingleton({ setReferenceManifestsSingleton({
page: workStore.page,
clientReferenceManifest, clientReferenceManifest,
serverActionsManifest, serverActionsManifest,
serverModuleMap, serverModuleMap,
......
import type { ActionManifest } from '../../build/webpack/plugins/flight-client-entry-plugin' import type { ActionManifest } from '../../build/webpack/plugins/flight-client-entry-plugin'
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin' import type {
ClientReferenceManifest,
ClientReferenceManifestForRsc,
} from '../../build/webpack/plugins/flight-manifest-plugin'
import type { DeepReadonly } from '../../shared/lib/deep-readonly' import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import { InvariantError } from '../../shared/lib/invariant-error'
import { workAsyncStorage } from './work-async-storage.external'
let __next_loaded_action_key: CryptoKey let __next_loaded_action_key: CryptoKey
...@@ -64,10 +69,12 @@ const SERVER_ACTION_MANIFESTS_SINGLETON = Symbol.for( ...@@ -64,10 +69,12 @@ const SERVER_ACTION_MANIFESTS_SINGLETON = Symbol.for(
) )
export function setReferenceManifestsSingleton({ export function setReferenceManifestsSingleton({
page,
clientReferenceManifest, clientReferenceManifest,
serverActionsManifest, serverActionsManifest,
serverModuleMap, serverModuleMap,
}: { }: {
page: string
clientReferenceManifest: DeepReadonly<ClientReferenceManifest> clientReferenceManifest: DeepReadonly<ClientReferenceManifest>
serverActionsManifest: DeepReadonly<ActionManifest> serverActionsManifest: DeepReadonly<ActionManifest>
serverModuleMap: { serverModuleMap: {
...@@ -78,9 +85,19 @@ export function setReferenceManifestsSingleton({ ...@@ -78,9 +85,19 @@ export function setReferenceManifestsSingleton({
} }
} }
}) { }) {
// @ts-ignore // @ts-expect-error
const clientReferenceManifestsPerPage = globalThis[
SERVER_ACTION_MANIFESTS_SINGLETON
]?.clientReferenceManifestsPerPage as
| undefined
| DeepReadonly<Record<string, ClientReferenceManifest>>
// @ts-expect-error
globalThis[SERVER_ACTION_MANIFESTS_SINGLETON] = { globalThis[SERVER_ACTION_MANIFESTS_SINGLETON] = {
clientReferenceManifest, clientReferenceManifestsPerPage: {
...clientReferenceManifestsPerPage,
[normalizePage(page)]: clientReferenceManifest,
},
serverActionsManifest, serverActionsManifest,
serverModuleMap, serverModuleMap,
} }
...@@ -100,29 +117,49 @@ export function getServerModuleMap() { ...@@ -100,29 +117,49 @@ export function getServerModuleMap() {
} }
if (!serverActionsManifestSingleton) { if (!serverActionsManifestSingleton) {
throw new Error( throw new InvariantError('Missing manifest for Server Actions.')
'Missing manifest for Server Actions. This is a bug in Next.js'
)
} }
return serverActionsManifestSingleton.serverModuleMap return serverActionsManifestSingleton.serverModuleMap
} }
export function getClientReferenceManifestSingleton() { export function getClientReferenceManifestForRsc(): DeepReadonly<ClientReferenceManifestForRsc> {
const serverActionsManifestSingleton = (globalThis as any)[ const serverActionsManifestSingleton = (globalThis as any)[
SERVER_ACTION_MANIFESTS_SINGLETON SERVER_ACTION_MANIFESTS_SINGLETON
] as { ] as {
clientReferenceManifest: DeepReadonly<ClientReferenceManifest> clientReferenceManifestsPerPage: DeepReadonly<
serverActionsManifest: DeepReadonly<ActionManifest> Record<string, ClientReferenceManifest>
>
} }
if (!serverActionsManifestSingleton) { if (!serverActionsManifestSingleton) {
throw new Error( throw new InvariantError('Missing manifest for Server Actions.')
'Missing manifest for Server Actions. This is a bug in Next.js' }
)
const { clientReferenceManifestsPerPage } = serverActionsManifestSingleton
const workStore = workAsyncStorage.getStore()
if (!workStore) {
// If there's no work store defined, we can assume that a client reference
// manifest is needed during module evaluation, e.g. to create a server
// action using a higher-order function. This might also use client
// components which need to be serialized by Flight, and therefore client
// references need to be resolvable. To make this work, we're returning a
// merged manifest across all pages. This is fine as long as the module IDs
// are not page specific, which they are not for Webpack. TODO: Fix this in
// Turbopack.
return mergeClientReferenceManifests(clientReferenceManifestsPerPage)
}
const page = normalizePage(workStore.page)
const clientReferenceManifest = clientReferenceManifestsPerPage[page]
if (!clientReferenceManifest) {
throw new InvariantError(`Missing Client Reference Manifest for ${page}.`)
} }
return serverActionsManifestSingleton.clientReferenceManifest return clientReferenceManifest
} }
export async function getActionEncryptionKey() { export async function getActionEncryptionKey() {
...@@ -133,14 +170,11 @@ export async function getActionEncryptionKey() { ...@@ -133,14 +170,11 @@ export async function getActionEncryptionKey() {
const serverActionsManifestSingleton = (globalThis as any)[ const serverActionsManifestSingleton = (globalThis as any)[
SERVER_ACTION_MANIFESTS_SINGLETON SERVER_ACTION_MANIFESTS_SINGLETON
] as { ] as {
clientReferenceManifest: DeepReadonly<ClientReferenceManifest>
serverActionsManifest: DeepReadonly<ActionManifest> serverActionsManifest: DeepReadonly<ActionManifest>
} }
if (!serverActionsManifestSingleton) { if (!serverActionsManifestSingleton) {
throw new Error( throw new InvariantError('Missing manifest for Server Actions.')
'Missing manifest for Server Actions. This is a bug in Next.js'
)
} }
const rawKey = const rawKey =
...@@ -148,7 +182,7 @@ export async function getActionEncryptionKey() { ...@@ -148,7 +182,7 @@ export async function getActionEncryptionKey() {
serverActionsManifestSingleton.serverActionsManifest.encryptionKey serverActionsManifestSingleton.serverActionsManifest.encryptionKey
if (rawKey === undefined) { if (rawKey === undefined) {
throw new Error('Missing encryption key for Server Actions') throw new InvariantError('Missing encryption key for Server Actions')
} }
__next_loaded_action_key = await crypto.subtle.importKey( __next_loaded_action_key = await crypto.subtle.importKey(
...@@ -161,3 +195,40 @@ export async function getActionEncryptionKey() { ...@@ -161,3 +195,40 @@ export async function getActionEncryptionKey() {
return __next_loaded_action_key return __next_loaded_action_key
} }
function normalizePage(page: string): string {
return page.replace(/\/(page|route)$/, '')
}
function mergeClientReferenceManifests(
clientReferenceManifestsPerPage: DeepReadonly<
Record<string, ClientReferenceManifest>
>
): ClientReferenceManifestForRsc {
const clientReferenceManifests = Object.values(
clientReferenceManifestsPerPage as Record<string, ClientReferenceManifest>
)
const mergedClientReferenceManifest: ClientReferenceManifestForRsc = {
clientModules: {},
edgeRscModuleMapping: {},
rscModuleMapping: {},
}
for (const clientReferenceManifest of clientReferenceManifests) {
mergedClientReferenceManifest.clientModules = {
...mergedClientReferenceManifest.clientModules,
...clientReferenceManifest.clientModules,
}
mergedClientReferenceManifest.edgeRscModuleMapping = {
...mergedClientReferenceManifest.edgeRscModuleMapping,
...clientReferenceManifest.edgeRscModuleMapping,
}
mergedClientReferenceManifest.rscModuleMapping = {
...mergedClientReferenceManifest.rscModuleMapping,
...clientReferenceManifest.rscModuleMapping,
}
}
return mergedClientReferenceManifest
}
...@@ -12,7 +12,7 @@ import { ...@@ -12,7 +12,7 @@ import {
decrypt, decrypt,
encrypt, encrypt,
getActionEncryptionKey, getActionEncryptionKey,
getClientReferenceManifestSingleton, getClientReferenceManifestForRsc,
getServerModuleMap, getServerModuleMap,
stringToUint8Array, stringToUint8Array,
} from './encryption-utils' } from './encryption-utils'
...@@ -70,11 +70,11 @@ async function encodeActionBoundArg(actionId: string, arg: string) { ...@@ -70,11 +70,11 @@ async function encodeActionBoundArg(actionId: string, arg: string) {
// Encrypts the action's bound args into a string. // Encrypts the action's bound args into a string.
export async function encryptActionBoundArgs(actionId: string, args: any[]) { export async function encryptActionBoundArgs(actionId: string, args: any[]) {
const clientReferenceManifestSingleton = getClientReferenceManifestSingleton() const { clientModules } = getClientReferenceManifestForRsc()
// Using Flight to serialize the args into a string. // Using Flight to serialize the args into a string.
const serialized = await streamToString( const serialized = await streamToString(
renderToReadableStream(args, clientReferenceManifestSingleton.clientModules) renderToReadableStream(args, clientModules)
) )
// Encrypt the serialized string with the action id as the salt. // Encrypt the serialized string with the action id as the salt.
...@@ -90,16 +90,17 @@ export async function decryptActionBoundArgs( ...@@ -90,16 +90,17 @@ export async function decryptActionBoundArgs(
actionId: string, actionId: string,
encrypted: Promise<string> encrypted: Promise<string>
) { ) {
const clientReferenceManifestSingleton = getClientReferenceManifestSingleton() const { edgeRscModuleMapping, rscModuleMapping } =
getClientReferenceManifestForRsc()
// Decrypt the serialized string with the action id as the salt. // Decrypt the serialized string with the action id as the salt.
const decryped = await decodeActionBoundArg(actionId, await encrypted) const decrypted = await decodeActionBoundArg(actionId, await encrypted)
// Using Flight to deserialize the args from the string. // Using Flight to deserialize the args from the string.
const deserialized = await createFromReadableStream( const deserialized = await createFromReadableStream(
new ReadableStream({ new ReadableStream({
start(controller) { start(controller) {
controller.enqueue(textEncoder.encode(decryped)) controller.enqueue(textEncoder.encode(decrypted))
controller.close() controller.close()
}, },
}), }),
...@@ -109,9 +110,7 @@ export async function decryptActionBoundArgs( ...@@ -109,9 +110,7 @@ export async function decryptActionBoundArgs(
// to be added to the current execution. Instead, we'll wait for any ClientReference // to be added to the current execution. Instead, we'll wait for any ClientReference
// to be emitted which themselves will handle the preloading. // to be emitted which themselves will handle the preloading.
moduleLoading: null, moduleLoading: null,
moduleMap: isEdgeRuntime moduleMap: isEdgeRuntime ? edgeRscModuleMapping : rscModuleMapping,
? clientReferenceManifestSingleton.edgeRscModuleMapping
: clientReferenceManifestSingleton.rscModuleMapping,
serverModuleMap: getServerModuleMap(), serverModuleMap: getServerModuleMap(),
}, },
} }
......
...@@ -177,6 +177,7 @@ async function loadComponentsImpl<N = any>({ ...@@ -177,6 +177,7 @@ async function loadComponentsImpl<N = any>({
// to them at the top level of the page module. // to them at the top level of the page module.
if (serverActionsManifest && clientReferenceManifest) { if (serverActionsManifest && clientReferenceManifest) {
setReferenceManifestsSingleton({ setReferenceManifestsSingleton({
page,
clientReferenceManifest, clientReferenceManifest,
serverActionsManifest, serverActionsManifest,
serverModuleMap: createServerModuleMap({ serverModuleMap: createServerModuleMap({
......
...@@ -25,10 +25,10 @@ import { makeHangingPromise } from '../dynamic-rendering-utils' ...@@ -25,10 +25,10 @@ import { makeHangingPromise } from '../dynamic-rendering-utils'
import { cacheScopeAsyncLocalStorage } from '../async-storage/cache-scope.external' import { cacheScopeAsyncLocalStorage } from '../async-storage/cache-scope.external'
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin' import type { ClientReferenceManifestForRsc } from '../../build/webpack/plugins/flight-manifest-plugin'
import { import {
getClientReferenceManifestSingleton, getClientReferenceManifestForRsc,
getServerModuleMap, getServerModuleMap,
} from '../app-render/encryption-utils' } from '../app-render/encryption-utils'
import type { CacheScopeStore } from '../async-storage/cache-scope.external' import type { CacheScopeStore } from '../async-storage/cache-scope.external'
...@@ -66,7 +66,7 @@ function generateCacheEntry( ...@@ -66,7 +66,7 @@ function generateCacheEntry(
workStore: WorkStore, workStore: WorkStore,
outerWorkUnitStore: WorkUnitStore | undefined, outerWorkUnitStore: WorkUnitStore | undefined,
cacheScope: undefined | CacheScopeStore, cacheScope: undefined | CacheScopeStore,
clientReferenceManifest: DeepReadonly<ClientReferenceManifest>, clientReferenceManifest: DeepReadonly<ClientReferenceManifestForRsc>,
encodedArguments: FormData | string, encodedArguments: FormData | string,
fn: any fn: any
): Promise<[ReadableStream, Promise<CacheEntry>]> { ): Promise<[ReadableStream, Promise<CacheEntry>]> {
...@@ -90,7 +90,7 @@ function generateCacheEntryWithRestoredWorkStore( ...@@ -90,7 +90,7 @@ function generateCacheEntryWithRestoredWorkStore(
workStore: WorkStore, workStore: WorkStore,
outerWorkUnitStore: WorkUnitStore | undefined, outerWorkUnitStore: WorkUnitStore | undefined,
cacheScope: undefined | CacheScopeStore, cacheScope: undefined | CacheScopeStore,
clientReferenceManifest: DeepReadonly<ClientReferenceManifest>, clientReferenceManifest: DeepReadonly<ClientReferenceManifestForRsc>,
encodedArguments: FormData | string, encodedArguments: FormData | string,
fn: any fn: any
) { ) {
...@@ -128,7 +128,7 @@ function generateCacheEntryWithRestoredWorkStore( ...@@ -128,7 +128,7 @@ function generateCacheEntryWithRestoredWorkStore(
function generateCacheEntryWithCacheContext( function generateCacheEntryWithCacheContext(
workStore: WorkStore, workStore: WorkStore,
outerWorkUnitStore: WorkUnitStore | undefined, outerWorkUnitStore: WorkUnitStore | undefined,
clientReferenceManifest: DeepReadonly<ClientReferenceManifest>, clientReferenceManifest: DeepReadonly<ClientReferenceManifestForRsc>,
encodedArguments: FormData | string, encodedArguments: FormData | string,
fn: any fn: any
) { ) {
...@@ -298,7 +298,7 @@ async function generateCacheEntryImpl( ...@@ -298,7 +298,7 @@ async function generateCacheEntryImpl(
workStore: WorkStore, workStore: WorkStore,
outerWorkUnitStore: WorkUnitStore | undefined, outerWorkUnitStore: WorkUnitStore | undefined,
innerCacheStore: UseCacheStore, innerCacheStore: UseCacheStore,
clientReferenceManifest: DeepReadonly<ClientReferenceManifest>, clientReferenceManifest: DeepReadonly<ClientReferenceManifestForRsc>,
encodedArguments: FormData | string, encodedArguments: FormData | string,
fn: any fn: any
): Promise<[ReadableStream, Promise<CacheEntry>]> { ): Promise<[ReadableStream, Promise<CacheEntry>]> {
...@@ -455,7 +455,7 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -455,7 +455,7 @@ export function cache(kind: string, id: string, fn: any) {
// Get the clientReferenceManifest while we're still in the outer Context. // Get the clientReferenceManifest while we're still in the outer Context.
// In case getClientReferenceManifestSingleton is implemented using AsyncLocalStorage. // In case getClientReferenceManifestSingleton is implemented using AsyncLocalStorage.
const clientReferenceManifest = getClientReferenceManifestSingleton() const clientReferenceManifest = getClientReferenceManifestForRsc()
// Because the Action ID is not yet unique per implementation of that Action we can't // Because the Action ID is not yet unique per implementation of that Action we can't
// safely reuse the results across builds yet. In the meantime we add the buildId to the // safely reuse the results across builds yet. In the meantime we add the buildId to the
......
...@@ -1587,6 +1587,16 @@ describe('app-dir action handling', () => { ...@@ -1587,6 +1587,16 @@ describe('app-dir action handling', () => {
expect(html).not.toContain('qwerty123') expect(html).not.toContain('qwerty123')
expect(html).not.toContain('some-module-level-encryption-value') expect(html).not.toContain('some-module-level-encryption-value')
}) })
it('should be able to resolve other server actions and client components', async () => {
const browser = await next.browser('/encryption')
expect(await browser.elementByCss('p').text()).toBe('initial')
await browser.elementByCss('button').click()
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('hello from client')
})
})
}) })
describe('redirects', () => { describe('redirects', () => {
......
'use client'
export function Client() {
return 'hello from client'
}
'use client'
import { useActionState } from 'react'
export default function Form({ action }) {
const [result, formAction] = useActionState(action, 'initial')
return (
<form action={formAction}>
<button>Submit</button>
<p>{result}</p>
</form>
)
}
import { Client } from './client'
import Form from './form'
async function otherAction() { async function otherAction() {
'use server' 'use server'
return 'hi' return 'hi'
} }
// Test top-level encryption (happens during the module load phase) // Test top-level encryption (happens during the module load phase)
function wrapAction(value, action) { function wrapAction(value, action, element) {
return async function () { return async function () {
'use server' 'use server'
const v = await action() const v = await action()
if (v === 'hi') { if (v === 'hi') {
console.log(value) console.log(value)
} }
return element
} }
} }
const action = wrapAction('some-module-level-encryption-value', otherAction) const action = wrapAction(
'some-module-level-encryption-value',
otherAction,
<Client />
)
// Test runtime encryption (happens during the rendering phase) // Test runtime encryption (happens during the rendering phase)
export default function Page() { export default function Page() {
const secret = 'my password is qwerty123' const secret = 'my password is qwerty123'
return ( return (
<form <Form
action={async () => { action={async () => {
'use server' 'use server'
console.log(secret) console.log(secret)
await action() return action()
return 'success'
}} }}
> />
<button type="submit" id="submit">
Submit
</button>
</form>
) )
} }
import { connection } from 'next/server'
export default async function DynamicLayout({
children,
}: {
children: React.ReactNode
}) {
// TODO: This is a workaround for Turbopack. Figure out why this fails during
// prerendering with:
// TypeError: Cannot read properties of undefined (reading 'Form')
await connection()
return children
}
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