2012-04-06 61 views
14

我对RoR相对比较陌生,我很好奇Rails为什么编译资产既有和没有md5散列用于生产?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)这些是什么文件(没有哈希)呢?

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

回答

14

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

所以不是轨道产生的每个资产文件的2个拷贝,一个具有在文件名中的指纹,其他没有(例如应用程序731bc240b0e8dbe7f2e6783811d2151a.css,和application.css)。加入了指纹的版本显然是优选的(参见“what is fingerprinting and why should I care '在rails asset pipeline guide)。但非消化ed版本作为后备。

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

HTH

+0

你好,克里斯感谢你的回复和解释,我认为我有一些错误配置导致了这种行为。如果这种方式应该是这样的话,那就可以了。再次感谢。 – 2012-04-06 17:04:17

+1

在事情的另一面,我的Rails安装与他的配置完全相同,但它只是编译带有指纹的资产,并没有编译没有指纹的版本。由于Chris Bailey列出的原因,这非常烦人。任何想法如何我可以解决这个问题? – NudeCanalTroll 2012-04-18 00:52:10

+2

@NudeCanalTroll:你没有运行'rake assets:precompile:nondigest'。 – jpatokal 2012-11-20 04:44:51