Skip to content

Commit

Permalink
[Flight] Add support for returning undefined from render (#26349)
Browse files Browse the repository at this point in the history
## Summary

Adds support for returning `undefined` from Server Components.
Also fixes a bug where rendering an empty fragment would throw the same
error as returning undefined.

## How did you test this change?

- [x] test failed with same error message I got when returning undefined
from Server Components in a Next.js app
- [x] test passes after adding encoding for `undefined`

DiffTrain build for [d1ad984](d1ad984)
  • Loading branch information
eps1lon committed Mar 9, 2023
1 parent fabd29a commit 2d9d96b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3706edb81c84ea01e68bfb477537ba9908e1bd70
d1ad984db1591b131d16739a24dee4ba44886a09
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,12 @@ function parseModelString(response, parentObject, key, value) {
}
}

case "u": {
// matches "$undefined"
// Special encoding for `undefined` which can't be serialized as JSON otherwise.
return undefined;
}

default: {
// We assume that anything else is a reference ID.
var _id3 = parseInt(value.substring(1), 16);
Expand Down
6 changes: 6 additions & 0 deletions compiled/facebook-www/ReactFlightDOMRelayClient-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,12 @@ function parseModelString(response, parentObject, key, value) {
}
}

case "u": {
// matches "$undefined"
// Special encoding for `undefined` which can't be serialized as JSON otherwise.
return undefined;
}

default: {
// We assume that anything else is a reference ID.
var _id3 = parseInt(value.substring(1), 16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ function parseModelString(response, parentObject, key, value) {
default:
throw parentObject.reason;
}
case "u":
return;
default:
value = parseInt(value.substring(1), 16);
response = getChunk(response, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ function parseModelString(response, parentObject, key, value) {
default:
throw parentObject.reason;
}
case "u":
return;
default:
value = parseInt(value.substring(1), 16);
response = getChunk(response, value);
Expand Down
14 changes: 9 additions & 5 deletions compiled/facebook-www/ReactFlightDOMRelayServer-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,10 @@ function serializeProviderReference(name) {
return "$P" + name;
}

function serializeUndefined() {
return "$undefined";
}

function serializeClientReference(request, parent, key, clientReference) {
var clientReferenceKey = getClientReferenceKey(clientReference);
var writtenClientReferences = request.writtenClientReferences;
Expand Down Expand Up @@ -2235,14 +2239,14 @@ function resolveModelToJSON(request, parent, key, value) {
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);
Expand Down
14 changes: 9 additions & 5 deletions compiled/facebook-www/ReactFlightDOMRelayServer-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,10 @@ function serializeProviderReference(name) {
return "$P" + name;
}

function serializeUndefined() {
return "$undefined";
}

function serializeClientReference(request, parent, key, clientReference) {
var clientReferenceKey = getClientReferenceKey(clientReference);
var writtenClientReferences = request.writtenClientReferences;
Expand Down Expand Up @@ -2235,14 +2239,14 @@ function resolveModelToJSON(request, parent, key, value) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,8 @@ function resolveModelToJSON(request, parent, key, value) {
}
if ("string" === typeof value)
return (request = "$" === value[0] ? "$" + value : value), request;
if (
"boolean" === typeof value ||
"number" === typeof value ||
"undefined" === typeof value
)
return value;
if ("boolean" === typeof value || "number" === typeof value) return value;
if ("undefined" === typeof value) return "$undefined";
if ("function" === typeof value) {
if (value instanceof JSResourceReferenceImpl)
return serializeClientReference(request, parent, key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,8 @@ function resolveModelToJSON(request, parent, key, value) {
}
if ("string" === typeof value)
return (request = "$" === value[0] ? "$" + value : value), request;
if (
"boolean" === typeof value ||
"number" === typeof value ||
"undefined" === typeof value
)
return value;
if ("boolean" === typeof value || "number" === typeof value) return value;
if ("undefined" === typeof value) return "$undefined";
if ("function" === typeof value) {
if (value instanceof JSResourceReferenceImpl)
return serializeClientReference(request, parent, key, value);
Expand Down

0 comments on commit 2d9d96b

Please sign in to comment.