Skip to content

Commit

Permalink
Clarify that moduleLoading must always remain null but the moduleMap …
Browse files Browse the repository at this point in the history
…should be filled in

Both in encryption and use-cache-wrapper.
  • Loading branch information
sebmarkbage committed Sep 25, 2024
1 parent e1b1b8d commit fc68321
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
20 changes: 14 additions & 6 deletions packages/next/src/server/app-render/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
stringToUint8Array,
} from './encryption-utils'

import type { ManifestNode } from '../../build/webpack/plugins/flight-manifest-plugin'

const textEncoder = new TextEncoder()
const textDecoder = new TextDecoder()

Expand Down Expand Up @@ -97,6 +99,13 @@ export async function decryptActionBoundArgs(
// Decrypt the serialized string with the action id as the salt.
const decryped = await decodeActionBoundArg(actionId, await encrypted)

// TODO: We can't use the client reference manifest to resolve the modules
// on the server side - instead they need to be recovered as the module
// references (proxies) again.
// For now, we'll just use an empty module map.
const ssrModuleMap: {
[moduleExport: string]: ManifestNode
} = {}
// Using Flight to deserialize the args from the string.
const deserialized = await createFromReadableStream(
new ReadableStream({
Expand All @@ -107,12 +116,11 @@ export async function decryptActionBoundArgs(
}),
{
ssrManifest: {
// TODO: We can't use the client reference manifest to resolve the modules
// on the server side - instead they need to be recovered as the module
// references (proxies) again.
// For now, we'll just use an empty module map.
moduleLoading: {},
moduleMap: {},
// moduleLoading must be null because we don't want to trigger preloads of ClientReferences
// to be added to the current execution. Instead, we'll wait for any ClientReference
// to be emitted which themselves will handle the preloading.
moduleLoading: null,
moduleMap: ssrModuleMap,
},
}
)
Expand Down
33 changes: 25 additions & 8 deletions packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
getServerModuleMap,
} from '../app-render/encryption-utils'

import type { ManifestNode } from '../../build/webpack/plugins/flight-manifest-plugin'

type CacheEntry = {
value: ReadableStream
stale: boolean
Expand Down Expand Up @@ -68,11 +70,6 @@ cacheHandlerMap.set('default', {
shouldRevalidateStale: false,
})

const ssrManifest: any = {
moduleMap: {},
moduleLoading: null,
} // TODO

// TODO: Consider moving this another module that is guaranteed to be required in a safe scope.
const runInCleanSnapshot = createSnapshot()

Expand All @@ -85,9 +82,13 @@ async function generateCacheEntry(
): Promise<ReadableStream> {
const temporaryReferences = createServerTemporaryReferenceSet()

const [, args] = await decodeReply(encodedArguments, getServerModuleMap(), {
temporaryReferences,
})
const [, args] = await decodeReply<any[]>(
encodedArguments,
getServerModuleMap(),
{
temporaryReferences,
}
)

// Invoke the inner function to load a new result.
const result = fn.apply(null, args)
Expand Down Expand Up @@ -245,6 +246,22 @@ export function cache(kind: string, id: string, fn: any) {
// server terminal. Once while generating the cache entry and once when replaying it on
// the server, which is required to pick it up for replaying again on the client.
const replayConsoleLogs = true

// TODO: We can't use the client reference manifest to resolve the modules
// on the server side - instead they need to be recovered as the module
// references (proxies) again.
// For now, we'll just use an empty module map.
const ssrModuleMap: {
[moduleExport: string]: ManifestNode
} = {}

const ssrManifest = {
// moduleLoading must be null because we don't want to trigger preloads of ClientReferences
// 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: ssrModuleMap,
}
return createFromReadableStream(stream, {
ssrManifest,
temporaryReferences,
Expand Down

0 comments on commit fc68321

Please sign in to comment.