Rails 3.1 http 流 - js 在头部还是正文底部?
Rails 3.1 中提供了启用 HTTP 流的选项,以便您的页面可以分块进行。在关于此功能的 Railscast 中,Ryan 建议启用此功能是一个好主意,这样您的 CSS 和 JavaScript 就可以在页面的其余部分仍在渲染时被下拉。
我一直遵循这样的准则:在加载所有页面内容后,脚本应位于页面底部,以便减少感知的加载时间,但这样做不会利用 HTTP 流。
您认为现在最好的做法是什么?
In Rails 3.1 there's the option to enable HTTP streaming so that your page can be brought down in chunks. In the Railscast on this feature, Ryan recommended that it would be a good idea to enable this so that your CSS and JavaScript can be pulled down while the rest of your page is still being rendered.
I've always followed the guideline that scripts should be at the bottom of the page after all the page content is loaded so that it would reduce perceived loading time, but by doing this it won't be taking advantage of the HTTP streaming.
What do you think is the best practice now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这是一个很好的问题;我觉得有必要向谷歌寻求答案。
将脚本资源放在页面底部的理由是为了防止阻塞浏览器的渲染器,否则浏览器的渲染器可能会在屏幕上绘制像素,以便在加载页面的其余功能时让用户保持忙碌。对于 HTTP 流,我们谈论的是能够对服务器这一瓶颈采取一些措施。当我们等待所有这些昂贵的数据库查询和后端服务调用完成时,我们可以加载 JS/CSS 资源。
在我看来,有一个转折点,你应该绕过这个转折点。您的资产与否;你的资产。如果您的 JS/CSS 资源可以在您的服务器准备好其余响应之前下载,这只是净性能增益。
不要<头>页面的资产,如果:
)页面的资产如果:
这听起来对吗?
I think this is an excellent question; one for which I felt compelled to Google for an answer.
The argument for putting script assets at the bottom of the page was to prevent blocking a browser's renderer that could otherwise be painting pixels on the screen to keep the user busy while the rest of the page's functionality loaded. With HTTP streaming, we're talking about being able to do something about the server being the bottleneck. While we wait for all those expensive database queries and backend service calls to finish, we can load JS/CSS assets.
It seems to me that there's a tipping point around which you should <head> your assets or not <head> your assets. This is only a net performance gain if your JS/CSS assets can be downloaded before your server has the rest of the response ready.
Don't <head> a page's assets if:
Do <head> a page's assets if:
Does that sound about right?
对于一般情况,我认为不幸的是他们仍然会触底。原因是 Mac 版 Safari 在开始发出资源请求之前会缓冲 1024 字节(iPhone 和 iPad 版 Safari 会缓冲 512 字节)。
由于文档的头部通常较小,Safari 用户仍然会得到普通的糟糕体验。
根据我和 Hongli Lai 一起做的一些测试,Firefox、Opera 和 IE8 不缓冲,而 Chrome 缓冲 252 字节。
我仍在对此进行研究,但截至撰写本文时,这是我的结论。
For the general case, I think unfortunately they are still going to go to the bottom. Reason is Safari for Mac buffers 1024 bytes before it starts to issue requests for assets (and Safari for iPhone and iPad buffer 512 bytes).
Since the head of a document is typically smaller, Safari users would still get the ordinary bad experience.
Firefox, Opera, and IE8 do not buffer, and Chrome buffers 252 bytes, according to some test I've done together with Hongli Lai.
I am still doing research about this, but as of this writing this is my conclusion.
对主观问题的主观回答:
Subjective answer to a subjective question: