15

我对 RoR 比较陌生,我很好奇为什么 Rails 会编译带有和不带有 md5 哈希的资产以用于生产?

bundle exec rake assets:clean然后我跑bundle exec rake assets:precompile

我的 production.rb 文件:

MyApp::Application.configure do

  # Code is not reloaded between requests

  config.cache_classes = true

  # Full error reports are disabled and caching is turned on

  config.consider_all_requests_local       = false

  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)

  config.serve_static_assets = false

  # Compress JavaScripts and CSS

  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed

  config.assets.compile = false

  # Generate digests for assets URLs

  config.assets.digest = true 

  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  config.assets.precompile += %w(tos.js, tos.css)

  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

end

我的应用程序使用名称中带有哈希的文件,这在我的情况下应该是这样:)

所以我在这里有两个问题:

1)为什么编译时会发生?

Rails 编译带有和不带有 md5 哈希的资产以用于生产

2)这些文件(没有哈希)是做什么用的?

也许我没有得到什么,所以请有人解释一下。

4

1 回答 1

15

这样做的原因是您可以在不知道 MD5 指纹的情况下访问文件(例如,在非 Rails 应用程序中,或 Rails 应用程序中未由 Rails 堆栈编译或运行的文件(例如 500 /502 状态错误页面)。在这种情况下,您必须编译资产,然后在每次更新代码时更改静态 HTML 文件中的 css/js 链接(从而导致 MD5 哈希值发生变化)。

So instead rails produces 2 copies of each asset file, one with the fingerprint in the filename, the other without (e.g. application-731bc240b0e8dbe7f2e6783811d2151a.css, and application.css). The fingerprinted version is obviously preferred (see 'what is fingerprinting and why should I care' in the rails asset pipeline guide). But the non-digested version is there as a fallback.

As a final thought on the matter I'd take a read of the following pull request to the rails git repo: https://github.com/rails/rails/pull/5379 where they are discussing the pros and cons of the non-digested filenames, and the possibility of being able to turn off compilation of them.

HTH

于 2012-04-06T16:40:48.797 回答