Skip to content

Commit b263419

Browse files
committed
[Flight] Support returning undefined
1 parent f9af385 commit b263419

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,10 @@ export function parseModelString(
556556
throw chunk.reason;
557557
}
558558
}
559+
case 'U': {
560+
// Special encoding for `undefined` which can't be serialized as JSON otherwise.
561+
return undefined;
562+
}
559563
default: {
560564
// We assume that anything else is a reference ID.
561565
const id = parseInt(value.substring(1), 16);

packages/react-server/src/ReactFlightServer.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export type ReactClientValue =
117117
| number
118118
| symbol
119119
| null
120+
| void
120121
| Iterable<ReactClientValue>
121122
| Array<ReactClientValue>
122123
| ReactClientObject
@@ -546,6 +547,10 @@ function serializeProviderReference(name: string): string {
546547
return '$P' + name;
547548
}
548549

550+
function serializeUndefined(): string {
551+
return '$U';
552+
}
553+
549554
function serializeClientReference(
550555
request: Request,
551556
parent:
@@ -1134,14 +1139,14 @@ export function resolveModelToJSON(
11341139
return escapeStringValue(value);
11351140
}
11361141

1137-
if (
1138-
typeof value === 'boolean' ||
1139-
typeof value === 'number' ||
1140-
typeof value === 'undefined'
1141-
) {
1142+
if (typeof value === 'boolean' || typeof value === 'number') {
11421143
return value;
11431144
}
11441145

1146+
if (typeof value === 'undefined') {
1147+
return serializeUndefined();
1148+
}
1149+
11451150
if (typeof value === 'function') {
11461151
if (isClientReference(value)) {
11471152
return serializeClientReference(request, parent, key, (value: any));

0 commit comments

Comments
 (0)