[Flight] Don't increase serializedSize for every recursive pass (#33123)
I noticed that we increase this in the recursive part of the algorithm. This would mean that we'd count a key more than once if it has Server Components inside it recursively resolving. This moves it out to where we enter from toJSON. Which is called once per JSON entry (and therefore once per key).
Sebastian Markbåge committed
May 5, 2025 at 11:37 UTC
52ea641449570bbc32eb90fb1a76740249b6bcf5
1 file changed
+3
-2
packages/react-server/src/ReactFlightServer.js
+3
-2
@@ -2302,6 +2302,9 @@ function renderModel(
2302
key: string,
2303
value: ReactClientValue,
2304
): ReactJSONValue {
2305
+ // First time we're serializing the key, we should add it to the size.
2306
+ serializedSize += key.length;
2307
+
2308
const prevKeyPath = task.keyPath;
2309
const prevImplicitSlot = task.implicitSlot;
2310
try {
@@ -2416,8 +2419,6 @@ function renderModelDestructive(
2419
// Set the currently rendering model
2420
task.model = value;
2421
2419
- serializedSize += parentPropertyName.length;
2420
-
2422
// Special Symbol, that's very common.
2423
if (value === REACT_ELEMENT_TYPE) {
2424
return '$';