使用 Rails 3 进行资产管理(在 Heroku 上)(Jammit、AssetHat、Rack PageSpeed)

发布于 2024-11-10 06:33:19 字数 674 浏览 3 评论 0原文

我对 Rails 3.0.x(尤其是 Heroku)中管理资产的不同工具的优缺点感兴趣。

已经有一些关于此的问题主题,但同时有一些新工具可用。

我对这些工具特别感兴趣:

Jammit似乎可以完成 AssetHat 可以做的所有事情,而且可用时间也更长。那么 AssetHat 适合用在哪里呢?

Rack PageSpeed 似乎通过直接处理服务器响应来即时完成所有操作。您这样做是否遇到任何性能问题?您会推荐它而不是其他两种解决方案吗?

I am interested in the pros and cons of the different tools for managing assets in Rails 3.0.x (especially on Heroku).

There are already some older questions regarding this topic, but in the meanwhile there are some new tools available.

I am especially interested in these tools:

Jammit seems to can do everything that AssetHat can do and is also longer available. So where does AssetHat fit in?

Rack PageSpeed seems to do everything on the fly by directly working on the server response. Did you experience any performance issues by doing that? Would you recommend it over the other two solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

凌乱心跳 2024-11-17 06:33:19

大家好,我是 AssetHat 的作者。缩小和串联是最容易实现的性能提升;这些功能是 Jammit、AssetHat 和rack-pagespeed 所共有的。 Rails 长期以来一直支持串联(尽管它是在运行时完成的,而不是在部署期间完成的),因此 Rails 3.1 在部署期间同时支持缩小和串联也就不足为奇了。

其余的功能使这些资产管理器变得有趣。例如,如果您想将图像和字体文件直接嵌入到样式表中,Jammit 非常有用。如果您想将所有优化保留在完全独立的层中,rack-pagespeed 也很方便。

将资源内联到 CSS 中非常适合样式表不经常更改的静态页面。但是,如果您的网站正在积极开发中,并且样式表发生了哪怕很小的变化,用户的浏览器就必须重新下载整个内容,包括可能没有改变的内联图像和字体。这取决于您的项目的性质。

如果您的资源太大而无法内联或连接,AssetHat 可以帮助优化 CDN 和并行加载:

  • 它充分利用了 CDN,无论是 Google 的 CDNcdnjs(使用亚马逊的服务器),或您选择的其他 CDN。例如,只需将 <%= include_js :jquery %> 添加到您的布局(以及配置文件中的版本号)即可从 Google 的 CDN 加载 jQuery。如果您处于开发模式并且有 jQuery 的本地副本,则会加载 — 轻松离线开发。
  • AssetHat 可以重写样式表的图像 URL 以使用您的 CDN。这将从您的 config.action_controller.asset_host 设置中读取,并在部署时完成。您原来的 CSS 保持不变。
  • 如果要加载多个 JS 文件,有时并行加载它们比连接它们(即强制它们串行加载)更快。您可以轻松开启LABjs模式<%= include_js 'big-file-1', ..., '大文件-n', :loader => :lab_js %>。如果您本地没有 LABjs 的副本,或者您正在生产中,则 LABjs 会通过 cdnjs 从 Amazon 的服务器加载。

通过使用 Google 或 Amazon 等 CDN,您的用户可以并行加载更多资源(因为有更多主机名),享受更快的速度,有时甚至根本不需要下载资源(例如,如果他们已经加载了 Google 的 jQuery 副本)通过别人的网站)。

我在 Heroku 上使用了 AssetHat,将部署脚本设置为简单地运行 rake asset_hat:minify(以缩小和连接 CSS/JS),将这些更改提交到我的存储库,然后进行实际部署。

如果您还没有看过这些,您可能会对以下内容感兴趣:

如果您需要设置方面的帮助,或者有任何其他问题,请随时在 GitHub (rondevera) 或 Twitter (< a href="https://twitter.com/ronalddevera">@ronalddevera)。

Hey there, I'm the author of AssetHat. Minification and concatenation are among the easiest performance boosts to implement; these features are common to Jammit, AssetHat, and rack-pagespeed. Rails has supported concatenation for a long time now (though it's done at runtime, rather than during deployment), and it's no surprise that Rails 3.1 supports both minification and concatenation during deployment.

The remaining features are what make each of these asset managers interesting. For example, Jammit is useful if you want to embed images and font files directly into your stylesheets. rack-pagespeed is also handy if you want to keep all your optimizations in a completely separate layer.

Inlining assets into CSS is great for static pages where stylesheets change infrequently. However, if your site is under active development, and the stylesheet changes even a tiny bit, the user's browser has to re-download the whole thing—including inline images and fonts that probably didn't change. It depends on the nature of your project.

If your assets are too big to inline or concatenate, AssetHat helps optimize for CDNs and parallel loading:

  • It takes great advantage of CDNs, whether it's Google's CDN, cdnjs (which uses Amazon's servers), or another CDN of your choosing. For example, just add <%= include_js :jquery %> to your layout (and a version number in a config file) to load jQuery from Google's CDN. If you're in dev mode and have a local copy of jQuery, that loads instead—easy offline dev.
  • AssetHat can rewrite stylesheets' image URLs to use your CDN instead. This reads from your config.action_controller.asset_host setting, and is done at deploy time. Your original CSS is left untouched.
  • If you have several JS files to load, it's sometimes faster to load them in parallel than to concatenate them (i.e., force them to load serially). You can switch on LABjs mode easily: <%= include_js 'big-file-1', ..., 'big-file-n', :loader => :lab_js %>. If you don't have a copy of LABjs locally, or if you're in production, LABjs loads from Amazon's servers via cdnjs.

By using CDNs like Google's or Amazon's, your users can load more assets in parallel (because there are more hostnames), enjoy greater speed, and sometimes, not even need to download assets at all (e.g., if they already loaded Google's copy of jQuery via someone else's website).

I've used AssetHat on Heroku by setting my deploy script to simply run rake asset_hat:minify (to minify and concatenate CSS/JS), commit those changes to my repository, then do the actual deployment.

In case you haven't seen these already, you might be interested in:

If you need help setting it up, or have any other questions, feel free to message me on GitHub (rondevera) or Twitter (@ronalddevera).

墨落画卷 2024-11-17 06:33:19

据我所知,Jammit 无法在 Heroku 上开箱即用。一种选择似乎是使用 Heroku Jammit 插件来管理您的资产 - https://github.com/chebyte /heroku-jammit

或者,可以将 Jammit 配置为输出到 /tmp: http ://geekninja.blogspot.com/2011/04/making-jammit-jam-with-heroku.html

Rails 3.1 将包含 Sprockets 来处理资源包装,我认为值得考虑。

Jammit won't work out of the box on Heroku as far as I know. One option seems to be to use the Heroku Jammit plugin to manage your assets - https://github.com/chebyte/heroku-jammit.

Alternatively, Jammit can be configured to output to /tmp: http://geekninja.blogspot.com/2011/04/making-jammit-jam-with-heroku.html

Rails 3.1 will include Sprockets to handle asset packaging, I think that's worth considering.

绾颜 2024-11-17 06:33:19

我目前正在 Heroku 上使用 jammit 以及 amazon s3,它的工作方式就像一个魅力:)

我不能对其他工具说太多,因为我还没有使用过它们。

最后你选了哪一个呢?

费尔南多.

I am currently using jammit on heroku, together with amazon s3, and it works like a charm :)

I can't say much about the others tools because I have not used them.

Which one did you pick, in the end?

Fernando.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文