How to enable gzip compression for static Rack sites on Heroku Cedar?

Following Creating static sites in Ruby with Rack , I get a static site on Heroku with config.ru that looks like this:

 use Rack::Static, :urls => ["/images", "/js", "/css"], :root => "public" run lambda { |env| [ 200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY) ] } 

When I run YSlow based on the result, it says that none of the files were gzipped. What should I do to compress both assets and public/index.html ?

+4
source share
1 answer

From my previous experience with Sprockets, Sinatra and Rack::Deflater , I was sure that I was just another use Rack::Deflater from what I wanted.

I changed config.ru to this:

 use Rack::Static, :urls => ["/images", "/js", "/css"], :root => "public" use Rack::Deflater run lambda # ...same as in the question 

and I was able to verify that the responses were sent by gzipped:

 $ curl -H 'Accept-Encoding: gzip' http://localhost:9292 | file - /dev/stdin: gzip compressed data 

but not for static assets in /css , /js or /images :

 $ curl -H 'Accept-Encoding: gzip' http://localhost:9292/css/bootstrap.min.css | file - /dev/stdin: ASCII English text, with very long lines 

And when I realized that this is the standard intermediate stacks - Rack :: Static intercepts the call of static files and thus skips the next stack! This is why it worked for public/index.html , but not for assets.

The following config.ru (note that use Rack::Deflater now preceded by use Rack::Static ):

 use Rack::Deflater use Rack::Static, :urls => ["/images", "/js", "/css"], :root => "public" run lambda { |env| [ 200, { 'Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=86400' }, File.open('public/index.html', File::RDONLY) ] } 

Verified with

 $ curl -H 'Accept-Encoding: gzip' http://localhost:9292/css/bootstrap.min.css | file - /dev/stdin: gzip compressed data, from Unix 
+8
source

All Articles