Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created August 30, 2016 02:37
Show Gist options
  • Save wchargin/00cf9dde137b26fb00c74f74742b1a01 to your computer and use it in GitHub Desktop.
Save wchargin/00cf9dde137b26fb00c74f74742b1a01 to your computer and use it in GitHub Desktop.
attempt to get ssr working with noscripts (react/facebook#7607)
diff --git a/src/renderers/dom/server/__tests__/ReactServerRendering-test.js b/src/renderers/dom/server/__tests__/ReactServerRendering-test.js
index 1c37530..391ecbe 100644
--- a/src/renderers/dom/server/__tests__/ReactServerRendering-test.js
+++ b/src/renderers/dom/server/__tests__/ReactServerRendering-test.js
@@ -123,6 +123,33 @@ describe('ReactServerRendering', function() {
);
});
+ it('should not put React ID inside <noscript>s', function() {
+ class Component extends React.Component {
+ render() {
+ return <div>
+ <noscript><span>Shhh...</span></noscript>
+ <span>Hello!</span>
+ </div>;
+ }
+ }
+
+ var response = ReactServerRendering.renderToString(
+ <Component />
+ );
+ expect(response).toMatch(
+ '<div ' + ROOT_ATTRIBUTE_NAME + '="" ' +
+ ID_ATTRIBUTE_NAME + '="[^"]+" ' +
+ ReactMarkupChecksum.CHECKSUM_ATTR_NAME + '="[^"]+">' +
+ '<noscript ' + ID_ATTRIBUTE_NAME + '="[^"]+">' +
+ '<span>Shhh...</span>' +
+ '</noscript>' +
+ '<span ' + ID_ATTRIBUTE_NAME + '="[^"]+">' +
+ 'Hello!' +
+ '</span>' +
+ '</div>'
+ );
+ });
+
it('should only execute certain lifecycle methods', function() {
function runTest() {
var lifecycle = [];
diff --git a/src/renderers/dom/shared/ReactDOMComponent.js b/src/renderers/dom/shared/ReactDOMComponent.js
index 88ed489..0573d98 100644
--- a/src/renderers/dom/shared/ReactDOMComponent.js
+++ b/src/renderers/dom/shared/ReactDOMComponent.js
@@ -524,7 +524,12 @@ ReactDOMComponent.Mixin = {
context
) {
this._rootNodeID = globalIdCounter++;
- this._domID = hostContainerInfo._idCounter++;
+ // STOPSHIP TODO(william): Don't hack this on to context;
+ // figure out the right way to thread it through instead.
+ this._domID = hostContainerInfo._idCounter;
+ if (!context.__inNoscript) {
+ hostContainerInfo._idCounter++;
+ }
this._hostParent = hostParent;
this._hostContainerInfo = hostContainerInfo;
@@ -648,7 +653,14 @@ ReactDOMComponent.Mixin = {
this._createInitialChildren(transaction, props, context, lazyTree);
mountImage = lazyTree;
} else {
- var tagOpen = this._createOpenTagMarkupAndPutListeners(transaction, props);
+ // STOPSHIP TODO(william): Don't hack this on to context;
+ // figure out the right way to thread it through instead.
+ var inNoscript = context.__inNoscript || false;
+ var tagOpen = this._createOpenTagMarkupAndPutListeners(
+ transaction, props, inNoscript);
+ if (this._tag === 'noscript') {
+ context = Object.assign({}, context, {__inNoscript: true});
+ }
var tagContent = this._createContentMarkup(transaction, props, context);
if (!tagContent && omittedCloseTags[this._tag]) {
mountImage = tagOpen + '/>';
@@ -721,9 +733,16 @@ ReactDOMComponent.Mixin = {
* @private
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction
* @param {object} props
+ * @param {boolean} inNoscript true if one of our proper ancestors is
+ * a noscript tag, in which case we won't attach the react ID (see #7607)
* @return {string} Markup of opening tag.
*/
- _createOpenTagMarkupAndPutListeners: function(transaction, props) {
+ _createOpenTagMarkupAndPutListeners: function(
+ transaction,
+ props,
+ inNoscript
+ ) {
+ // TODO STOPSHIP strip this
var ret = '<' + this._currentElement.type;
for (var propKey in props) {
@@ -772,7 +791,9 @@ ReactDOMComponent.Mixin = {
if (!this._hostParent) {
ret += ' ' + DOMPropertyOperations.createMarkupForRoot();
}
- ret += ' ' + DOMPropertyOperations.createMarkupForID(this._domID);
+ if (!inNoscript) {
+ ret += ' ' + DOMPropertyOperations.createMarkupForID(this._domID);
+ }
return ret;
},
diff --git a/src/renderers/dom/shared/ReactDOMEmptyComponent.js b/src/renderers/dom/shared/ReactDOMEmptyComponent.js
index 0a7b8c6..93603c2 100644
--- a/src/renderers/dom/shared/ReactDOMEmptyComponent.js
+++ b/src/renderers/dom/shared/ReactDOMEmptyComponent.js
@@ -31,7 +31,12 @@ Object.assign(ReactDOMEmptyComponent.prototype, {
hostContainerInfo,
context
) {
- var domID = hostContainerInfo._idCounter++;
+ // STOPSHIP TODO(william): Don't hack this on to context;
+ // figure out the right way to thread it through instead.
+ var domID = hostContainerInfo._idCounter;
+ if (!context.__inNoscript) {
+ hostContainerInfo._idCounter++;
+ }
this._domID = domID;
this._hostParent = hostParent;
this._hostContainerInfo = hostContainerInfo;
diff --git a/src/renderers/dom/shared/ReactDOMTextComponent.js b/src/renderers/dom/shared/ReactDOMTextComponent.js
index 860bc33..bfd8f26 100644
--- a/src/renderers/dom/shared/ReactDOMTextComponent.js
+++ b/src/renderers/dom/shared/ReactDOMTextComponent.js
@@ -79,7 +79,12 @@ Object.assign(ReactDOMTextComponent.prototype, {
}
}
- var domID = hostContainerInfo._idCounter++;
+ // STOPSHIP TODO(william): Don't hack this on to context;
+ // figure out the right way to thread it through instead.
+ var domID = hostContainerInfo._idCounter;
+ if (!context.__inNoscript) {
+ hostContainerInfo._idCounter++;
+ }
var openingValue = ' react-text: ' + domID + ' ';
var closingValue = ' /react-text ';
this._domID = domID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment