Rails sending gzip or uncompressed file - ruby-on-rails

As per rails guides, compression is on atuomatically for assets unless specifically defined differently config.assets.gzip. These, and the uncompressed files are being generated on the application's shared/assets folder.
Context: nginx server version 1.14 + Phusion Passegner 6.0 running
How can I tell whether the call to the server is invoking the .gz vs uncompressed asset (assuming the client browser is not using the cached version)?

In the client request, there will be the Accept-Encoding header
It may contain smth like gzip, deflate, br
In this case, gzipped resource will be sent if there's one available. In the response headers there will be Content-Encoding: gzip then

Related

Using cowboy_static to serve gzip compressed files

We are using cowboy version 1.0.3 and we would like to serve compressed gzip versions of our files. We set the flag as noted in the documentation:
{compress, true}
But we notice that files served by the cowboy_static module are not being compressed, do we need to set up our own handler for this or are we missing something ?

net::ERR_CONTENT_DECODING_FAILED when using Rack::Deflater on jruby

net::ERR_CONTENT_DECODING_FAILED
I am receiving this in chrome when Rack::Deflater is enabled, when using on jruby.
After having some reading it looks like this is some jruby issue with gzipping.
Is there any alternative for serving gzipped assets? And also dynamically gzipping responses?
I am hosting the application on heroku. Rails version is 3.2.X
I think this is related with this issue:
https://github.com/jruby/jruby/issues/1371#ref-issue-15218894
This error comes by browser
The response should contain the header telling the client which compression scheme is being used: Content-Encoding: gzip and
The request should contain the correct header: "Accept-Encoding: gzip, deflate"

configuring nginx to serve static json files

Switching to nginx for a site, one issue I'm having is serving up static json files.
I added to the mime types:
application/zip zip;
...
application/json json;
...
and restarted but it tried serving it up as a download (ie http://domain.com/json-tmp/locations.json). What else would I need to configure?
thx
I had to add
application/json json;
in /etc/nginx/mime.types and restart nginx to make it work.
I know this post is quite old but adding application/json mime type to nginx configuration file plus restarting the server should work.
When you request the json file try to debug the response header and check if the Content-Type header was successfully changed to application/json.
Try serving it as javascript ...
application/javascript json;
Or as plain text ...
text/plain txt json;
I'll try the javascript first.

how to compress javascript files served from my rails app running on apache/passenger?

I'm using Apache 2.2, Passenger 3.0.2, Rails 3.
I've managed to compress the css file by modifying the deflate.conf file. However, when I run the firefox addon yslow, it still says the javascript files are uncompressed.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript text/javascript application/x-javascript
</IfModule>
Above, I'm using all three variants: "application/javascript text/javascript application/x-javascript" but no luck.
Here's the message from yslow:
Grade D on Compress components with
gzip
There are 3 plain text components that
should be sent compressed
* http://myhost.dyndns.org:8080/javascripts/jquery-1.4.2.min.js?...
* http://myhost.dyndns.org:8080/javascripts/jquery.validate.min.js?...
* http://myhost.dyndns.org:8080/javascripts/rails.js?...
Jammit is an industrial strength asset packaging library for Rails, providing both the CSS and JavaScript concatenation and compression that you'd expect, as well as YUI Compressor and Closure Compiler compatibility, ahead-of-time gzipping, built-in JavaScript template support, and optional Data-URI / MHTML image and font embedding.
http://documentcloud.github.com/jammit/
Assuming that you are using capistrano, add a deployment task that compresses our javascript files in-place on the production server.
Check this:
http://blog.jcoglan.com/2007/05/26/make-capistrano-compress-your-javascript-and-css-automatically/

Compressing a JSON response from a Rails app

We have an app that queries for locations for a customer. We're getting to the point where some customers could have upwards of 10,000 locations. The JSON response for this can get quite large, over 1mb sometimes.
I'm wondering first off the best way to compress this. We have apache in front of a Rails app running in trinidad with JRuby. Can I just set mod_deflate to always compress any responses that are application/json? How might I go about doing this?
Next, what is the browser support for gzip'd json? When I gzip a sample response of 200k it goes down to 30k. That's a significant savings. We're really like to be able to minimize the size of that response without having to minimize the number of locations returned.
In general, for newer versions of Rails, you can do it by adding
use Rack::Deflater
before the "run" line in the config.ru file. This will work perfectly with browsers/clients that support gzip. We use it in production on major websites.
Note for JRuby users: This assumes that your Rails app is launched through Rack, which it often isn't for JRuby. You need a recent version of JRuby-Rack and configure it in Warbler to run in 'rack' mode instead of 'rails' mode.
If the browser supports gzip'd/deflated data, then JSON will go through it just fine. AJAX data is just a regular HTTP request that was done on behalf of a script, rather than a human. At the HTTP level, there's absolutely zero difference between transferring some HTML or a JSON string - it's just data.
For Googlers... [I'm running Apache 2.2.16 and don't care about IE6]
JSON Responses with Content-Encoding = gzip didn't happen until I edit mod_deflate.conf to include this:
AddOutputFilterByType DEFLATE application/json
You can check server response headers with Firefox / Firebug / Net tab
First, make sure you have apache's mod_deflate installed by running this command.
a2enmod deflate
If this command installed it, restart apache. If not, you're good for now.
service apache2 restart
In apache2.conf usually located at /etc/apache2 append this line to the end of the file. This will include a file that we'll create in a little.
Include mod_deflate.conf
Next, we'll edit the mod_deflate.conf with our options:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|bz2|sit|rar)$ \
no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
#Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
The first block of code disables gzipping exes, gzs, pdfs.. etc
The second block of code skips deflation from browsers that don't support it.
Finally, restart apache again
service apache2 restart
The settings were copied from the link below:
http://www.howtoforge.com/apache2_mod_deflate

Resources