From b25c8132422be406b94cd3a30e69a966af1e7adb Mon Sep 17 00:00:00 2001 From: eps1lon Date: Wed, 8 Mar 2023 21:36:42 +0100 Subject: [PATCH] [Flight] Support returning undefined --- packages/react-client/src/ReactFlightClient.js | 4 ++++ packages/react-server/src/ReactFlightServer.js | 15 ++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/react-client/src/ReactFlightClient.js b/packages/react-client/src/ReactFlightClient.js index aa864b6895b46..24da4242e949f 100644 --- a/packages/react-client/src/ReactFlightClient.js +++ b/packages/react-client/src/ReactFlightClient.js @@ -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); diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index 17e1c7aca83c5..e3a7b12ebe2bb 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -105,6 +105,7 @@ export type ReactModel = | number | symbol | null + | void | Iterable | ReactModelObject | Promise; @@ -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, @@ -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));