Friday, February 27, 2009

Quick Tip: Apache, mod_deflast and JavaScript

We've been compressing the content we serve with Apache's mod_deflate for a while, but we weren't seeing the performance gain we would like and recently found out why - our JavaScript wasn't being compressed.

I was sure I had told Apache to compress JavaScript and sure enough there was the line in our httpd.conf file:

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

So why wasn't it compressing? It turns out there is some (very understandable) confusion around the MIME type. In our HTML declaration we use <script type="text/javascript"> but that's html. The MIME specification for JavaScript - and this is what is defined in /etc/mime.types - is application/x-javascript.

We modified our AddOutputFilterByType declaration and saw a noticeable increase in load times:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/x-javascript

Any Other Optimization Tips?
If you have any other optimization tips or gotchas please share them here. I'd love to hear how you've increased site performance.

Further Reading
JavaScript MIME Type:
http://annevankesteren.nl/2005/02/javascript-mime-type

Apache Docs on mod_deflate:
http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

How To for mod_deflate:
http://www.howtoforge.com/apache2_mod_deflate

No comments:

Post a Comment