# gzip response to AJAX requests

**URL:** <https://discuss.rubyonrails.org/t/gzip-response-to-ajax-requests/17072>\
**Category:** rubyonrails-talk\
**Created:** [August 16, 2007, 1:52pm UTC](https://discuss.rubyonrails.org/t/gzip-response-to-ajax-requests/17072 "2007-08-16T13:52:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephen\_O\_D](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@stephen\_O\_D](https://discuss.rubyonrails.org/u/stephen_O_D)\
**Post date:** [August 16, 2007, 1:52pm UTC](https://discuss.rubyonrails.org/t/gzip-response-to-ajax-requests/17072/1 "2007-08-16T13:52:17Z")

</div>

Hi,

I an application I am working on, each user has a bunch of text files that they can retrieve using a AJAX request. These files don't change after creation, but they are served from a Rails process as they can only be viewed by a logged in user.

Some of the files are a reasonable size, so what I would like to do it gzip them on disk, and then send them gzipped to the browser, avoiding the need to gzip them on the fly.

Is it possible to do this with Rails?

I have done some googling and apache mod\_deflate seems to be the answer to compressing static pages on the fly, but with my case I reckon I should be able to compress once and serve many times instead (plus to Apache my 'static pages' look dynamic).

Also can mod\_deflate compress the output from a Rails (Mongrel) process or does it not touch dynamic content?

Thanks,

Stephen.

---

<div class="post-metadata">

**Author:** ![Peter\_De\_Berdt](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@Peter\_De\_Berdt](https://discuss.rubyonrails.org/u/Peter_De_Berdt)\
**Post date:** [August 16, 2007, 3:09pm UTC](https://discuss.rubyonrails.org/t/gzip-response-to-ajax-requests/17072/2 "2007-08-16T15:09:55Z")

</div>

Apache’s mod\_deflate can compress dynamic content from a mongrel without a problem (all our apps do it), you just add the following lines to the end of your virtual host configuration:

# Deflate

AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript

# … text/xml application/xml application/xhtml+xml text/javascript or whatever text-based mime type you want to send gzipped

BrowserMatch ^Mozilla/4 gzip-only-text/html

BrowserMatch ^Mozilla/4.0[678] no-gzip

BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# The lines above make sure compressed pages are only sent to browsers that support it

The overhead of the deflating is negligible from what we’ve seen, so don’t bother too much trying to cache compressed versions (and how would you handle requests from browsers that don’t support compressed content?). Implementing caching in your Rails application on the other hand might be a very good idea, because it’s the processing of the request in mongrel that takes up most of the cycles when generating a page. If you’ve implemented caching in Rails, make sure you add the following line to your virtual host config before redirecting the request to your mongrel cluster (Apache will then serve the page without mongrel and it will deflate it before sending it to the user):

# Rewrite to check for Rails cached page

RewriteRule ^([^.]+)$ $1.html [QSA]

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![stephen\_O\_D](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@stephen\_O\_D](https://discuss.rubyonrails.org/u/stephen_O_D)\
**Post date:** [August 16, 2007, 4:22pm UTC](https://discuss.rubyonrails.org/t/gzip-response-to-ajax-requests/17072/3 "2007-08-16T16:22:21Z")

</div>

Good point on the browsers that don't support gzip!

The files I am sending are cached (ie they are not generated from the database), but I need to go through the rails process to ensure the user is allowed to access the file they have asked for, so I cannot use Rails caching as it stands. The AJAX request pretty much checks the ownership of the file and pulls it off disk as it is so it should be pretty efficient (I hope!).

Must have a go at setting up Apache, mod\_deflate and mongrel tonight.

Thanks,

Stephen.
