Skip to content

Commit

Permalink
Only use temporary references where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed May 22, 2024
1 parent 39b0031 commit 8ad8173
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
32 changes: 23 additions & 9 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ export async function handleAction({
requestStore,
serverActions,
ctx,
temporaryReferences,
}: {
req: BaseNextRequest
res: BaseNextResponse
Expand All @@ -390,7 +389,6 @@ export async function handleAction({
requestStore: RequestStore
serverActions?: ServerActionsConfig
ctx: AppRenderContext
temporaryReferences: unknown
}): Promise<
| undefined
| {
Expand All @@ -400,6 +398,7 @@ export async function handleAction({
type: 'done'
result: RenderResult | undefined
formState?: any
temporaryReferences: unknown
}
> {
const contentType = req.headers['content-type']
Expand Down Expand Up @@ -515,6 +514,7 @@ export async function handleAction({
// if the page was not revalidated, we can skip the rendering the flight tree
skipFlight: !staticGenerationStore.pathWasRevalidated,
}),
temporaryReferences: undefined,
}
}

Expand Down Expand Up @@ -556,6 +556,7 @@ export async function handleAction({
ctx.renderOpts.basePath,
staticGenerationStore
),
temporaryReferences: undefined,
}
}
}
Expand All @@ -569,10 +570,16 @@ export async function handleAction({
isWebNextRequest(req)
) {
// Use react-server-dom-webpack/server.edge
const { decodeReply, decodeAction, decodeFormState } = ComponentMod
const {
createTemporaryReferenceSet,
decodeReply,
decodeAction,
decodeFormState,
} = ComponentMod
if (!req.body) {
throw new Error('invariant: Missing request body.')
}
const temporaryReferences = createTemporaryReferenceSet()

// TODO: add body limit

Expand All @@ -581,7 +588,7 @@ export async function handleAction({
const formData = await req.request.formData()
if (isFetchAction) {
bound = await decodeReply(formData, serverModuleMap, {
temporaryReferences: temporaryReferences,
temporaryReferences,
})
} else {
const action = await decodeAction(formData, serverModuleMap)
Expand Down Expand Up @@ -622,11 +629,11 @@ export async function handleAction({
if (isURLEncodedAction) {
const formData = formDataFromSearchQueryString(actionData)
bound = await decodeReply(formData, serverModuleMap, {
temporaryReferences: temporaryReferences,
temporaryReferences,
})
} else {
bound = await decodeReply(actionData, serverModuleMap, {
temporaryReferences: temporaryReferences,
temporaryReferences,
})
}
}
Expand All @@ -638,11 +645,13 @@ export async function handleAction({
) {
// Use react-server-dom-webpack/server.node which supports streaming
const {
createTemporaryReferenceSet,
decodeReply,
decodeReplyFromBusboy,
decodeAction,
decodeFormState,
} = require(`./react-server.node`)
const temporaryReferences = createTemporaryReferenceSet()

const { Transform } =
require('node:stream') as typeof import('node:stream')
Expand Down Expand Up @@ -690,7 +699,7 @@ export async function handleAction({
body.pipe(busboy)

bound = await decodeReplyFromBusboy(busboy, serverModuleMap, {
temporaryReferences: temporaryReferences,
temporaryReferences,
})
} else {
// React doesn't yet publish a busboy version of decodeAction
Expand Down Expand Up @@ -748,11 +757,11 @@ export async function handleAction({
if (isURLEncodedAction) {
const formData = formDataFromSearchQueryString(actionData)
bound = await decodeReply(formData, serverModuleMap, {
temporaryReferences: temporaryReferences,
temporaryReferences,
})
} else {
bound = await decodeReply(actionData, serverModuleMap, {
temporaryReferences: temporaryReferences,
temporaryReferences,
})
}
}
Expand Down Expand Up @@ -813,6 +822,7 @@ export async function handleAction({
type: 'done',
result: actionResult,
formState,
temporaryReferences: undefined,
}
} catch (err) {
if (isRedirectError(err)) {
Expand All @@ -839,6 +849,7 @@ export async function handleAction({
ctx.renderOpts.basePath,
staticGenerationStore
),
temporaryReferences: undefined,
}
}

Expand All @@ -856,6 +867,7 @@ export async function handleAction({
return {
type: 'done',
result: RenderResult.fromStatic(''),
temporaryReferences: undefined,
}
} else if (isNotFoundError(err)) {
res.statusCode = 404
Expand Down Expand Up @@ -883,6 +895,7 @@ export async function handleAction({
actionResult: promise,
asNotFound: true,
}),
temporaryReferences: undefined,
}
}
return {
Expand Down Expand Up @@ -917,6 +930,7 @@ export async function handleAction({
skipFlight:
!staticGenerationStore.pathWasRevalidated || actionWasForwarded,
}),
temporaryReferences: undefined,
}
}

Expand Down
11 changes: 6 additions & 5 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ type RenderToStreamOptions = {
asNotFound: boolean
tree: LoaderTree
formState: any
temporaryReferences: unknown
}

/**
Expand Down Expand Up @@ -906,8 +907,6 @@ async function renderToHTMLOrFlightImpl(

getTracer().getRootSpanAttributes()?.set('next.route', pagePath)

const temporaryReferences = ComponentMod.createTemporaryReferenceSet()

const renderToStream = getTracer().wrap(
AppRenderSpan.getBodyResult,
{
Expand Down Expand Up @@ -961,7 +960,6 @@ async function renderToHTMLOrFlightImpl(
{
onError: serverComponentsErrorHandler,
nonce,
temporaryReferences: temporaryReferences,
}
)

Expand Down Expand Up @@ -1294,7 +1292,6 @@ async function renderToHTMLOrFlightImpl(
{
onError: serverComponentsErrorHandler,
nonce,
temporaryReferences: temporaryReferences,
}
)

Expand Down Expand Up @@ -1368,7 +1365,6 @@ async function renderToHTMLOrFlightImpl(
requestStore,
serverActions,
ctx,
temporaryReferences,
})

let formState: null | any = null
Expand All @@ -1379,6 +1375,7 @@ async function renderToHTMLOrFlightImpl(
asNotFound: true,
tree: notFoundLoaderTree,
formState,
temporaryReferences: undefined,
})

return new RenderResult(response.stream, { metadata })
Expand All @@ -1400,6 +1397,10 @@ async function renderToHTMLOrFlightImpl(
asNotFound: isNotFoundPath,
tree: loaderTree,
formState,
temporaryReferences:
actionRequestResult !== undefined
? actionRequestResult.temporaryReferences
: undefined,
})

// If we have pending revalidates, wait until they are all resolved.
Expand Down
5 changes: 1 addition & 4 deletions packages/next/src/server/app-render/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ export async function decryptActionBoundArgs(
// This extra step ensures that the server references are recovered.
const serverModuleMap = getServerModuleMap()
const transformed = await decodeReply(
await encodeReply(deserialized, {
// TODO: How is decryptActionBoundArgs used? Do we need to support temporary references here?
temporaryReferences: undefined,
}),
await encodeReply(deserialized),
serverModuleMap
)

Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/app-render/react-server.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// eslint-disable-next-line import/no-extraneous-dependencies
export {
createTemporaryReferenceSet,
decodeReply,
decodeReplyFromBusboy,
decodeAction,
Expand Down

0 comments on commit 8ad8173

Please sign in to comment.