Search code examples
ruby-on-railsherokuruby-on-rails-3.2rackhttp-caching

Heroku production setting Cache-Control differently from local production


I have an app running on heroku at http://chesseng.herokuapp.com/ when I visit the page with chrome and its caching disabled I get a response header for application-fingerprint.css that is something like

Cache-Control:private
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/css
Date:Wed, 17 Oct 2012 00:17:19 GMT
Last-Modified:Tue, 16 Oct 2012 03:13:38 GMT
Status:200 OK
transfer-encoding:chunked
Vary:Accept-Encoding
X-Rack-Cache:miss

However if I start up a local instance with rails s -e production and visit it the response header for application-fingerprint.css is something like

Age:5119
Cache-Control:public, max-age=31536000
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:95828
Content-Type:application/javascript
Date:Tue, 16 Oct 2012 23:01:27 GMT
Etag:"0bf9e9837d421c2e28be1ef4f0794a48"
Last-Modified:Tue, 16 Oct 2012 01:07:17 GMT
Server:WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
Vary:Accept-Encoding
X-Content-Digest:add442e2036c1e6e9f4860dcc44496582a5c91b1
X-Rack-Cache:fresh
X-Request-Id:b89de17e397ac7b60acfe500e8d15df9
X-Runtime:0.001632
X-Ua-Compatible:IE=Edge,chrome=1

Why are the caching related fields like Cache-Control, Etag so different compared to heroku? Presumably I want heroku to return Cache-Control:public, max-age=31536000. But I need to first understand why Cache-Control:public, max-age=31536000 is being set in my local production mode. Its puzzling because if I set config.static_cache_control = "public, max-age=3600" in config/environments/production.rb and start up a local server in production it still gives back max-age=31536000 and seems to ignore max-age=3600


Solution

  • From the X-Rack-Cache headers it looks like you’re using the rack-cache middleware which would affect the caching headers. Since locally your resource is in the cache (X-Rack-Cache:fresh), but on Heroku it isn’t (X-Rack-Cache:miss) that would explain the difference.

    Try removing rack-cache from your middleware stack and see if the differences go away.