最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

If I used gzipped JavaScript files, I get illegal charactertoken errors in both Chrome and Firefox - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Feb 29, 2012 at 15:38 John FreemanJohn Freeman 2,6921 gold badge30 silver badges39 bronze badges 1
  • 1 This should be done in the central server configuration, then you don't have to deal with manually gzipped files. Not sure whether it makes much sense on a local dev server though? – Pekka Commented Feb 29, 2012 at 15:40
Add a comment  | 

3 Answers 3

Reset to default 12

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.

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.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论