2013-04-19 74 views
7

我無法讓rails爲我的任何CSS或JS資產設置最大年齡值。我們正在運行rails 3.1,但是我們升級了,所以我很可能缺少一些明顯的配置。在這一點上,我已經將大部分配置從真到錯翻轉過來,沒有運氣。Rails資產緩存 - 無法獲取最大年齡設置

這是我目前的環境/ production.rb

Ventura::Application.configure do 
    # Settings specified here will take precedence over those in config/application.rb 

    # 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 

    # Configure static asset server for tests with Cache-Control for performance 
    config.serve_static_assets = false 
    config.static_cache_control = "public, max-age=3600" 

    # Compress JavaScripts and CSS 
    config.assets.compress = true 

    # Don't fallback to assets pipeline if a precompiled asset is missed 
    config.assets.compile = true 

    # Generate digests for assets URLs 
    config.assets.digest = true 

    # Defaults to Rails.root.join("public/assets") 
    # config.assets.manifest = YOUR_PATH 

    # Specifies the header that your server uses for sending files                                                                           
    # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache 
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx 

    # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 
    # config.force_ssl = true 

    # See everything in the log (default is :info) 
    # config.log_level = :debug 

    # Use a different logger for distributed setups 
    # config.logger = SyslogLogger.new 

    # Use a different cache store in production 
    # config.cache_store = :mem_cache_store 

    # Enable serving of images, stylesheets, and JavaScripts from an asset server 
    # config.action_controller.asset_host = "http://assets.example.com" 

    # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) 
    # config.assets.precompile += %w(search.js) 

    # Disable delivery errors, bad email addresses will be ignored 
    # config.action_mailer.raise_delivery_errors = false 

    # Default mailer URL 
    config.action_mailer.default_url_options = { :host => 'http://ventura-production.herokuapp.com/'} 

    # Enable threaded mode 
    # config.threadsafe! 

    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 
    # the I18n.default_locale when a translation can not be found) 
    config.i18n.fallbacks = true 

    # Send deprecation notices to registered listeners 
    config.active_support.deprecation = :notify 
end 

這是我的application.rb中

# Enable the asset pipeline 
config.assets.enabled = true 

# Version of your assets, change this if you want to expire all your assets 
config.assets.version = '1.0' 

# Don't load resources when precompiling 
config.assets.initialize_on_precompile = false 

我正確,包括資產,我想。這裏有一個例子:<%= javascript_include_tag "client/jquery", "client/jquery-ui-1_8_18", "client/bootstrap_min", "client/jquery-select-menu", "client/jquery_tablesorter", "client/jquery_tablesorter_pager", "client/application" %>

而且在任何給定網頁的請求的部分跟蹤:

cache: [GET /assets/client/jquery-ui-1_8_18-d903da4c219079ca31f0ea1b23f89afc.css] fresh 
cache: [GET /assets/client/bootstrap-2186f501bbd967564f2793c1c30aebc8.css] fresh 
cache: [GET /assets/client/rg-5f04e577cfffd5dbcb8a1735ad211bd9.css] fresh 
cache: [GET /assets/client/custom_charts-63eaad73c206c7ce6616c7f718be783f.css] fresh 
cache: [GET /assets/client/bootstrap_min-afbee53fdd364c866cbd15abd6473012.js] fresh 
cache: [GET /assets/client/jquery-select-menu-f2a3776430c5b4ead15173d0247f3f11.js] fresh 
cache: [GET /assets/client/jquery_tablesorter-7fc613e34c891c852e2932f59bf91368.js] fresh 
cache: [GET /assets/client/jquery_tablesorter_pager-141a886e7f35eb9f662b865516b23eca.js] fresh 
cache: [GET /assets/client/jquery-689ca6a49fcbd1c3777b13d1abcc1316.js] fresh 
cache: [GET /assets/client/application-6a61f272dd6a1ff7b5587435e67cd1bf.js] fresh 

我應該能夠避免所有這些得到每個頁面加載。 (是的,我不應該本地託管jQuery的大部分東西,我的待辦事項清單上)

+1

應該甚至在待辦事項清單上?它取代了一行或兩行代碼。 –

+1

如果您在URL中提供散列摘要的資產,您是否考慮過僅在Apache/Nginx級別進行緩存?這樣你可以設置遠期緩存到期。或者你只關心緩存開發? –

+0

@StuartM我們在heroku上運行,並且在開發中看到與prod相同的行爲。我的理論是,如果我解決這個問題,我就會解決這兩個問題。 –

回答

6

Rails Configuration Guide(重點煤礦):

config.serve_static_assets配置的Rails本身提供靜態資產。默認爲true,但在生產環境中關閉,因爲用於運行應用程序的服務器軟件(例如Nginx或Apache)應該替代靜態資產。與默認設置不同,在運行時將其設置爲true(絕對不推薦!)或使用WEBrick在生產模式下測試您的應用程序。 否則,你將無法使用頁面緩存,並且定期在公共目錄下存在的文件的請求將無論如何打到你的Rails應用程序。

我會嘗試設置config.serve_static_assets = true如果你還沒有。

更新:該Heroku Dev Center還建議如下:

爲了讓您的應用程序正常投放,廢止和刷新靜態資產數配置設置必須配置/環境/ production.rb進行更新。允許Rails通過serve_static_assets設置來投放資產。

+1

因此,我設置了'serve_static_assets = true'。仍然看到無緩存響應。 –

+0

測試cURL命令時看到了相同的結果嗎?例如,'curl -I http:// your-host/path/to/asset',響應中有什麼標題? –

+2

我站好了。在試圖縮小這個範圍的時候,這個解決方案正在工作。儘管如此,Chrome和Firefox所存儲的最大年齡值仍然爲0.是否沒有辦法讓瀏覽器不檢查每個資產是否每次都是新鮮的?所以我看到從服務器返回的響應max-age = 3600,但chrome和firefox在每次頁面加載時都會得到。 –

相關問題