Using gzipped javascript files

Giganews Newsgroups
Subject: Using gzipped javascript files
Posted by:  (O)enone (oeno…@nowhere.com)
Date: Wed, 4 Jul 2007

Hello,

I've read in numerous web sites that I should be able to gzip my javascript
files in order to reduce the amount of data transfer required to get them
from my web server (running IIS6 in Windows Server 2003) to my web browser.

Despite trying all sorts of things, I've been unable to get this to work.
Could someone help point me in the right direction?

I have taken my original javascript source file (mycode.js) and gzipped it
to produce mycode.js.gz. I then change the script tag in my HTML page from:

    <script type="text/javascript" src="mycode.js">

(which works fine) to reference the gzip file instead:

    <script type="text/javascript" src="mycode.js.gz">

At this point, the script stops working.

If I look at the network transfer using Firebug in Firefox, I can see that
the .gz file was successfully downloaded. The request headers include
"Accept-Encoding: gzip,deflate" and the response includes "Content-Type:
application/x-gzip". The file is obviously being interpreted directly in its
compressed form (I can see from the Firebug errors the raw data from the .gz
file is being displayed with an "Illegal character" error) rather than being
decompressed first.

Is this because I am delivering a file whose content type is
"application/x-gzip", when I should actually be delivering a file whose
content type in "text/javascript" and whose encoding is "gzip"? If so, how
can I configure my IIS server to respond in this way?

Many thanks,

--

(O)enone

Replies