Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] Encode server rendered host components as array tuples #18273

Merged
merged 1 commit into from
Mar 11, 2020

Conversation

sebmarkbage
Copy link
Collaborator

@sebmarkbage sebmarkbage commented Mar 11, 2020

This replaces the HTML renderer with instead resolving host elements into arrays tagged with the react.element symbol. These turn into proper React Elements on the client.

The symbol is encoded as the magical value "$". This has security implications so this special value needs to remain escaped for other strings.

We could just encode the element as {$$typeof: "$", type: type, key: key props: props} but that's a lot more bytes. So instead I encode it as ["$", type, key, props] and then convert it back on the client.

In a follow up I intend to encode Block Pairs ("thunks") as ["@", render, data].

It would be nicer if React's reconciler could just accept these tuples directly. We should consider that as part of the deprecation work around elements.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Mar 11, 2020
This replaces the HTML renderer with instead resolving host elements into
arrays tagged with the react.element symbol. These turn into proper
React Elements on the client.

The symbol is encoded as the magical value "$". This has security implications
so this special value needs to remain escaped for other strings.

We could just encode the element as {$$typeof: "$", key: key props: props}
but that's a lot more bytes. So instead I encode it as:
["$", key, props] and then convert it back.

It would be nicer if React's reconciler could just accept these tuples.
@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 11, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 47df380:

Sandbox Source
elastic-albattani-lh0md Configuration

@sizebot
Copy link

sizebot commented Mar 11, 2020

Details of bundled changes.

Comparing: 99d7371...47df380

react-flight-dom-relay

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactFlightDOMRelayClient-dev.js +38.8% +42.1% 4.87 KB 6.75 KB 1.54 KB 2.2 KB FB_WWW_DEV
ReactFlightDOMRelayClient-prod.js 🔺+16.6% 🔺+15.6% 2.96 KB 3.45 KB 1.05 KB 1.22 KB FB_WWW_PROD
ReactFlightDOMRelayServer-dev.js -3.6% -4.7% 7.93 KB 7.64 KB 2.38 KB 2.27 KB FB_WWW_DEV
ReactFlightDOMRelayServer-prod.js 🔺+0.5% -0.4% 5.68 KB 5.71 KB 1.66 KB 1.65 KB FB_WWW_PROD

react-flight-dom-webpack

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-flight-dom-webpack-server.node.development.js -3.2% -4.5% 8.83 KB 8.54 KB 2.73 KB 2.61 KB NODE_DEV
react-flight-dom-webpack-server.node.production.min.js -0.6% -1.1% 2.77 KB 2.75 KB 1.25 KB 1.24 KB NODE_PROD
react-flight-dom-webpack-server.browser.development.js -3.8% -4.9% 8.64 KB 8.31 KB 2.57 KB 2.45 KB UMD_DEV
react-flight-dom-webpack-server.browser.production.min.js -1.9% -0.9% 2.83 KB 2.78 KB 1.32 KB 1.31 KB UMD_PROD
react-flight-dom-webpack-server.browser.development.js -3.6% -4.7% 7.88 KB 7.6 KB 2.46 KB 2.34 KB NODE_DEV
react-flight-dom-webpack-server.browser.production.min.js -0.7% -0.9% 2.61 KB 2.59 KB 1.24 KB 1.23 KB NODE_PROD
react-flight-dom-webpack.development.js +22.8% +26.8% 8.76 KB 10.75 KB 2.39 KB 3.03 KB UMD_DEV
react-flight-dom-webpack.production.min.js 🔺+6.8% 🔺+8.7% 2.89 KB 3.09 KB 1.33 KB 1.44 KB UMD_PROD
react-flight-dom-webpack.development.js +23.6% +28.4% 8 KB 9.89 KB 2.28 KB 2.93 KB NODE_DEV
react-flight-dom-webpack.production.min.js 🔺+7.4% 🔺+8.8% 2.71 KB 2.91 KB 1.25 KB 1.36 KB NODE_PROD

react-client

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-client-flight.development.js +25.6% +28.0% 7.38 KB 9.27 KB 2.28 KB 2.92 KB NODE_DEV
react-client-flight.production.min.js 🔺+8.1% 🔺+9.3% 2.49 KB 2.7 KB 1.17 KB 1.27 KB NODE_PROD

react-server

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-server-flight.development.js -7.5% -0.5% 8.78 KB 8.12 KB 2.56 KB 2.55 KB NODE_DEV
react-server-flight.production.min.js -0.3% -0.2% 2.84 KB 2.83 KB 1.28 KB 1.28 KB NODE_PROD
react-server.development.js -2.0% -1.3% 3.69 KB 3.62 KB 1.24 KB 1.22 KB NODE_DEV
react-server.production.min.js 0.0% -0.2% 1.15 KB 1.15 KB 644 B 643 B NODE_PROD

Size changes (experimental)

Generated by 🚫 dangerJS against 47df380

@sizebot
Copy link

sizebot commented Mar 11, 2020

Details of bundled changes.

Comparing: 99d7371...47df380

react-flight-dom-relay

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactFlightDOMRelayServer-dev.js -3.6% -4.7% 7.93 KB 7.64 KB 2.38 KB 2.27 KB FB_WWW_DEV
ReactFlightDOMRelayServer-prod.js 🔺+0.5% -0.4% 5.68 KB 5.71 KB 1.66 KB 1.65 KB FB_WWW_PROD
ReactFlightDOMRelayClient-dev.js +38.8% +42.1% 4.87 KB 6.75 KB 1.54 KB 2.2 KB FB_WWW_DEV
ReactFlightDOMRelayClient-prod.js 🔺+16.6% 🔺+15.6% 2.96 KB 3.45 KB 1.05 KB 1.22 KB FB_WWW_PROD

