最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting Cache-Control headers on js assets in RAILS 3.1 - Stack Overflow

programmeradmin0浏览0评论

I check the headers on my production server as

    curl --head -H "Accept-Encoding: gzip" .js

and I get back the following headers which suggest RAILS is not setting the cachecontrol headers.

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

I'd like to get heroku's varnish cache caching the assets and only refreshing on git push. Any ideas on how to get this?

Brad

I check the headers on my production server as

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

and I get back the following headers which suggest RAILS is not setting the cachecontrol headers.

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

I'd like to get heroku's varnish cache caching the assets and only refreshing on git push. Any ideas on how to get this?

Brad

Share Improve this question asked Jun 22, 2011 at 12:22 bradgonesurfingbradgonesurfing 32.2k20 gold badges122 silver badges231 bronze badges 1
  • This article should shed some light on the issue: jimmycuadra./posts/… – Jey Balachandran Commented Feb 11, 2012 at 23:21
Add a ment  | 

2 Answers 2

Reset to default 9

You have to add this in your environment/production.rb:

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

Camille's answer is right for Rails versions before 5.1

However, in Rails 5.1 config.static_cache_control will be deprecated. The code updated with the new available option should be:

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

评论列表(0)

  1. 暂无评论