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

Isomorphic Rendering with inserted html broken. (15.0) #6452

Closed
DylanPiercey opened this issue Apr 8, 2016 · 14 comments
Closed

Isomorphic Rendering with inserted html broken. (15.0) #6452

DylanPiercey opened this issue Apr 8, 2016 · 14 comments

Comments

@DylanPiercey
Copy link

In React 0.14x and below inserted dom nodes inserted by plugins such as adblock, browser sync, or otherwise were ignored.

Now in React 15 these new nodes cause the

index.js:65 Invariant Violation: You're trying to render a component to the document using server rendering but the checksum was invalid. This usually means you rendered a different component type or props on the client from the one on the server, or your render() methods are impure. React cannot handle this case due to cross-browser quirks by rendering at the document root. You should look for environment dependent code in your components and ensure the props are the same client and server side:

I'd say this is a regression since it breaks (full page) isomorphic rendering for anyone using plugins (or scripts) that insert dom when loaded.

I'd say react should just ignore anything without a "data-reactid" when mounting server rendered code.


Edit:

On top of this React now throws in this case, where as before it would gracefully continue.

@DylanPiercey DylanPiercey changed the title Isomorphic Rendering with inserted html. Isomorphic Rendering with inserted html broken. (15.0) Apr 8, 2016
@zpao
Copy link
Member

zpao commented Apr 8, 2016

What does the rest of that warning say?

@buzinas
Copy link

buzinas commented Apr 8, 2016

@zpao I'm having problems with server side rendering also.

When using 15.0.0-rc.2, everything was working smooth, but after update, I'm receiving this message:

{"error":"React.__spread is not a function"}

Tried to reinstall all my dependencies and the error persists. Tried to install the previous version (RC2), and it started working again, smoothly.

@petetnt
Copy link
Contributor

petetnt commented Apr 8, 2016

@buzinas See #6444

@zpao
Copy link
Member

zpao commented Apr 11, 2016

I'm pretty sure this is just the same as #6451 so I'm going to close this out. Let me know if I'm incorrect in that assumption.

@zpao zpao closed this as completed Apr 11, 2016
@DylanPiercey
Copy link
Author

@zpao this is unrelated to #6451. Here is the full stack trace:

index.js:65 Invariant Violation: You're trying to render a component to the document using server rendering but the checksum was invalid. This usually means you rendered a different component type or props on the client from the one on the server, or your render() methods are impure. React cannot handle this case due to cross-browser quirks by rendering at the document root. You should look for environment dependent code in your components and ensure the props are the same client and server side:
 (client)  data-reactid="13"></head><body data-rea
 (server)  data-reactid="13"><style type="text/css
    at invariant (http://efca.localhost.ca:3002/admin/js/app.js:34199:15)
    at Object.ReactMount._mountImageIntoNode (http://efca.localhost.ca:3002/admin/js/app.js:83566:91)
    at Object.wrapper [as _mountImageIntoNode] (http://efca.localhost.ca:3002/admin/js/app.js:84437:21)
    at mountComponentIntoNode (http://efca.localhost.ca:3002/admin/js/app.js:83225:14)
    at ReactReconcileTransaction.Mixin.perform (http://efca.localhost.ca:3002/admin/js/app.js:87875:20)
    at batchedMountComponentIntoNode (http://efca.localhost.ca:3002/admin/js/app.js:83239:15)
    at ReactDefaultBatchingStrategyTransaction.Mixin.perform (http://efca.localhost.ca:3002/admin/js/app.js:87875:20)
    at Object.ReactDefaultBatchingStrategy.batchedUpdates (http://efca.localhost.ca:3002/admin/js/app.js:81259:19)
    at Object.batchedUpdates (http://efca.localhost.ca:3002/admin/js/app.js:85595:20)
    at Object.ReactMount._renderNewRootComponent (http://efca.localhost.ca:3002/admin/js/app.js:83392:18)

The issue is to due with nodes (in this case a <style> tag used by an adblocker) being inserted into the document before react loads breaking the checksum. In the past react would just ignore things like this.

This works in 15.0.0-rc.1 and below.

Ideally react would ignore anything without a data-reactid when validating the checksum if possible, i'm not sure how it worked before.

@gaearon gaearon reopened this Apr 12, 2016
@zpao
Copy link
Member

zpao commented Apr 12, 2016

@spicyj How has the checksum check changed now that we're using createElement? Before it would work in this case because we'd be looking at the checksum of the generated markup string which wouldn't have seen the actual DOM. Or is this actually a case where the warning uses outerHTML so this mismatch is just pointing at the wrong thing.

@chandlerprall
Copy link

In my application we have a chunk of code that is executed when the window object exists which builds a new stylesheet and appends it to head. Experienced identical error as @DylanPiercey, and I ended up solving during my upgrade to React 15 by building up the stylesheet object as part of a state and injecting it at component render. The error message looks correct to me.

Interestingly this was a race condition as sometimes React's initial render would happen before the additional stylesheet was injected, and all would be fine.

@sophiebits
Copy link
Collaborator

@zpao Nothing's changed – we still generate HTML client-side when reviving server-rendered markup.

@sophiebits
Copy link
Collaborator

@DylanPiercey The checksum still would not be thrown off by anything added by an adblocker like this (and should work identically to 0.14.7). The error message is misleading in this case though. If you disable your adblocker, what error do you get instead?

@sophiebits
Copy link
Collaborator

Going to close this because it's unlikely this is actually a React bug but please reply and we can continue to debug.

@DylanPiercey
Copy link
Author

@spicyj Sorry for not replying quicker. I tried 0.15x on a couple other projects with similar setups and it works fine so this must be something specific to the project. This can stay closed, thanks.

@sophiebits
Copy link
Collaborator

No worries, I just wanted to clarify my earlier comments. Didn't mean to put any pressure on you to reply quickly! If you have more trouble with that project then feel free to post and we'll figure out what's going on.

@manughuman
Copy link

manughuman commented Nov 6, 2017

@DylanPiercey - Were you able to solve this issue. if yes can you share some pointers.. I am facing the similar issue on upgrading react from 0.14x to 15 ..

invariant.js:42 Uncaught Error: You're trying to render a component to the document using server rendering but the checksum was invalid. This usually means you rendered a different component type or props on the client from the one on the server, or your render() methods are impure. React cannot handle this case due to cross-browser quirks by rendering at the document root. You should look for environment dependent code in your components and ensure the props are the same client and server side:
(client) 27"><a href="Mongo" data-reactid="28"><h
(server) 27"><a href="Mongo" class="activeNav" da
    at invariant (invariant.js:42)
    at Object._mountImageIntoNode (ReactMount.js:502)
    at mountComponentIntoNode (ReactMount.js:110)
    at ReactReconcileTransaction.perform (Transaction.js:141)
    at batchedMountComponentIntoNode (ReactMount.js:124)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:141)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:60)
    at Object.batchedUpdates (ReactUpdates.js:95)
    at Object._renderNewRootComponent (ReactMount.js:317)
    at Object._renderSubtreeIntoContainer (ReactMount.js:399)

@DylanPiercey
Copy link
Author

DylanPiercey commented Nov 9, 2017

@mgmn ultimately we made 100% sure that the server rendered output was the same (this was tricky for some dates for unknown users). There was also setup a way to catch the error and discard the dom if needed.

My larger problem and the reason I created this issue was that react was broken for people who used browser extensions in some cases but I believe that was fixed when they removed data-reactid.

Sorry if that doesn’t help any. This issue is also pretty old so perhaps it was fixed in react 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants