【问题标题】:Setting Cache-Control headers on js assets in RAILS 3.1在 RAILS 3.1 中为 js 资产设置 Cache-Control 标头
【发布时间】:2011-09-20 08:24:27
【问题描述】:

我将生产服务器上的标头检查为

    curl --head -H "Accept-Encoding: gzip" http://foo.heroku.com/assets/mobile.js

我得到以下标头,这些标头表明 RAILS 没有设置缓存控制标头。

HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Wed, 22 Jun 2011 12:01:55 GMT
Content-Type: application/javascript
Connection: keep-alive
Content-Md5: efb90436a465f8a73efb467109f745f3
Cache-Control: no-cache
Last-Modified: Wed, 22 Jun 2011 11:46:04 GMT
Etag: "efb90436a465f8a73efb467109f745f3"
X-Ua-Compatible: IE=Edge,chrome=1
X-Runtime: 0.001258
X-Content-Digest: 6493f457e9550773761bb1c2c52ec4cb44a19c19
X-Rack-Cache: stale, valid, store
X-Varnish: 164373614
Age: 0
Via: 1.1 varnish
Content-Encoding: gzip

我想让 heroku 的清漆缓存缓存资产,并且只在 git push 时刷新。关于如何获得这个的任何想法?

布拉德

【问题讨论】:

标签: javascript ruby-on-rails caching heroku ruby-on-rails-3.1


【解决方案1】:

你必须在你的 environment/production.rb 中添加这个:

config.serve_static_assets = true
config.static_cache_control = "public, max-age=172800"

【讨论】:

  • 即使在 3 年后,你的 config.static_cache_control 也让我很开心
  • 做到 7 :-)
【解决方案2】:

Camille 的回答对于 5.1 之前的 Rails 版本是正确的

但是,在 Rails 5.1 中 config.static_cache_control 将被弃用。使用新的可用选项更新的代码应该是:

config.serve_static_assets = true
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age= 172800' }

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2019-01-20
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多