From 631bb04939db503146623be18cd2c61063b415e9 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Thu, 8 Feb 2024 18:27:07 -0500 Subject: [PATCH] Serialize deduped elements by direct reference even if they suspend --- packages/react-server/src/ReactFlightServer.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index b1b6c73e46780..7f0ad956cf5be 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -1192,12 +1192,16 @@ function renderModelDestructive( // but that is able to reuse the same task if we're already in one but then that // will be a lazy future value rather than guaranteed to exist but maybe that's good. const newId = outlineModel(request, (value: any)); - return serializeLazyID(newId); + return serializeByValueID(newId); } else { // We've already emitted this as an outlined object, so we can refer to that by its - // existing ID. We use a lazy reference since, unlike plain objects, elements might - // suspend so it might not have emitted yet even if we have the ID for it. - return serializeLazyID(existingId); + // existing ID. TODO: We should use a lazy reference since, unlike plain objects, + // elements might suspend so it might not have emitted yet even if we have the ID for + // it. However, this creates an extra wrapper when it's not needed. We should really + // detect whether this already was emitted and synchronously available. In that + // case we can refer to it synchronously and only make it lazy otherwise. + // We currently don't have a data structure that lets us see that though. + return serializeByValueID(existingId); } } else { // This is the first time we've seen this object. We may never see it again