GeezerVison is showing github.com — Encode server rendered host components as array tuples (#18273) · react/react@dc7eeda
File treeExpand file treeCollapse file tree Open diff view settings react-flight-dom-relay/src react-flight-dom-webpack/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fixtures/flight-browser/index.html +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,9 +57,7 @@ <h1>Flight Example</h1> | 57 | 57 |
| 58 | 58 | let model = { | 59 | 59 | title: <Title />, | 60 | | - content: { | 61 | | - __html: <HTML />, | 62 | | - } | | 60 | + content: <HTML />, | 63 | 61 | }; | 64 | 62 |
| 65 | 63 | let stream = ReactFlightDOMServer.renderToReadableStream(model); | @@ -90,7 +88,7 @@ <h1>Flight Example</h1> | 90 | 88 | <Suspense fallback="..."> | 91 | 89 | <h1>{model.title}</h1> | 92 | 90 | </Suspense> | 93 | | - <div dangerouslySetInnerHTML={model.content} /> | | 91 | + {model.content} | 94 | 92 | </div>; | 95 | 93 | } | 96 | 94 |
| |
Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/server/handler.js +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,9 +20,7 @@ function HTML() { | 20 | 20 | module.exports = function(req, res) { | 21 | 21 | res.setHeader('Access-Control-Allow-Origin', '*'); | 22 | 22 | let model = { | 23 | | - content: { | 24 | | - __html: <HTML />, | 25 | | - }, | | 23 | + content: <HTML />, | 26 | 24 | }; | 27 | 25 | ReactFlightDOMServer.pipeToNodeWritable(model, res); | 28 | 26 | }; |
Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/src/App.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import React, {Suspense} from 'react'; | 2 | 2 |
| 3 | 3 | function Content({data}) { | 4 | | - return <p dangerouslySetInnerHTML={data.model.content} />; | | 4 | + return data.model.content; | 5 | 5 | } | 6 | 6 |
| 7 | 7 | function App({data}) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClient.js +73-16Lines changed: 73 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| | 10 | +import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols'; | | 11 | + | 10 | 12 | export type ReactModelRoot<T> = {| | 11 | 13 | model: T, | 12 | 14 | |}; | @@ -19,6 +21,8 @@ export type JSONValue = | 19 | 21 | | {[key: string]: JSONValue} | 20 | 22 | | Array<JSONValue>; | 21 | 23 |
| | 24 | +const isArray = Array.isArray; | | 25 | + | 22 | 26 | const PENDING = 0; | 23 | 27 | const RESOLVED = 1; | 24 | 28 | const ERRORED = 2; | @@ -141,28 +145,81 @@ function definePendingProperty( | 141 | 145 | }); | 142 | 146 | } | 143 | 147 |
| | 148 | +function createElement(type, key, props): React$Element<any> { | | 149 | + const element: any = { | | 150 | + // This tag allows us to uniquely identify this as a React Element | | 151 | + $$typeof: REACT_ELEMENT_TYPE, | | 152 | + | | 153 | + // Built-in properties that belong on the element | | 154 | + type: type, | | 155 | + key: key, | | 156 | + ref: null, | | 157 | + props: props, | | 158 | + | | 159 | + // Record the component responsible for creating this element. | | 160 | + _owner: null, | | 161 | + }; | | 162 | + if (__DEV__) { | | 163 | + // We don't really need to add any of these but keeping them for good measure. | | 164 | + // Unfortunately, _store is enumerable in jest matchers so for equality to | | 165 | + // work, I need to keep it or make _store non-enumerable in the other file. | | 166 | + element._store = {}; | | 167 | + Object.defineProperty(element._store, 'validated', { | | 168 | + configurable: false, | | 169 | + enumerable: false, | | 170 | + writable: true, | | 171 | + value: true, // This element has already been validated on the server. | | 172 | + }); | | 173 | + Object.defineProperty(element, '_self', { | | 174 | + configurable: false, | | 175 | + enumerable: false, | | 176 | + writable: false, | | 177 | + value: null, | | 178 | + }); | | 179 | + Object.defineProperty(element, '_source', { | | 180 | + configurable: false, | | 181 | + enumerable: false, | | 182 | + writable: false, | | 183 | + value: null, | | 184 | + }); | | 185 | + } | | 186 | + return element; | | 187 | +} | | 188 | + | 144 | 189 | export function parseModelFromJSON( | 145 | 190 | response: Response, | 146 | 191 | targetObj: Object, | 147 | 192 | key: string, | 148 | 193 | value: JSONValue, | 149 | | -): any { | 150 | | - if (typeof value === 'string' && value[0] === '$') { | 151 | | - if (value[1] === '$') { | 152 | | - // This was an escaped string value. | 153 | | - return value.substring(1); | 154 | | - } else { | 155 | | - let id = parseInt(value.substring(1), 16); | 156 | | - let chunks = response.chunks; | 157 | | - let chunk = chunks.get(id); | 158 | | - if (!chunk) { | 159 | | - chunk = createPendingChunk(); | 160 | | - chunks.set(id, chunk); | 161 | | - } else if (chunk.status === RESOLVED) { | 162 | | - return chunk.value; | | 194 | +): mixed { | | 195 | + if (typeof value === 'string') { | | 196 | + if (value[0] === '$') { | | 197 | + if (value === '$') { | | 198 | + return REACT_ELEMENT_TYPE; | | 199 | + } else if (value[1] === '$' || value[1] === '@') { | | 200 | + // This was an escaped string value. | | 201 | + return value.substring(1); | | 202 | + } else { | | 203 | + let id = parseInt(value.substring(1), 16); | | 204 | + let chunks = response.chunks; | | 205 | + let chunk = chunks.get(id); | | 206 | + if (!chunk) { | | 207 | + chunk = createPendingChunk(); | | 208 | + chunks.set(id, chunk); | | 209 | + } else if (chunk.status === RESOLVED) { | | 210 | + return chunk.value; | | 211 | + } | | 212 | + definePendingProperty(targetObj, key, chunk); | | 213 | + return undefined; | 163 | 214 | } | 164 | | - definePendingProperty(targetObj, key, chunk); | 165 | | - return undefined; | | 215 | + } | | 216 | + } | | 217 | + if (isArray(value)) { | | 218 | + let tuple: [mixed, mixed, mixed, mixed] = (value: any); | | 219 | + if (tuple[0] === REACT_ELEMENT_TYPE) { | | 220 | + // TODO: Consider having React just directly accept these arrays as elements. | | 221 | + // Or even change the ReactElement type to be an array. | | 222 | + return createElement(tuple[1], tuple[2], tuple[3]); | 166 | 223 | } | 167 | 224 | } | 168 | 225 | return value; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-flight-dom-relay/src/ReactFlightDOMRelayClient.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,11 +22,11 @@ function parseModel(response, targetObj, key, value) { | 22 | 22 | if (typeof value === 'object' && value !== null) { | 23 | 23 | if (Array.isArray(value)) { | 24 | 24 | for (let i = 0; i < value.length; i++) { | 25 | | - value[i] = parseModel(response, value, '' + i, value[i]); | | 25 | + (value: any)[i] = parseModel(response, value, '' + i, value[i]); | 26 | 26 | } | 27 | 27 | } else { | 28 | 28 | for (let innerKey in value) { | 29 | | - value[innerKey] = parseModel( | | 29 | + (value: any)[innerKey] = parseModel( | 30 | 30 | response, | 31 | 31 | value, | 32 | 32 | innerKey, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-flight-dom-webpack/src/__tests__/ReactFlightDOM-test.js +40-3Lines changed: 40 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -92,7 +92,12 @@ describe('ReactFlightDOM', () => { | 92 | 92 | let result = ReactFlightDOMClient.readFromReadableStream(readable); | 93 | 93 | await waitForSuspense(() => { | 94 | 94 | expect(result.model).toEqual({ | 95 | | - html: '<div><span>hello</span><span>world</span></div>', | | 95 | + html: ( | | 96 | + <div> | | 97 | + <span>hello</span> | | 98 | + <span>world</span> | | 99 | + </div> | | 100 | + ), | 96 | 101 | }); | 97 | 102 | }); | 98 | 103 | }); | @@ -120,7 +125,7 @@ describe('ReactFlightDOM', () => { | 120 | 125 |
| 121 | 126 | // View | 122 | 127 | function Message({result}) { | 123 | | - return <p dangerouslySetInnerHTML={{__html: result.model.html}} />; | | 128 | + return <section>{result.model.html}</section>; | 124 | 129 | } | 125 | 130 | function App({result}) { | 126 | 131 | return ( | @@ -140,7 +145,7 @@ describe('ReactFlightDOM', () => { | 140 | 145 | root.render(<App result={result} />); | 141 | 146 | }); | 142 | 147 | expect(container.innerHTML).toBe( | 143 | | - '<p><div><span>hello</span><span>world</span></div></p>', | | 148 | + '<section><div><span>hello</span><span>world</span></div></section>', | 144 | 149 | ); | 145 | 150 | }); | 146 | 151 |
| @@ -176,6 +181,38 @@ describe('ReactFlightDOM', () => { | 176 | 181 | expect(container.innerHTML).toBe('<p>$1</p>'); | 177 | 182 | }); | 178 | 183 |
| | 184 | + it.experimental('should not get confused by @', async () => { | | 185 | + let {Suspense} = React; | | 186 | + | | 187 | + // Model | | 188 | + function RootModel() { | | 189 | + return {text: '@div'}; | | 190 | + } | | 191 | + | | 192 | + // View | | 193 | + function Message({result}) { | | 194 | + return <p>{result.model.text}</p>; | | 195 | + } | | 196 | + function App({result}) { | | 197 | + return ( | | 198 | + <Suspense fallback={<h1>Loading...</h1>}> | | 199 | + <Message result={result} /> | | 200 | + </Suspense> | | 201 | + ); | | 202 | + } | | 203 | + | | 204 | + let {writable, readable} = getTestStream(); | | 205 | + ReactFlightDOMServer.pipeToNodeWritable(<RootModel />, writable); | | 206 | + let result = ReactFlightDOMClient.readFromReadableStream(readable); | | 207 | + | | 208 | + let container = document.createElement('div'); | | 209 | + let root = ReactDOM.createRoot(container); | | 210 | + await act(async () => { | | 211 | + root.render(<App result={result} />); | | 212 | + }); | | 213 | + expect(container.innerHTML).toBe('<p>@div</p>'); | | 214 | + }); | | 215 | + | 179 | 216 | it.experimental('should progressively reveal chunks', async () => { | 180 | 217 | let {Suspense} = React; | 181 | 218 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-flight-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,7 +65,12 @@ describe('ReactFlightDOMBrowser', () => { | 65 | 65 | let result = ReactFlightDOMClient.readFromReadableStream(stream); | 66 | 66 | await waitForSuspense(() => { | 67 | 67 | expect(result.model).toEqual({ | 68 | | - html: '<div><span>hello</span><span>world</span></div>', | | 68 | + html: ( | | 69 | + <div> | | 70 | + <span>hello</span> | | 71 | + <span>world</span> | | 72 | + </div> | | 73 | + ), | 69 | 74 | }); | 70 | 75 | }); | 71 | 76 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactDOMServerFormatConfig.js -12Lines changed: 0 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | import {convertStringToBuffer} from 'react-server/src/ReactServerStreamConfig'; | 11 | 11 |
| 12 | | -import {renderToStaticMarkup} from 'react-dom/server'; | 13 | | - | 14 | 12 | export function formatChunkAsString(type: string, props: Object): string { | 15 | 13 | let str = '<' + type + '>'; | 16 | 14 | if (typeof props.children === 'string') { | @@ -23,13 +21,3 @@ export function formatChunkAsString(type: string, props: Object): string { | 23 | 21 | export function formatChunk(type: string, props: Object): Uint8Array { | 24 | 22 | return convertStringToBuffer(formatChunkAsString(type, props)); | 25 | 23 | } | 26 | | - | 27 | | -export function renderHostChildrenToString( | 28 | | - children: React$Element<any>, | 29 | | -): string { | 30 | | - // TODO: This file is used to actually implement a server renderer | 31 | | - // so we can't actually reference the renderer here. Instead, we | 32 | | - // should replace this method with a reference to Fizz which | 33 | | - // then uses this file to implement the server renderer. | 34 | | - return renderToStaticMarkup(children); | 35 | | -} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFlightServer.js +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ import { | 19 | 19 | processModelChunk, | 20 | 20 | processErrorChunk, | 21 | 21 | } from './ReactFlightServerConfig'; | 22 | | -import {renderHostChildrenToString} from './ReactServerFormatConfig'; | | 22 | + | 23 | 23 | import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols'; | 24 | 24 |
| 25 | 25 | type ReactJSONValue = | @@ -88,7 +88,7 @@ function attemptResolveModelComponent(element: React$Element<any>): ReactModel { | 88 | 88 | return type(props); | 89 | 89 | } else if (typeof type === 'string') { | 90 | 90 | // This is a host element. E.g. HTML. | 91 | | - return renderHostChildrenToString(element); | | 91 | + return [REACT_ELEMENT_TYPE, type, element.key, element.props]; | 92 | 92 | } else { | 93 | 93 | throw new Error('Unsupported type.'); | 94 | 94 | } | @@ -119,7 +119,7 @@ function serializeIDRef(id: number): string { | 119 | 119 | function escapeStringValue(value: string): string { | 120 | 120 | if (value[0] === '$') { | 121 | 121 | // We need to escape $ prefixed strings since we use that to encode | 122 | | - // references to IDs. | | 122 | + // references to IDs and as a special symbol value. | 123 | 123 | return '$' + value; | 124 | 124 | } else { | 125 | 125 | return value; | @@ -134,6 +134,10 @@ export function resolveModelToJSON( | 134 | 134 | return escapeStringValue(value); | 135 | 135 | } | 136 | 136 |
| | 137 | + if (value === REACT_ELEMENT_TYPE) { | | 138 | + return '$'; | | 139 | + } | | 140 | + | 137 | 141 | while ( | 138 | 142 | typeof value === 'object' && | 139 | 143 | value !== null && | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/forks/ReactServerFormatConfig.custom.js -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,5 +28,3 @@ export opaque type Destination = mixed; // eslint-disable-line no-undef | 28 | 28 |
| 29 | 29 | export const formatChunkAsString = $$$hostConfig.formatChunkAsString; | 30 | 30 | export const formatChunk = $$$hostConfig.formatChunk; | 31 | | -export const renderHostChildrenToString = | 32 | | - $$$hostConfig.renderHostChildrenToString; |
|