views:

836

answers:

1

I'm using an .htaccess file to allow my pages to call gzipped JavaScript files.

It works fine in ie8 & ff3, but the script is not loaded in Safari (or Chrome).

Here is the .htaccess file I'm using:

<files *.js.gz>
ForceType text/javascript
Header set Content-Encoding: gzip
</files>

Then, for example, I can call JS files from my HTML pages:

<script src="foo.js.gz"></script>

How can I modify the above .htaccess code to get it working for Safari (and still work for other browsers)?

+4  A: 

I assume you're trying to use Gzip compression for faster downloads of assets and text content. It's a great technique for speeding up your site.

Safari, Chrome, and IE6 all have problems with Gzipped downloads. Also, Apache will do the gzip compression for you, there is no need to manually gzip files. Try this snippet:

# This uses mod_deflate, which is pretty standard on Apache 2.  Loading
# mod_deflate looks like this:
#
#   LoadModule deflate_module modules/mod_deflate.so
#
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html

That should safely gzip all text before it's sent out from Apache, and only for browsers that support it. You can link to JavaScript and other assets normally, just myscript.js, with no gz extension or compression on disk.

mixonic
Thaks for the info. But... I don't have access to apache. That is why I'm using an htaccess file. Do you know how to change the htaccess file to get Safari to read my gzipped file?
htaccess is an Apache configuration file. Safari will not read gzipped files, as I stated above. The solution is to serve Safari, Chrome, and IE6 a plain file, but serve other browsers gzipped content. You may still be able to use the syntax I dropped above, it's going to depend on how the Apache server is configured. Before going too far down that path, use firebug or YSlow to be sure the server isn't already gzipping content for you.
mixonic
Thanks for the lesson. But... I have only one gzipped file to add to a site. I would like to avoid modifying/breaking the apache config file... So, I cheated. I used PPK's browser detection script. If the browser is Safari, I dynamically load the non-gzipped js file. Otherwise, I dynamically load the gzipped version Used document.createElement("script") to dynamically load script.
i fail to see where your configuration will prevent gzip taking place for chrome browsers...
HorusKol