Skip to content

Commit 5edd706

Browse files
committed
Count the size of the currently serialized row
Including any outlined chunks that would go before this row since they also block it. The size doesn't include every single byte written like every bracket and number. It's just the big picture scale like string lengths including keys.
1 parent 8e9a5fc commit 5edd706

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

packages/react-server/src/ReactFlightServer.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,11 @@ function renderClientElement(
16001600
// The chunk ID we're currently rendering that we can assign debug data to.
16011601
let debugID: null | number = null;
16021602

1603+
// Approximate string length of the currently serializing row.
1604+
// Used to power outlining heuristics.
1605+
let serializedSize = 0;
1606+
const MAX_ROW_SIZE = 3200;
1607+
16031608
function outlineTask(request: Request, task: Task): ReactJSONValue {
16041609
const newTask = createTask(
16051610
request,
@@ -2393,6 +2398,8 @@ function renderModelDestructive(
23932398
// Set the currently rendering model
23942399
task.model = value;
23952400

2401+
serializedSize += parentPropertyName.length;
2402+
23962403
// Special Symbol, that's very common.
23972404
if (value === REACT_ELEMENT_TYPE) {
23982405
return '$';
@@ -2811,6 +2818,7 @@ function renderModelDestructive(
28112818
throwTaintViolation(tainted.message);
28122819
}
28132820
}
2821+
serializedSize += value.length;
28142822
// TODO: Maybe too clever. If we support URL there's no similar trick.
28152823
if (value[value.length - 1] === 'Z') {
28162824
// Possibly a Date, whose toJSON automatically calls toISOString
@@ -3892,9 +3900,18 @@ function emitChunk(
38923900
return;
38933901
}
38943902
// For anything else we need to try to serialize it using JSON.
3895-
// $FlowFixMe[incompatible-type] stringify can return null for undefined but we never do
3896-
const json: string = stringify(value, task.toJSON);
3897-
emitModelChunk(request, task.id, json);
3903+
// We stash the outer parent size so we can restore it when we exit.
3904+
// We don't reset the serialized size counter from reentry because that indicates that we
3905+
// are outlining a model and we actually want to include that size into the parent since
3906+
// it will still block the parent row. It only restores to zero at the top of the stack.
3907+
const parentSerializedSize = 0;
3908+
try {
3909+
// $FlowFixMe[incompatible-type] stringify can return null for undefined but we never do
3910+
const json: string = stringify(value, task.toJSON);
3911+
emitModelChunk(request, task.id, json);
3912+
} finally {
3913+
serializedSize = parentSerializedSize;
3914+
}
38983915
}
38993916

39003917
function erroredTask(request: Request, task: Task, error: mixed): void {

0 commit comments

Comments
 (0)