Skip to content

Commit

Permalink
[Flight] Support returning undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 8, 2023
1 parent f744d73 commit b25c813
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ export function parseModelString(
throw chunk.reason;
}
}
case 'U': {
// Special encoding for `undefined` which can't be serialized as JSON otherwise.
return undefined;
}
default: {
// We assume that anything else is a reference ID.
const id = parseInt(value.substring(1), 16);
Expand Down
15 changes: 10 additions & 5 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export type ReactModel =
| number
| symbol
| null
| void
| Iterable<ReactModel>
| ReactModelObject
| Promise<ReactModel>;
Expand Down Expand Up @@ -533,6 +534,10 @@ function serializeProviderReference(name: string): string {
return '$P' + name;
}

function serializeUndefined(): string {
return '$U';
}

function serializeClientReference(
request: Request,
parent: {+[key: string | number]: ReactModel} | $ReadOnlyArray<ReactModel>,
Expand Down Expand Up @@ -1105,14 +1110,14 @@ export function resolveModelToJSON(
return escapeStringValue(value);
}

if (
typeof value === 'boolean' ||
typeof value === 'number' ||
typeof value === 'undefined'
) {
if (typeof value === 'boolean' || typeof value === 'number') {
return value;
}

if (typeof value === 'undefined') {
return serializeUndefined();
}

if (typeof value === 'function') {
if (isClientReference(value)) {
return serializeClientReference(request, parent, key, (value: any));
Expand Down

0 comments on commit b25c813

Please sign in to comment.