Skip to content

Commit

Permalink
Add separator comment between text nodes (facebook#21099)
Browse files Browse the repository at this point in the history
This is needed to avoid mutating the DOM during hydration. This *always*
adds it even when it's just text children.

We need to avoid this overhead but it's a somewhat tricky problem to solve
so we defer the optimization to later.
  • Loading branch information
sebmarkbage authored and koto committed Jun 15, 2021
1 parent 3aa29ae commit c4213f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('ReactDOMFizzServer', () => {
<div>hello world</div>,
);
const result = await readResult(stream);
expect(result).toBe('<div>hello world</div>');
expect(result).toMatchInlineSnapshot(`"<div>hello world<!-- --></div>"`);
});

// @gate experimental
Expand Down Expand Up @@ -93,7 +93,9 @@ describe('ReactDOMFizzServer', () => {
expect(isComplete).toBe(true);

const result = await readResult(stream);
expect(result).toBe('<div><!--$-->Done<!--/$--></div>');
expect(result).toMatchInlineSnapshot(
`"<div><!--$-->Done<!-- --><!--/$--></div>"`,
);
});

// @gate experimental
Expand Down
12 changes: 7 additions & 5 deletions packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ describe('ReactDOMFizzServer', () => {
);
startWriting();
jest.runAllTimers();
expect(output.result).toBe('<div>hello world</div>');
expect(output.result).toMatchInlineSnapshot(
`"<div>hello world<!-- --></div>"`,
);
});

// @gate experimental
Expand All @@ -81,8 +83,8 @@ describe('ReactDOMFizzServer', () => {
'<!doctype html><html><head><title>test</title><head><body>';
// Then React starts writing.
startWriting();
expect(output.result).toBe(
'<!doctype html><html><head><title>test</title><head><body><div>hello world</div>',
expect(output.result).toMatchInlineSnapshot(
`"<!doctype html><html><head><title>test</title><head><body><div>hello world<!-- --></div>"`,
);
});

Expand Down Expand Up @@ -129,8 +131,8 @@ describe('ReactDOMFizzServer', () => {
'<!doctype html><html><head><title>test</title><head><body>';
// Then React starts writing.
startWriting();
expect(output.result).toBe(
'<!doctype html><html><head><title>test</title><head><body><div><!--$-->Done<!--/$--></div>',
expect(output.result).toMatchInlineSnapshot(
`"<!doctype html><html><head><title>test</title><head><body><div><!--$-->Done<!-- --><!--/$--></div>"`,
);
});

Expand Down
9 changes: 8 additions & 1 deletion packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export function pushEmpty(
}
}

const textSeparator = stringToPrecomputedChunk('<!-- -->');

export function pushTextInstance(
target: Array<Chunk | PrecomputedChunk>,
text: string,
Expand All @@ -113,7 +115,12 @@ export function pushTextInstance(
if (assignID !== null) {
pushDummyNodeWithID(target, responseState, assignID);
}
target.push(stringToChunk(encodeHTMLTextNode(text)));
if (text === '') {
// Empty text doesn't have a DOM node representation and the hydration is aware of this.
return;
}
// TODO: Avoid adding a text separator in common cases.
target.push(stringToChunk(encodeHTMLTextNode(text)), textSeparator);
}

const startTag1 = stringToPrecomputedChunk('<');
Expand Down

0 comments on commit c4213f7

Please sign in to comment.