react-flight-dom-webpack

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-flight-dom-webpack-server.browser.development.js -3.6% -4.7% 7.87 KB 7.59 KB 2.45 KB 2.34 KB NODE_DEV
react-flight-dom-webpack-server.browser.production.min.js -0.7% -1.0% 2.6 KB 2.58 KB 1.23 KB 1.22 KB NODE_PROD
react-flight-dom-webpack.development.js +22.8% +26.9% 8.74 KB 10.74 KB 2.38 KB 3.02 KB UMD_DEV
react-flight-dom-webpack.production.min.js 🔺+6.9% 🔺+8.7% 2.88 KB 3.08 KB 1.32 KB 1.43 KB UMD_PROD
react-flight-dom-webpack.development.js +23.6% +28.4% 7.99 KB 9.88 KB 2.27 KB 2.92 KB NODE_DEV
react-flight-dom-webpack.production.min.js 🔺+7.5% 🔺+9.2% 2.7 KB 2.9 KB 1.24 KB 1.35 KB NODE_PROD
react-flight-dom-webpack-server.node.development.js -3.2% -4.5% 8.81 KB 8.53 KB 2.72 KB 2.6 KB NODE_DEV
react-flight-dom-webpack-server.node.production.min.js -0.6% -1.0% 2.76 KB 2.74 KB 1.24 KB 1.23 KB NODE_PROD
react-flight-dom-webpack-server.browser.development.js -3.8% -4.8% 8.63 KB 8.3 KB 2.56 KB 2.44 KB UMD_DEV
react-flight-dom-webpack-server.browser.production.min.js -1.9% -0.9% 2.82 KB 2.76 KB 1.32 KB 1.3 KB UMD_PROD

react-client

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-client-flight.development.js +25.6% +28.1% 7.37 KB 9.26 KB 2.27 KB 2.91 KB NODE_DEV
react-client-flight.production.min.js 🔺+8.1% 🔺+9.3% 2.48 KB 2.68 KB 1.16 KB 1.27 KB NODE_PROD

react-server

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-server-flight.production.min.js -0.3% 0.0% 2.82 KB 2.81 KB 1.27 KB 1.27 KB NODE_PROD
react-server-flight.development.js -7.5% -0.6% 8.77 KB 8.11 KB 2.55 KB 2.54 KB NODE_DEV
react-server.development.js -2.0% -1.3% 3.68 KB 3.6 KB 1.23 KB 1.22 KB NODE_DEV
react-server.production.min.js 0.0% -0.2% 1.14 KB 1.14 KB 636 B 635 B NODE_PROD

Size changes (stable)

Generated by 🚫 dangerJS against 47df380

Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

Comment on lines +220 to +222
// TODO: Consider having React just directly accept these arrays as elements.
// Or even change the ReactElement type to be an array.
return createElement(tuple[1], tuple[2], tuple[3]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that change we'd just be able to pass arrays through without deserializing, right? Though we'd still call this function on the props element, right? Or I guess those are flattened(?). The overall architecture makes sense but i'm still catching up on the details of each stage in the expansion, serialization, deserialization, and rendering.

Copy link
Collaborator Author

@sebmarkbage sebmarkbage Mar 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still a deserialization step for the strings. String values starting with $ have special meaning so we need to deserialize those but we also need to unescape the user space strings that start with $.

JSON.parse materializes all objects and arrays in the JSON and then traverses over them by mutating them with new values. Which we'd still need to do to replace "$" with the symbol and replace references to reused or future values (i.e. "$12345" to their materialized value).

The benefit would be that we'd reuse the array already created by JSON.parse and just mutate the first slot from a string to a symbol.

We need the symbol to avoid the XSS potential in #3473.

The problem is that if this value gets passed into a render function and then returned from the render, we don't know if that came from Flight on the server or user provided JSON.

props: { userdata: ["$", "script", {children: "alert('p0wned')"}] }

So I solve this by escaping the "$" to "$$" in user provided data. At some point, I need to unescape it though so code can read from it. That's where deserialization comes in.

It's also used for references and I expect us to use it for more things.

In the raw Flight streaming model, this is done using the second argument to JSON.parse so it's kind of inline with the parser which (I hope) is very fast.

The issue with the Relay layering is that we currently have to do a second traversal. We could possibly avoid that if we were hooked in somehow to the same JSON.parse that Relay uses or any other pre-existing traversals.

Copy link
Contributor

@josephsavona josephsavona Mar 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL that JSON.parse takes a second argument, i've never had a use-case for that but this is a great one.

The issue with the Relay layering is that we currently have to do a second traversal. We could possibly avoid that if we were hooked in somehow to the same JSON.parse that Relay uses or any other pre-existing traversals.

Cool. Note that Relay doesn't directly call JSON.parse - that's up to the user-provided network layer - so it should be possible for us to integrate the Flight deserialization into our internal network layer (and for folks in OSS to do the same) to avoid a double traversal.

@josephsavona
Copy link
Contributor

In a follow up I intend to encode Block Pairs ("thunks") as ["@", render, data].

👍

It would be nicer if React's reconciler could just accept these tuples directly. We should consider that as part of the deprecation work around elements.

Would be cleaner conceptually and also faster - are there any particular concerns or challenges to doing that?

@sebmarkbage
Copy link
Collaborator Author

Would be cleaner conceptually and also faster - are there any particular concerns or challenges to doing that?

There are challenges to changing existing React Elements. Mostly because people read from them using element.props.foo or do isArray type checks on various things like traversing down a VDOM tree.

We could add these as a new type of element though. It'll mean they break similar traversal mechanisms that check for isArray or iterable etc. But it's plausible.

@sebmarkbage sebmarkbage merged commit dc7eeda into facebook:master Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants