提供 gzip 文件时使用什么“Content-Type"标头?

2022-01-17 00:00:00 gzip http-headers http javascript

我正在提供我的 css/javascript 文件的 gzip 副本.根据教程,我在提供这些文件时将内容类型设置为 application/gzip.但是,chrome 似乎没有解压缩这些文件,并且对于 javascript 文件,我得到了很多非法字符"错误.如果我查看源代码,我仍然看到文件是压缩的,而不是未压缩的.

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.

我的问题是,我应该为这些文件设置什么内容类型,以便浏览器将它们正确解释为 gzip 压缩的 css/js 文件并解压缩它们?如果我只设置了text/javascripttext/css,浏览器还能正确解释它们吗?

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?

完整的响应标头:

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

推荐答案

响应中的压缩内容在Content-Encoding中指明.Content-Type 应该保持不变,也就是说,它应该反映被压缩的底层媒体类型.

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

参见 14.11 内容编码 和 3.5 内容编码 的 RFC 2616 了解更多信息.

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

相关文章