Skip to content

Commit 03bea63

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

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
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 & 2 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>,
@@ -1107,12 +1112,15 @@ export function resolveModelToJSON(
11071112

11081113
if (
11091114
typeof value === 'boolean' ||
1110-
typeof value === 'number' ||
1111-
typeof value === 'undefined'
1115+
typeof value === 'number'
11121116
) {
11131117
return value;
11141118
}
11151119

1120+
if (typeof value === 'undefined') {
1121+
return serializeUndefined()
1122+
}
1123+
11161124
if (typeof value === 'function') {
11171125
if (isClientReference(value)) {
11181126
return serializeClientReference(request, parent, key, (value: any));

0 commit comments

Comments
 (0)