简体   繁体   English

ReactJS服务器端渲染似乎并没有改变客户端的情况

[英]Reactjs server-side rendering seems to not change a thing on the client side

I'm trying to optimize my big ReactJS project and I've stumbled upon a conclusion, that React code in the browser is not taking any advantage of server rendering. 我正在尝试优化我的大型ReactJS项目,但偶然发现了一个结论,即浏览器中的React代码没有利用服务器渲染的任何优势。 I'm sure that server-side rendering is working properly, I can see data in the page source returned from server, with all the data-reactid s. 我确定服务器端呈现工作正常,我可以看到服务器返回的页面源中的data-reactid以及所有data-reactid

Setup : server-side code is rendered in Node. 设置 :服务器端代码在Node中呈现。 React version 15.0.1. 反应版本15.0.1。 I'm using ClojureScript and rum, but it shouldn't matter in the end 我正在使用ClojureScript和朗姆酒,但最后没关系

Test environment : Using React.addons.Perf , wrapping it around initial mount on the client side. 测试环境 :使用React.addons.Perf ,将其包装在客户端的初始安装周围。

Test 1 : proper server-side rendering 测试1 :正确的服务器端渲染

Total time around 1.8s, Inclusive Time of root component about 1.5s. 总时间约为1.8秒,包含根部分的时间约为1.5秒。 React.addons.Perf.printWasted() shows nothing. React.addons.Perf.printWasted()什么也不显示。

But what is the most puzzling part for me is that React.addons.Perf.printOperations() shows one operation: "set innerHTML" with the whole app's html (eg <div data-reactroot=\\"\\" data-reactid=\\"1\\"><div id=\\"wrapper\\"... ). 但是对我来说,最令人困惑的部分是React.addons.Perf.printOperations()显示了一个操作:使用整个应用的html来"set innerHTML" (例如<div data-reactroot=\\"\\" data-reactid=\\"1\\"><div id=\\"wrapper\\"... )。 Like the whole server-side rendering result was discarded and this client-rendered HTML is inserted in the DOM, probably causing re-rendeding. 就像整个服务器端的渲染结果都被丢弃一样,此客户端渲染的HTML插入到DOM中,可能导致重新渲染。

Test 2 : invalid checksum; 测试2 :校验和无效; by purpose I've added some extra attribute on the server-side only. 出于目的,我仅在服务器端添加了一些额外的属性。

Of course the "React attempted to reuse markup in a container but the checksum was invalid." 当然,“ React尝试重用容器中的标记,但校验和无效。” warning appears. 警告出现。

However, everything else is similar to the Test1 above! 但是,其他所有内容都与上面的Test1相似! Similar times, and also the single "set innerHTML" operation. 类似的时间,还有单个的"set innerHTML"操作。 This confirms for me that even if checksum is correct, React ignores pre-rendered markup (because it seems to do the same operations in the similar times whether checksum is correct or not) 这为我确认,即使校验和正确,React也会忽略预渲染的标记(因为无论校验和正确与否,它似乎都在相似的时间执行相同的操作)

Test 3 : turned off server-side rendering; 测试3 :关闭服务器端渲染; returning empty mount-point container. 返回空的安装点容器。

Times about 0.2s longer. 时间约0.2s。 The only real difference now is that there are many operations, most of them update attribute or update styles and one set innerHTML with {"node":"<not serializable>","children":[],"html":null,"text":null} . 现在唯一真正的区别是,有许多操作,其中大多数操作update attributeupdate styles并且一set innerHTML {"node":"<not serializable>","children":[],"html":null,"text":null}带有{"node":"<not serializable>","children":[],"html":null,"text":null}

Question/Problem 问题/问题

I don't know what to think about it. 我不知道该怎么想。 React advertises SSR as a way to speed up application bootstrap, as it does not need to generate DOM tree again (or rather: insert it in the document; it still need to be rendered in order to compare checksums, right?). React宣传SSR作为加快应用程序引导的一种方式,因为它不需要再次生成DOM树(或者:将其插入文档中;仍然需要呈现它以便比较校验和,对吗?)。 But for me it seems like nothing is gained. 但是对我来说似乎没有任何收获。

Can someone who is successfully utilizing SSR tell me what are his/her reading in printWasted() and printOperations() ? 成功利用SSR的人可以告诉我他/她在printWasted()printOperations()什么吗? The only thing I managed to find was this audit: https://github.com/reddit/reddit-mobile/issues/247#issuecomment-118398416 (only printWasted , though) - none of my tests shows any entires in printWasted . 我唯一找到的就是这次审核: https : //github.com/reddit/reddit-mobile/issues/247#issuecomment-118398416 (虽然只有printWasted )-我的测试都没有显示出printWasted中的任何完整printWasted

Well, it all was simple: after upgrading to React 15.4.2 Test 1 shows no operations, as I expected it to in the first place. 好了,一切都很简单:升级到React 15.4.2之后, 测试1并没有显示任何操作,正如我最初期望的那样。 So in the end it all was issue of (not so) old library version. 因此,最后都是(不是)旧库版本的问题。 Yet the times of mount are still very similar. 但是坐骑的时间仍然非常相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM