70

I'm serving gzipped copies of my css / javascript files. As per a tutorial, I set the content-type as application/gzip when serving these files. However, chrome doesn't seem to un-gzip these files, and for the javascript files I get a lot of 'illegal character' errors. If I view source, I still see the files as compressed, rather than uncompressed.

My question is, what should I set as the content type for these files, in order for the browser to interpret them correctly as gzipped css / js files, and un-gzip them? If I just set text/javascript or text/css, will the browser still interpret them correctly?

Edit: Full response headers:

HTTP/1.1 200 OK
x-amz-id-2: UIBkZT/MuFxsmn+3nVOzEO63rRY99l3traCbMExUgSdGHUrOIPtNp34h1+ujYKmt
x-amz-request-id: 19346C9B01D8FC62
Date: Mon, 12 May 2014 03:59:51 GMT
Content-Encoding: gzip
Last-Modified: Mon, 12 May 2014 02:24:54 GMT
ETag: "561080b5e19f6daea2e74fd5a0623c79"
Accept-Ranges: bytes
Content-Type: application/x-gzip
Content-Length: 5153
Server: AmazonS3
Ali
  • 261,656
  • 265
  • 575
  • 769

1 Answers1

118

Compressed content in the response is indicated in the Content-Encoding. The Content-Type should remain the same, that is, it should reflect the underlying media type that is compressed.

Content-Type: application/javascript
Content-Encoding: gzip

See sections 14.11 Content-Encoding and 3.5 Content Codings of RFC 2616 for more information.

Community
  • 1
  • 1
Joel Allison
  • 2,091
  • 1
  • 12
  • 9
  • 1
    I'm already doing that, but it still seems to be not interpreting the script correctly. I've edited to put the full headers in the question – Ali May 12 '14 at 04:02
  • It gives the error `Uncaught SyntaxError: Unexpected token ILLEGAL` on line 1, and when I check it, I see that it has the file name at that line, e.g `�� aRjquery-1.7.1.min.js�}`. I compressed the files using `gzip -9`, do I need to gzip using a lower setting? Or do I need to delete the filenames from the start of file? – Ali May 12 '14 at 04:05
  • 2
    OK, I see the expected Content-Encoding, but the Content-Type should reflect the underlying media type that is compressed. Try changing `application/x-gzip` back to `application/javascript`. – Joel Allison May 12 '14 at 04:06
  • 2
    I also see AmazonS3 is the server. Perhaps http://stackoverflow.com/questions/5442011/serving-gzipped-css-and-javascript-from-amazon-cloudfront-via-s3 is also relevant. – Joel Allison May 12 '14 at 04:26