Where to Insert Rack::Deflater in the Rack

Rack::Deflater and Rack::URLMap

It looks like Rack::BodyProxy does provide an #each method as a workaround of issue rack/rack#434. This method returns an enumerator over the elements of the private @body ivar. Adding an .each into the call chain resolves this problem for Rack::BodyProxy objects, and it's a no-op for more ordinary array-based bodies, so for now this gets me where I need to be. However, I would still like to understand why I'm getting these objects and if there's a better way to deal with them.

Here's the modified and working solution:

use Rack::Deflater, :if => lambda { |*, body| 
body.each.map(&:bytesize).reduce(0, :+) > 512
}

UPDATE: Hrrm, sometimes body is a Rack::BodyProxy object, and body.body is a Rack::Response object. This is getting a little out of hand...! Here's the solution I'm using now:

use Rack::Deflater, :if => lambda { |*, body|
body.map(&:bytesize).reduce(0, :+) > 512 \
if body.respond_to?(:map) \
or body.respond_to?(:each) and (body = body.each).respond_to?(:map)
}

Far from elegant...

Rails: Conditionally enable Rack::Deflater?

As mentioned by @apneadiving in his comment. From condition here: https://github.com/rack/rack/blob/master/lib/rack/deflater.rb#L31, you can do something like this:

config.middleware.use Rack::Deflater, :if => lambda { |env, status, headers, body| env["PATH_INFO"] == "/v2/" } # or Regular expression: env["PATH_INFO"].match(/\/v2\//) matches -> /v2/

Rails 4 Rack::Deflater is not working. Page speed says not gzipped

I had exactly the similar problem.

When I started the suggestion here to inserting config.middleware.use Rack::Deflater in config/application.rb content was not compressed. Solutions suggested like Using config.middleware.insert_before or updating config.ru also did not work.

Then I found out that it was not because of any problem with approaches listed above, but it was because, I was using apache/httpd and I had to explicitly enable rack_deflate module as described here, and compression started working.

Rack::Deflater and gzip in rails app

This is the correct behavior. Check your HTTP headers to see if the gzip compression actually works:

curl -I -H "Accept-Encoding: gzip,deflate" <url>

This should return something like Content-Encoding: gzip if gzip compression is enabled.

You can also see the compressed output by using a lowercase -i. Alternatively to curl, you can also use the WebKit developer tools (or something similiar) to check this.

Unable to check response size with Rack Deflater

Yes, you are right. It's version issue. You paste code from the very last version (2.0.3/2.0.4), but according to your trace you have 1.5.5 in your local setup.

Here you can see that Rack::Deflater doesn't accept any additional options in 1.5.5 -- https://github.com/rack/rack/blob/1.5.5/lib/rack/deflater.rb#L20

Firstly options were added in the very next release 1.6.0.beta. You can see that here -- https://github.com/rack/rack/blob/1.6.0.beta/lib/rack/deflater.rb#L28

Updating your rack should solve your issue for you

Rack::Deflater works if only added to config.ru

It's a bug of jRuby 1.7

https://github.com/rack/rack/issues/571

And it was fixed in 1.7.23, so I just had to upgrade.

Rack::Deflater apparently gzip page but it appears as gibberish in browser

This was a bit silly. Apparently rails 3.1 automatically uses Rack::Deflater so I was basically gzipping twice...

I discovered it by executing rake middleware.

I hope it helps anyway.

ETag changes when Rack::Deflater is enabled

So I have fixed my original issue but still not quite getting the desired results. Turns out Rack::Deflater needs to be before Rack::ETag in the middleware stack. Still not sure why this would cause the ETag to change every request but sure enough if I change config.middleware.use "Rack::Deflater" to
config.middleware.insert_before "Rack::ETag", "Rack::Deflater"
then the ETag becomes consistent across requests. I'm still not getting a 304 but I think thats because of incorrect cache-control headers and unrelated to the original problem. Hopefully this helps someone in the future.



Related Topics



Leave a reply



Submit