6

I have a Rails 3.2 app that I'm deploying on the Heroku Cedar stack. This means that the app itself is responsible for serving its static assets. I'd like these assets to be gzipped, so I've inserted Rack::Deflater in my middleware stack in production.rb:

middleware.insert_after('Rack::Cache', Rack::Deflater)

...and curl tells me that this works as advertised.

However, since Heroku is going to all the effort of running rake assets:precompile, producing a bunch of pre-gzipped assets, I'd quite like to use those (rather than letting Rack::Deflater do all the work again). I've seen recipes for serving these up with nginx (no use on Heroku), and with CDNs (not wanting to use a CDN just yet), but I haven't seen anything that can just run standalone. I've hacked together a rack middleware to do this, but I was wondering if this is the best way to go about it?

John Bachir
  • 22,495
  • 29
  • 154
  • 227
Guy Bolton King
  • 973
  • 1
  • 8
  • 17

1 Answers1

2

Since deflater is after rack cache, then deflator will only have to do the work once, and after that the compressed assets will be served from rack cache (assuming the cache is big enough so that they don't get bumped out occasionally).

That said, your middleware looks pretty cool, and you should make make it a gem and blog about it, maybe it will be what people start to use :-)

John Bachir
  • 22,495
  • 29
  • 154
  • 227
  • Thanks: I'm not sure the gem would be super-useful, but I'll try and find time to do that. As it is, I suspect the deafening silence means that this is generally viewed as a non-problem! – Guy Bolton King Apr 02 '12 at 16:24
  • Also Cedar is pretty new and folks are still optimizing for it. Heroku only recently published their own comprehensive docs about Rack::Cache https://devcenter.heroku.com/articles/rack-cache-memcached-static-assets-rails31 It's a very non-ideal system for static assets, one should really use a CDN for that. Even for non-static assets it's way less performant than varnish on bamboo. – John Bachir Apr 02 '12 at 17:59
  • Agreed: when I wrote the question I was in the process of figuring out the best way of going about things, and I've gone for [using CloudFlare and bypassing Rack::Cache for static assets](http://stackoverflow.com/questions/6962896/how-do-i-prevent-rails-3-1-from-caching-static-assets-to-rails-cache) for the time being. – Guy Bolton King Apr 04 '12 at 09:48