Rails 3.1 http 流 - js 在头部还是正文底部?

发布于 2024-11-10 02:45:35 字数 220 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仄言 2024-11-17 02:45:35

我认为这是一个很好的问题;我觉得有必要向谷歌寻求答案。

将脚本资源放在页面底部的理由是为了防止阻塞浏览器的渲染器,否则浏览器的渲染器可能会在屏幕上绘制像素,以便在加载页面的其余功能时让用户保持忙碌。对于 HTTP 流,我们谈论的是能够对服务器这一瓶颈采取一些措施。当我们等待所有这些昂贵的数据库查询和后端服务调用完成时,我们可以加载 JS/CSS 资源。

在我看来,有一个转折点,你应该绕过这个转折点。您的资产与否;你的资产。如果您的 JS/CSS 资源可以在您的服务器准备好其余响应之前下载,这只是净性能增益。

不要<头>页面的资产,如果:

  • 您从服务器端缓存中提供页面
  • 您的服务器响应时间比 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:

  • You're serving the page out of a server-side cache
  • Your server's response time is faster than your JS/CSS' actual asset load time (when calculating load time, consider carefully whether those assets are likely to have already been cached on the client side or not)

Do <head> a page's assets if:

  • Your server's takes longer to respond than it would take to load all of your JS/CSS assets while waiting

Does that sound about right?

翻身的咸鱼 2024-11-17 02:45:35

对于一般情况,我认为不幸的是他们仍然会触底。原因是 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.

听你说爱我 2024-11-17 02:45:35

对主观问题的主观回答:

  • 库(以及 head 中的函数定义),全部作为静态资产提供,以便可以缓存。
  • “触发”脚本块中页面底部的回调等。

Subjective answer to a subjective question:

  • Libraries (and function definitions in head), all provided as static assets so they can be cached.
  • 'triggers' to callbacks, etc. at bottom of page in script blocks.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文