react icon indicating copy to clipboard operation
react copied to clipboard

Incorrect "unable to reuse markup" warning message

Open th0r opened this issue 11 years ago • 22 comments

React 0.13 improved warning message when it fails to reuse server rendered markup and now shows the exact incorrect part. But it shows wrong part if there are text nodes containing some html-escaped chars (single quotes, double quotes etc.) somewhere before this incorrect part. For example, React shows this message...

(client) sxg4h94hs.2.1.0.1">{"Hi!"
(server) sxg4h94hs.2.1.0.1">"Hi!"

...but this part is ok. The problematic markup (which actually renders differently on client and server) is located after this place in DOM. If i remove "Hi!" text from the rendered markup, React shows correct warning message and points to the real problematic place.

th0r avatar Mar 13 '15 09:03 th0r

+1

klimashkin avatar Mar 13 '15 09:03 klimashkin

https://github.com/facebook/react/blob/75a8bc96b10ef364c751bbc7a0594dbdb7e8663c/src/browser/ui/ReactMount.js#L819

It's comparing it against outerHTML which isn't guaranteed to be identical to the initial markup (and never is on IE8 too).

cc @arnihermann

syranide avatar Mar 13 '15 11:03 syranide

I was aware that outerHTML might cause issues like this at some point :\ Me and @vjeux decided that since it was just a debug message, we were going to try it out and see how it worked out.

innerHTML might be more accurate but we'd have to perform it on the parent element and find the rootMarkup in the innerHTML string. I'm not sure but I think neither outerHTML nor innerHTML will guarantee to give back the same html as the initial markup.

I'm happy to work on it to prevent these kind of issues but I'd like to call for suggestions on how to fix permanently.

@th0r which browser were you using when this occured?

arnihermann avatar Mar 14 '15 00:03 arnihermann

@arnihermann Latest Chrome.

th0r avatar Mar 14 '15 09:03 th0r

@arnihermann innerHTML vs outerHTML makes no difference, the issue is that neither of them gives any guarantee to return the same markup you put in. The only reasonable solution I see is to render the new markup as well and read outerHTML from it and compare that.

syranide avatar Mar 14 '15 09:03 syranide

+1

knightli avatar Mar 30 '15 05:03 knightli

+1. I'm wondering if use of escapeTextContentForBrowser on 'outerHTML' before comparing against client markup couldn't be a solution ?

rayquiz avatar Apr 03 '15 07:04 rayquiz

I'm wondering if use of escapeTextContentForBrowser on 'outerHTML' before comparing against client markup couldn't be a solution ?

I don't think that'll work. Copy/pasting my comments from #3465

I'm not sure what would be a good way to fix this. Re-escaping " to " and ' to ' in rootMarkup before comparing calling firstDifferenceIndex (https://github.com/facebook/react/blob/a321247ee5c6a51f4cf9feec4c8989a769bcd78a/src/browser/ui/ReactMount.js#L830) would likely escape unrelated characters as well and unescaping both markup and rootMarkup before comparing would not produce an exact diff (unless we were to keep track of the unescapes performed before comparing and then revert them while producing the diff string, but that sounds like going through more trouble than might be needed).

voideanvalue avatar Apr 05 '15 01:04 voideanvalue

This issue reliably occurs with boolean attributes like disabled and checked which React renders differently on the client and server:

(client) ect-prompt" disabled value="" data-react
(server) ect-prompt" disabled="" value="" data-re

Related: #961, #1005

jordaaash avatar May 02 '15 20:05 jordaaash

@jordansexton The message here is a red herring – it's not those attributes themselves that are the problem, but something else in your markup.

sophiebits avatar May 02 '15 23:05 sophiebits

@spicyj Hmm. Any pointers on how to diagnose this since the invariant error message is flawed?

jordaaash avatar May 03 '15 01:05 jordaaash

@jordansexton My best suggestion is to call React.renderToString on the server and on the client and compare them. They'll have different root node IDs so the strings won't match up directly but maybe you can do a find and replace to make them match up.

sophiebits avatar May 05 '15 23:05 sophiebits

@spicyj Oof. Okay, thanks for the idea!

jordaaash avatar May 05 '15 23:05 jordaaash

@jordansexton I am having the exact same issue. When I inspect the text/html content of the initial HTTP request the html is actually correct:

 disabled style="opacity:0.6;"

but somehow on DOM rendering the browser seems to parse and render it as:

disabled="" style="opacity:06;"

but the time the client side React code try to reuse the markup it complains about the difference:

 (client) olor-white" disabled style="opacity:0.6;
 (server) olor-white" disabled="" style="opacity:0

@spicyj so the markup between the client and React.renderToString on the server are the same, but the browser reinterprets it to be something different.

due to the fix for this issue https://github.com/facebook/react/issues/961, I am unable to sent over disabled="disabled" instead by using a syntax of

disabled: if true then "disabled" else false

since it will always be converted into a bare attribute and then parsed by browser as I have described above.

goldensunliu avatar Jun 13 '15 18:06 goldensunliu

@goldensunliu I actually fixed this issue with the method suggested by @spicyj. The trick is not to compare the markup normally rendered on the client with React.render to the markup rendered on the server with React.renderToString. It's to call React.renderToString on the client and the server, and compare the results with a diff viewer.

You will see what he was saying (that instances of disabled/disabled="", checked/checked="", etc. are red herrings) and that something else in the markup is off, completely unrelated. In my case, the mismatch was image src attributes: I was using window.devicePixelRatio on the client side for rendering @2x images, but on the server I was rendering them with the regular source. I changed to using srcset on both since it doesn't require detection unavailable on the server and the bug was gone.

jordaaash avatar Jun 13 '15 20:06 jordaaash

@spicyj This comes up often, and is more than a bit surprising. In addition, the process to diagnose the issue is pretty painful: you have to change some code on the client to use renderToString, and before you get into the diff, you have to replace the data-react-id hash generated on the server with .0 to compare the two usefully. What do you think of the idea of doing this in the warning message so it displays the actual mismatch? I know it would be expensive, but the warning is only thrown in development anyway.

jordaaash avatar Jun 13 '15 20:06 jordaaash

Thanks @jordansexton. That makes quite a lot of sense. I will take a look at what the actual mismatch is. The process seems archaic but gets the job done.

goldensunliu avatar Jun 15 '15 12:06 goldensunliu

This has probably has been fixed in #4324 by @drd.

arnihermann avatar Jul 17 '15 02:07 arnihermann

Reading through the commits, I suspect it's only partially fixed. #4324 deals with HTML entities being rendered differently. It doesn't fix the bad error indication for actually different server and client rendering, which is partly what this issue pertains to since #3685 was folded into it. Maybe this issue should be closed and that issue reopened?

jordaaash avatar Jul 17 '15 03:07 jordaaash

@jordansexton As far as I can tell, these are all the same problem: the warning message not being useful when browsers slightly change the format of HTML that gets passed through.

sophiebits avatar Jul 18 '15 04:07 sophiebits