2012-04-06 59 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