Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive <noscript> rehydration text difference warning in React 16 #11157

Merged
merged 4 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/ssr/src/components/Chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export default class Chrome extends Component {
<title>{this.props.title}</title>
</head>
<body>
<noscript
dangerouslySetInnerHTML={{
__html: `<b>Enable JavaScript to run this app.</b>`,
}}
/>
{this.props.children}
<script
dangerouslySetInnerHTML={{
Expand Down
13 changes: 13 additions & 0 deletions src/renderers/dom/fiber/ReactDOMFiberComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ if (__DEV__) {
parent.tagName,
);
testElement.innerHTML = html;

if (parent.tagName === 'NOSCRIPT') {
// <noscript> content is parsed as text, but only if the browser parses it
// together with <noscript> tag itself. So we have to wrap it once more.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a = document.createElement('noscript');
a.innerHTML = '<b>foobar</b>';
console.log(a.childNodes);

Outputs a single TextNode for me in both Chrome and Edge (it does not parse the b-tag), which seems to contradict the comment and the whole reason for this existing... right? Is there more to it than that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. What you're saying is right for document.createElement(), but not for document.implementation.createHTMLDocument('').createElement() which is what we're using. I'm not sure why there is a difference (is it because it doesn't have a body or something like this?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be a difference between the wrapper and not in this test:
http://jsbin.com/mofexaxaya/2/edit?js,console

What is different?

var wrapperElement = document.createElement('div');
wrapperElement.innerHTML = '<noscript>' + html + '</noscript>';
var noscriptElement = wrapperElement.firstElementChild;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just firstChild (since it goes back longer in browser support and seems sufficient)?

// Make Flow happy (but it'll always be there).
if (noscriptElement !== null && typeof noscriptElement !== 'undefined') {
return noscriptElement.innerHTML;
}
}

return testElement.innerHTML;
};
}
Expand Down