Skip to content

Commit b25c813

Browse files
committed
[Flight] Support returning undefined
1 parent f744d73 commit b25c813

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
@@ -553,6 +553,10 @@ export function parseModelString(
553553
throw chunk.reason;
554554
}
555555
}
556+
case 'U': {
557+
// Special encoding for `undefined` which can't be serialized as JSON otherwise.
558+
return undefined;
559+
}
556560
default: {
557561
// We assume that anything else is a reference ID.
558562
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
@@ -105,6 +105,7 @@ export type ReactModel =
105105
| number
106106
| symbol
107107
| null
108+
| void
108109
| Iterable<ReactModel>
109110
| ReactModelObject
110111
| Promise<ReactModel>;
@@ -533,6 +534,10 @@ function serializeProviderReference(name: string): string {
533534
return '$P' + name;
534535
}
535536

537+
function serializeUndefined(): string {
538+
return '$U';
539+
}
540+
536541
function serializeClientReference(
537542
request: Request,
538543
parent: {+[key: string | number]: ReactModel} | $ReadOnlyArray<ReactModel>,
@@ -1105,14 +1110,14 @@ export function resolveModelToJSON(
11051110
return escapeStringValue(value);
11061111
}
11071112

1108-
if (
1109-
typeof value === 'boolean' ||
1110-
typeof value === 'number' ||
1111-
typeof value === 'undefined'
1112-
) {
1113+
if (typeof value === 'boolean' || typeof value === 'number') {
11131114
return value;
11141115
}
11151116

1117+
if (typeof value === 'undefined') {
1118+
return serializeUndefined();
1119+
}
1120+
11161121
if (typeof value === 'function') {
11171122
if (isClientReference(value)) {
11181123
return serializeClientReference(request, parent, key, (value: any));

0 commit comments

Comments
 (0)