Rails资产缓存 - 无法设置max-age

Joh*_*gan 7 caching ruby-on-rails heroku asset-pipeline

我无法获取rails来为我的任何CSS或JS资产设置max-age值.我们正在运行rails 3.1,但我们升级了所以我很可能错过了一些明显的配置.在这一点上,我已经把我能找到的大部分配置从真实转为假,然后又没有运气.

这是我目前的环境/ 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
Run Code Online (Sandbox Code Playgroud)

这是我的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
Run Code Online (Sandbox Code Playgroud)

我认为,我正确地包括了资产.这是一个例子:<%= 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
Run Code Online (Sandbox Code Playgroud)

我应该能够避免在每次页面加载时获得所有这些.(是的,我不应该在我的待办事项列表中本地托管大多数jquery内容)

Stu*_*t M 6

Rails配置指南(强调我的):

config.serve_static_assets配置Rails本身以提供静态资产.默认为true,但在生产环境中关闭,因为用于运行应用程序的服务器软件(例如Nginx或Apache)应该为静态资产提供服务.与默认设置不同,在运行时(绝对不推荐!)或使用WEBrick在生产模式下测试应用程序时将此设置为true.否则,您将无法使用页面缓存,并且对公共目录下定期存在的文件的请求将无论如何都会打到您的Rails应用程序.

config.serve_static_assets = true如果你还没有,我会尝试设置.

更新:Heroku开发中心还建议:

要允许您的应用程序正确地提供服务,使无效和刷新静态资产,必须在config/environments/production.rb中更新几个配置设置.允许Rails使用该serve_static_assets设置提供资源.

  • 我纠正了.在尝试缩小范围的同时,这个解决方案正在起作用.但是,chrome和firefox存储的max-age值仍然是0.是否没有办法让浏览器不检查每个资源上的每个资产是否都是新鲜的?所以我看到响应从服务器返回max-age = 3600,但chrome和firefox在每个页面加载时都会得到get. (2认同)
  • 嗯,很高兴这是显而易见的事情.我的测试有缺陷.我在导航时看到了最初的问题,但是通过点击刷新来测试各种解决方案. (2认同)