Skip to content

Commit d9c8add

Browse files
committed
Only use temporary references where necessary
1 parent 39b0031 commit d9c8add

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

packages/next/src/server/app-render/action-handler.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ export async function handleAction({
379379
requestStore,
380380
serverActions,
381381
ctx,
382-
temporaryReferences,
383382
}: {
384383
req: BaseNextRequest
385384
res: BaseNextResponse
@@ -390,7 +389,6 @@ export async function handleAction({
390389
requestStore: RequestStore
391390
serverActions?: ServerActionsConfig
392391
ctx: AppRenderContext
393-
temporaryReferences: unknown
394392
}): Promise<
395393
| undefined
396394
| {
@@ -400,6 +398,7 @@ export async function handleAction({
400398
type: 'done'
401399
result: RenderResult | undefined
402400
formState?: any
401+
temporaryReferences: unknown
403402
}
404403
> {
405404
const contentType = req.headers['content-type']
@@ -515,6 +514,7 @@ export async function handleAction({
515514
// if the page was not revalidated, we can skip the rendering the flight tree
516515
skipFlight: !staticGenerationStore.pathWasRevalidated,
517516
}),
517+
temporaryReferences: undefined,
518518
}
519519
}
520520

@@ -556,6 +556,7 @@ export async function handleAction({
556556
ctx.renderOpts.basePath,
557557
staticGenerationStore
558558
),
559+
temporaryReferences: undefined,
559560
}
560561
}
561562
}
@@ -569,10 +570,16 @@ export async function handleAction({
569570
isWebNextRequest(req)
570571
) {
571572
// Use react-server-dom-webpack/server.edge
572-
const { decodeReply, decodeAction, decodeFormState } = ComponentMod
573+
const {
574+
createTemporaryReferenceSet,
575+
decodeReply,
576+
decodeAction,
577+
decodeFormState,
578+
} = ComponentMod
573579
if (!req.body) {
574580
throw new Error('invariant: Missing request body.')
575581
}
582+
const temporaryReferences = createTemporaryReferenceSet()
576583

577584
// TODO: add body limit
578585

@@ -581,7 +588,7 @@ export async function handleAction({
581588
const formData = await req.request.formData()
582589
if (isFetchAction) {
583590
bound = await decodeReply(formData, serverModuleMap, {
584-
temporaryReferences: temporaryReferences,
591+
temporaryReferences,
585592
})
586593
} else {
587594
const action = await decodeAction(formData, serverModuleMap)
@@ -622,11 +629,11 @@ export async function handleAction({
622629
if (isURLEncodedAction) {
623630
const formData = formDataFromSearchQueryString(actionData)
624631
bound = await decodeReply(formData, serverModuleMap, {
625-
temporaryReferences: temporaryReferences,
632+
temporaryReferences,
626633
})
627634
} else {
628635
bound = await decodeReply(actionData, serverModuleMap, {
629-
temporaryReferences: temporaryReferences,
636+
temporaryReferences,
630637
})
631638
}
632639
}
@@ -638,11 +645,13 @@ export async function handleAction({
638645
) {
639646
// Use react-server-dom-webpack/server.node which supports streaming
640647
const {
648+
createTemporaryReferenceSet,
641649
decodeReply,
642650
decodeReplyFromBusboy,
643651
decodeAction,
644652
decodeFormState,
645653
} = require(`./react-server.node`)
654+
const temporaryReferences = createTemporaryReferenceSet()
646655

647656
const { Transform } =
648657
require('node:stream') as typeof import('node:stream')
@@ -748,11 +757,11 @@ export async function handleAction({
748757
if (isURLEncodedAction) {
749758
const formData = formDataFromSearchQueryString(actionData)
750759
bound = await decodeReply(formData, serverModuleMap, {
751-
temporaryReferences: temporaryReferences,
760+
temporaryReferences,
752761
})
753762
} else {
754763
bound = await decodeReply(actionData, serverModuleMap, {
755-
temporaryReferences: temporaryReferences,
764+
temporaryReferences,
756765
})
757766
}
758767
}
@@ -813,6 +822,7 @@ export async function handleAction({
813822
type: 'done',
814823
result: actionResult,
815824
formState,
825+
temporaryReferences: undefined,
816826
}
817827
} catch (err) {
818828
if (isRedirectError(err)) {
@@ -839,6 +849,7 @@ export async function handleAction({
839849
ctx.renderOpts.basePath,
840850
staticGenerationStore
841851
),
852+
temporaryReferences: undefined,
842853
}
843854
}
844855

@@ -856,6 +867,7 @@ export async function handleAction({
856867
return {
857868
type: 'done',
858869
result: RenderResult.fromStatic(''),
870+
temporaryReferences: undefined,
859871
}
860872
} else if (isNotFoundError(err)) {
861873
res.statusCode = 404
@@ -883,6 +895,7 @@ export async function handleAction({
883895
actionResult: promise,
884896
asNotFound: true,
885897
}),
898+
temporaryReferences: undefined,
886899
}
887900
}
888901
return {
@@ -917,6 +930,7 @@ export async function handleAction({
917930
skipFlight:
918931
!staticGenerationStore.pathWasRevalidated || actionWasForwarded,
919932
}),
933+
temporaryReferences: undefined,
920934
}
921935
}
922936

packages/next/src/server/app-render/app-render.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ type RenderToStreamOptions = {
388388
asNotFound: boolean
389389
tree: LoaderTree
390390
formState: any
391+
temporaryReferences: unknown
391392
}
392393

393394
/**
@@ -906,8 +907,6 @@ async function renderToHTMLOrFlightImpl(
906907

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

909-
const temporaryReferences = ComponentMod.createTemporaryReferenceSet()
910-
911910
const renderToStream = getTracer().wrap(
912911
AppRenderSpan.getBodyResult,
913912
{
@@ -961,7 +960,6 @@ async function renderToHTMLOrFlightImpl(
961960
{
962961
onError: serverComponentsErrorHandler,
963962
nonce,
964-
temporaryReferences: temporaryReferences,
965963
}
966964
)
967965

@@ -1294,7 +1292,6 @@ async function renderToHTMLOrFlightImpl(
12941292
{
12951293
onError: serverComponentsErrorHandler,
12961294
nonce,
1297-
temporaryReferences: temporaryReferences,
12981295
}
12991296
)
13001297

@@ -1368,7 +1365,6 @@ async function renderToHTMLOrFlightImpl(
13681365
requestStore,
13691366
serverActions,
13701367
ctx,
1371-
temporaryReferences,
13721368
})
13731369

13741370
let formState: null | any = null
@@ -1379,6 +1375,7 @@ async function renderToHTMLOrFlightImpl(
13791375
asNotFound: true,
13801376
tree: notFoundLoaderTree,
13811377
formState,
1378+
temporaryReferences: undefined,
13821379
})
13831380

13841381
return new RenderResult(response.stream, { metadata })
@@ -1400,6 +1397,10 @@ async function renderToHTMLOrFlightImpl(
14001397
asNotFound: isNotFoundPath,
14011398
tree: loaderTree,
14021399
formState,
1400+
temporaryReferences:
1401+
actionRequestResult !== undefined
1402+
? actionRequestResult.temporaryReferences
1403+
: undefined,
14031404
})
14041405

14051406
// If we have pending revalidates, wait until they are all resolved.

packages/next/src/server/app-render/encryption.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ export async function decryptActionBoundArgs(
120120
// This extra step ensures that the server references are recovered.
121121
const serverModuleMap = getServerModuleMap()
122122
const transformed = await decodeReply(
123-
await encodeReply(deserialized, {
124-
// TODO: How is decryptActionBoundArgs used? Do we need to support temporary references here?
125-
temporaryReferences: undefined,
126-
}),
123+
await encodeReply(deserialized),
127124
serverModuleMap
128125
)
129126

packages/next/src/server/app-render/react-server.node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// eslint-disable-next-line import/no-extraneous-dependencies
44
export {
5+
createTemporaryReferenceSet,
56
decodeReply,
67
decodeReplyFromBusboy,
78
decodeAction,

0 commit comments

Comments
 (0)