How to get Gzip and Expires Header on a Rails 3.1.1 app on Heroku Cedar?

6.4k views Asked by At

I'm running a Rails 3.1.1 application on Heroku Cedar. By default this stack doesn't Gzip and set Expires Headers on assets. There is some doc about that, but it's not very clear : http://devcenter.heroku.com/articles/http-routing

Can somebody give me the piece of code to activate that ?

Thank you very much

3

There are 3 answers

4
DuoSRX On BEST ANSWER

Cedar doesn't use Nginx, so you have to gzip assets yourself with Rack::Deflater, like so :

# config.ru
require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
run YourApp::Application

Also you can set headers for static files directly in your app :

# config/environments/production.rb
config.static_cache_control = "public, max-age=3600"

Finally you're probably better off setting up Rack::Cache to replace Varnish caching. See this blog post for more infos.

0
Peter Ehrlich On

It is important that the middleware be included early, before ActionDispatch::Static

#production.rb
config.middleware.insert_before ActionDispatch::Static, Rack::Deflater

> rake middleware
use Rack::Deflater
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007f8e18455e90>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Remotipart::Middleware
use ActionDispatch::Head
use Rack::ConditionalGet
use Rack::ETag
use ActionDispatch::BestStandardsSupport
use Warden::Manager
use Rack::Mongoid::Middleware::IdentityMap
use Rack::Pjax
run MyApp::Application.routes
4
Roman On

Shameless plug - I created a gem which enables compression, but avoids compressing images.

https://github.com/romanbsd/heroku-deflater