John Freeman
John Freeman

Reputation: 2692

If I used gzipped JavaScript files, I get illegal character/token errors in both Chrome and Firefox

I'm using the built-in server on my MacBook.

Say my script is foo.js. This works in both my test browsers (Firefox 10, Chrome 17):

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

If I gzip the file, however, both browsers will give me an illegal token/character error after I change the above line to this:

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

What gives? Does the type need to be changed too or something? Where is this kind of practice documented? Whenever I see it mentioned on the web to "zip your scripts!", no one ever bothers to mention that you need to do anything special with the links.

Upvotes: 13

Views: 7701

Answers (3)

Sachchit Bansal
Sachchit Bansal

Reputation: 496

I had similar kind of error. Check in the network tab corresponding to your foo.js. In response header, the requested header should have Content-Encoding:gzip and Content-Type:application/javascript. Otherwise, browser will not decompress your file. Browser should know what kind of content encoding is done over the file.

Upvotes: 4

Ali
Ali

Reputation: 267077

This error can also occur if your gzip setting is too high. I've tried gzip -9 and even when sending the correct Content-Encoding, i got this error. When I just ran a regular gzip, via gzip file.js, it then works correctly.

Upvotes: 2

Sjoerd
Sjoerd

Reputation: 75609

The webserver needs to tell the browser that the content is gzipped. This is done using the content-encoding header. Maybe you can configure your webserver to supply this header with files ending in .gz.

Instead of compressing the files statically, it is also common for the webserver to have functionality to compress documents on-the-fly. This means that you put the normal foo.js file in the document root, and configure the webserver to compress it when sending it to the client.

Upvotes: 12

Related Questions