From 391454aadd14b785ff06eb2be87f23de634350ee Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 14 Apr 2021 17:24:59 -0400 Subject: [PATCH 1/3] Emit string children without extra node This avoids adding comments to the title tag. This is idempotent if you don't restructure your code. --- .../src/__tests__/ReactDOMFizzServerBrowser-test.js | 2 +- .../react-dom/src/__tests__/ReactDOMFizzServerNode-test.js | 4 ++-- packages/react-dom/src/server/ReactDOMServerFormatConfig.js | 6 ++++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js index f805854841ffa..71c301134dd07 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js @@ -56,7 +56,7 @@ describe('ReactDOMFizzServer', () => { ); const result = await readResult(stream); expect(result).toMatchInlineSnapshot( - `"
hello world
"`, + `"
hello world
"`, ); }); diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js index cd93ac5a5163c..464ee488d25cb 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js @@ -66,7 +66,7 @@ describe('ReactDOMFizzServer', () => { startWriting(); jest.runAllTimers(); expect(output.result).toMatchInlineSnapshot( - `"
hello world
"`, + `"
hello world
"`, ); }); @@ -84,7 +84,7 @@ describe('ReactDOMFizzServer', () => { // Then React starts writing. startWriting(); expect(output.result).toMatchInlineSnapshot( - `"test
hello world
"`, + `"test
hello world
"`, ); }); diff --git a/packages/react-dom/src/server/ReactDOMServerFormatConfig.js b/packages/react-dom/src/server/ReactDOMServerFormatConfig.js index 62d27c5e99174..3f1734c0c722a 100644 --- a/packages/react-dom/src/server/ReactDOMServerFormatConfig.js +++ b/packages/react-dom/src/server/ReactDOMServerFormatConfig.js @@ -1079,6 +1079,12 @@ function pushStartGenericElement( target.push(endOfStartTag); pushInnerHTML(target, innerHTML, children); + if (typeof children === 'string') { + // Special case children as a string to avoid the unnecessary comment. + // TODO: Remove this special case after the general optimization is in place. + target.push(stringToChunk(encodeHTMLTextNode(children))); + return null; + } return children; } From 20ec41e2c53ca081a5e35dfd644501ea69ed6c0e Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 14 Apr 2021 21:59:16 -0400 Subject: [PATCH 2/3] ToString dangerouslySetInnerHTML --- .../src/server/ReactDOMServerFormatConfig.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/react-dom/src/server/ReactDOMServerFormatConfig.js b/packages/react-dom/src/server/ReactDOMServerFormatConfig.js index 3f1734c0c722a..53a4786f73cdf 100644 --- a/packages/react-dom/src/server/ReactDOMServerFormatConfig.js +++ b/packages/react-dom/src/server/ReactDOMServerFormatConfig.js @@ -563,7 +563,9 @@ function pushInnerHTML( 'for more information.', ); const html = innerHTML.__html; - target.push(stringToChunk(html)); + if (html !== null && html !== undefined) { + target.push(stringToChunk('' + html)); + } } } @@ -1211,10 +1213,13 @@ function pushStartPreformattedElement( 'for more information.', ); const html = innerHTML.__html; - if (typeof html === 'string' && html[0] === '\n') { - target.push(leadingNewline); + if (html !== null && html !== undefined) { + if (typeof html === 'string' && html.length > 0 && html[0] === '\n') { + target.push(leadingNewline, stringToChunk(html)); + } else { + target.push(stringToChunk('' + html)); + } } - target.push(stringToChunk(html)); } if (typeof children === 'string' && children[0] === '\n') { target.push(leadingNewline); From 2e69951b592d6b55632d64aef768d7a68dc0e83d Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 14 Apr 2021 22:06:12 -0400 Subject: [PATCH 3/3] Fix forward refs --- packages/react-server/src/ReactFizzServer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/react-server/src/ReactFizzServer.js b/packages/react-server/src/ReactFizzServer.js index a27a845f422e7..1a838be226c8b 100644 --- a/packages/react-server/src/ReactFizzServer.js +++ b/packages/react-server/src/ReactFizzServer.js @@ -767,7 +767,8 @@ function renderForwardRef( props: Object, ref: any, ): void { - renderWithHooks(request, task, type, props, ref); + const children = renderWithHooks(request, task, type.render, props, ref); + renderNodeDestructive(request, task, children); } function renderMemo(