Rails编译带有和不带有md5哈希的资产,为什么呢? [英] Rails compiles assets both with and without md5 hash, why?
问题描述
我对RoR相对陌生,我很好奇为什么Rails会同时编译带有和不带有md5哈希的资产进行生产?
I'm relatively new to RoR and I'm curious about why Rails compiles assets both with and without md5 hash for production?
我先运行bundle exec rake assets:clean
,然后再运行bundle exec rake assets:precompile
I run bundle exec rake assets:clean
then bundle exec rake assets:precompile
我的production.rb文件:
My production.rb file:
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
我的应用程序使用名称中带有哈希值的文件,这就是我的情况:)
My application works with files with hashes in their names and it's the way it should be in my case :)
所以我在这里有两个问题:
So I have two questions here:
1)为什么在编译时会发生这种情况?
1) Why is it happening when compiled?
Rails编译带有或不带有md5哈希的资产进行生产
Rails compiles assets both with and without md5 hash for production
2)这些文件(无散列)是做什么用的?
2) What are these files (without hashes) for?
也许我什么都没得到,所以请有人解释.
Maybe I don't get something, so please could someone explain.
推荐答案
这样做的原因是,您可以在不知道MD5指纹的情况下访问文件(例如,在非Rails应用程序中,或者在并非由Rails堆栈编译或运行的Rails应用程序(例如,状态为500/502的错误页面),在这种情况下,您必须先编译资产,然后在每次更新时更改静态HTML文件中的css/js链接代码(从而导致MD5哈希值发生变化).
The reason it does it is so that you can access the files without knowing the MD5 fingerprint (for example in a non-rails application, or a file within the rails app which isn't compiled or run by the rails stack (e.g. a 500/502 status error page). In this case you would have to compile the assets then change the css/js links in the static HTML files each time you updated the code (thus causing a change in the MD5 hash).
因此,rails会为每个资产文件生成2个副本,一个副本在文件名中带有指纹,另一个副本不带(例如application-731bc240b0e8dbe7f2e6783811d2151a.css和application.css).显然最好使用指纹识别版本(请参见'轨道资产管道指南 ).但是未消化的版本作为后备版本.
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.
作为对此事的最终思考,我将阅读以下对Rails git repo的拉取请求: https://github.com/rails/rails/pull/5379 ,他们在这里讨论未消化文件名的优缺点,以及能够关闭它们的编译的可能性
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
这篇关于Rails编译带有和不带有md5哈希的资产,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!