【问题标题】:Rails asset caching -- cannot get max-age to setRails 资产缓存——无法设置 max-age
【发布时间】:2013-04-12 08:49:43
【问题描述】:

我无法让 rails 为我的任何 CSS 或 JS 资产设置 max-age 值。我们正在运行 rails 3.1,但我们升级了,所以我很可能错过了一些明显的配置。在这一点上,我已经将我能找到的大部分配置从 true 翻转为 false 并再次返回,但没有运气。

这是我当前的环境/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 东西,在我的待办事项列表中)

【问题讨论】:

  • 这应该放在待办事项清单上吗?它正在替换一两行代码。
  • 如果您在 URL 中提供带有哈希摘要的资产,您是否考虑过只在 Apache/Nginx 级别进行缓存?这样你就可以设置遥远的缓存过期时间。还是您只关心开发中的缓存?
  • @StuartM 我们在 heroku 上运行,并在 prod 中看到与我在开发中相同的行为。我的理论是,如果我为一个修复它,我为两个修复它。
  • @StuartM 另外,我并不真正关心摘要。从文档来看,这似乎被认为是“好事”。我的理解是,使用摘要,您可以将资产设置为将来无限期过期,因为如果它们每次更改,您都会加载不同的摘要。问题是,它们没有缓存。
  • 你试过config.serve_static_assets = true吗?请参阅下面的答案

标签: ruby-on-rails caching heroku asset-pipeline


【解决方案1】:

来自Rails Configuration Guide(强调我的):

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

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

更新Heroku Dev Center 也建议这样做:

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

【讨论】:

  • 所以,我设置了serve_static_assets=true。仍然看到无缓存响应。
  • 您在测试 cURL 命令时看到的是否相同?例如,curl -I http://your-host/path/to/asset,响应中有哪些标头?
  • 我的立场是正确的。在尝试缩小范围时,此解决方案正在工作。但是,chrome 和 firefox 存储的 max-age 值仍然为 0。有没有办法让浏览器在每次加载时不检查每个资产是否都是新鲜的?所以我看到从服务器返回的响应是 max-age=3600,但是 chrome 和 firefox 会在每次页面加载时获取。
  • 您是在浏览器中点击“刷新”还是只是正常浏览网站上的不同页面?在过去,我已经看到明确点击“刷新”会导致原本缓存的资产重新加载。并且重新:强制浏览器遵守某些约束,您是否尝试过任何其他 Cache-Control 标头指令?见mnot.net/cache_docs/#CACHE-CONTROL
  • 嗯,很高兴这是显而易见的。我的测试有缺陷。我最初在导航时看到了这个问题,但一直在通过点击刷新来测试各种解决方案。
猜你喜欢
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 2018-05-08
  • 1970-01-01
相关资源
最近更新 更多