Rails使用和不使用md5哈希编译资产,为什么?

时间:2012-04-06 15:52:57

标签: ruby-on-rails md5 assets digest

我对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)这些文件(没有哈希)是什么?

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

1 个答案:

答案 0 :(得分:15)

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

因此,rails会生成每个资产文件的2个副本,一个在文件名中有指纹,另一个没有(例如application-731bc240b0e8dbe7f2e6783811d2151a.css和application.css)。指纹版本显然是首选(请参阅what is fingerprinting and why should I care中的“rails asset pipeline guide”)。但是非消化的版本就是后备版本。

作为对此问题的最后想法,我会读取以下对pull git repo:https://github.com/rails/rails/pull/5379的请求,他们正在讨论非消化文件名的优缺点,以及可能性能够关闭它们的编译。

HTH