Skip to content

Commit

Permalink
Emit reactroot attribute on the first element we discover
Browse files Browse the repository at this point in the history
This may not be the first root element if the root is a fragment and the
second one unsuspends first. But this tag doesn't work well for root
fragments anyway.
  • Loading branch information
sebmarkbage committed Apr 1, 2021
1 parent dbc8fb9 commit cf3aa47
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
OVERLOADED_BOOLEAN,
NUMERIC,
POSITIVE_NUMERIC,
ROOT_ATTRIBUTE_NAME,
} from '../shared/DOMProperty';
import {isUnitlessNumber} from '../shared/CSSProperty';

Expand Down Expand Up @@ -59,6 +60,7 @@ export type ResponseState = {
sentCompleteSegmentFunction: boolean,
sentCompleteBoundaryFunction: boolean,
sentClientRenderFunction: boolean,
hasEmittedRoot: boolean,
};

// Allows us to keep track of what we've already written so we can refer back to it.
Expand All @@ -74,6 +76,7 @@ export function createResponseState(
sentCompleteSegmentFunction: false,
sentCompleteBoundaryFunction: false,
sentClientRenderFunction: false,
hasEmittedRoot: false,
};
}

Expand All @@ -94,7 +97,7 @@ type InsertionMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;

// Lets us keep track of contextual state and pick it back up after suspending.
export type FormatContext = {
insertionMode: InsertionMode, // root/svg/html/mathml/table
insertionMode: InsertionMode, // svg/html/mathml/table
selectedValue: null | string | Array<string>, // the selected value(s) inside a <select>, or null outside <select>
};

Expand Down Expand Up @@ -487,6 +490,14 @@ const endOfStartTagSelfClosing = stringToPrecomputedChunk('/>');
const idAttr = stringToPrecomputedChunk(' id="');
const attrEnd = stringToPrecomputedChunk('"');

const reactRootAttribute = stringToPrecomputedChunk(' ' + ROOT_ATTRIBUTE_NAME + '=""');
function pushReactRoot(target: Array<Chunk | PrecomputedChunk>, responseState: ResponseState): void {
if (!responseState.hasEmittedRoot) {
responseState.hasEmittedRoot = true;
target.push(reactRootAttribute);
}
}

function pushID(
target: Array<Chunk | PrecomputedChunk>,
responseState: ResponseState,
Expand Down Expand Up @@ -618,6 +629,7 @@ function pushStartSelect(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTag);
pushInnerHTML(target, innerHTML, children);
Expand Down Expand Up @@ -731,6 +743,7 @@ function pushStartOption(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTag);
return children;
Expand Down Expand Up @@ -818,6 +831,7 @@ function pushInput(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTagSelfClosing);
return null;
Expand Down Expand Up @@ -882,6 +896,7 @@ function pushStartTextArea(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTag);

Expand Down Expand Up @@ -958,6 +973,7 @@ function pushSelfClosing(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTagSelfClosing);
return null;
Expand Down Expand Up @@ -994,6 +1010,7 @@ function pushStartMenuItem(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTag);
return null;
Expand Down Expand Up @@ -1032,6 +1049,7 @@ function pushStartGenericElement(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTag);
pushInnerHTML(target, innerHTML, children);
Expand Down Expand Up @@ -1086,6 +1104,7 @@ function pushStartCustomElement(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTag);
pushInnerHTML(target, innerHTML, children);
Expand Down Expand Up @@ -1127,6 +1146,7 @@ function pushStartPreformattedElement(
if (assignID !== null) {
pushID(target, responseState, assignID, props.id);
}
pushReactRoot(target, responseState);

target.push(endOfStartTag);

Expand Down

0 comments on commit cf3aa47

Please sign in to comment.