Skip to content

Commit

Permalink
Leverage temporary references on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed May 22, 2024
1 parent d4552a2 commit b6fdeeb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5331,8 +5331,8 @@ function renderToPipeableStream(model, webpackMap, options) {
};
}

function decodeReplyFromBusboy(busboyStream, webpackMap) {
var response = createResponse(webpackMap, '');
function decodeReplyFromBusboy(busboyStream, webpackMap, options) {
var response = createResponse(webpackMap, '', options ? options.temporaryReferences : undefined);
var pendingFiles = 0;
var queuedFields = [];
busboyStream.on('field', function (name, value) {
Expand Down
26 changes: 20 additions & 6 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export async function handleAction({
requestStore,
serverActions,
ctx,
temporaryReferences,
}: {
req: BaseNextRequest
res: BaseNextResponse
Expand All @@ -389,6 +390,7 @@ export async function handleAction({
requestStore: RequestStore
serverActions?: ServerActionsConfig
ctx: AppRenderContext
temporaryReferences: unknown
}): Promise<
| undefined
| {
Expand Down Expand Up @@ -578,7 +580,9 @@ export async function handleAction({
// TODO-APP: Add streaming support
const formData = await req.request.formData()
if (isFetchAction) {
bound = await decodeReply(formData, serverModuleMap)
bound = await decodeReply(formData, serverModuleMap, {
temporaryReferences: temporaryReferences,
})
} else {
const action = await decodeAction(formData, serverModuleMap)
if (typeof action === 'function') {
Expand Down Expand Up @@ -617,9 +621,13 @@ export async function handleAction({

if (isURLEncodedAction) {
const formData = formDataFromSearchQueryString(actionData)
bound = await decodeReply(formData, serverModuleMap)
bound = await decodeReply(formData, serverModuleMap, {
temporaryReferences: temporaryReferences,
})
} else {
bound = await decodeReply(actionData, serverModuleMap)
bound = await decodeReply(actionData, serverModuleMap, {
temporaryReferences: temporaryReferences,
})
}
}
} else if (
Expand Down Expand Up @@ -681,7 +689,9 @@ export async function handleAction({

body.pipe(busboy)

bound = await decodeReplyFromBusboy(busboy, serverModuleMap)
bound = await decodeReplyFromBusboy(busboy, serverModuleMap, {
temporaryReferences: temporaryReferences,
})
} else {
// React doesn't yet publish a busboy version of decodeAction
// so we polyfill the parsing of FormData.
Expand Down Expand Up @@ -737,9 +747,13 @@ export async function handleAction({

if (isURLEncodedAction) {
const formData = formDataFromSearchQueryString(actionData)
bound = await decodeReply(formData, serverModuleMap)
bound = await decodeReply(formData, serverModuleMap, {
temporaryReferences: temporaryReferences,
})
} else {
bound = await decodeReply(actionData, serverModuleMap)
bound = await decodeReply(actionData, serverModuleMap, {
temporaryReferences: temporaryReferences,
})
}
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,8 @@ async function renderToHTMLOrFlightImpl(

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

const temporaryReferences = ComponentMod.createTemporaryReferenceSet()

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

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

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

let formState: null | any = null
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/app-render/entry-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
decodeReply,
decodeAction,
decodeFormState,
createTemporaryReferenceSet,
} from 'react-server-dom-webpack/server.edge'

import AppRouter from '../../client/components/app-router'
Expand Down

0 comments on commit b6fdeeb

Please sign in to comment.