【问题标题】:Chrome Devtool do not show Content-Encoding in response headersChrome Devtool 不在响应标头中显示 Content-Encoding
【发布时间】:2025-12-10 07:20:03
【问题描述】:

我正在启用 gzip 压缩以提高 Google PageSpeed Insights 的速度:

如果压缩 mantaspersonalizadas.com/fonts/Cocktail-Shaker.svg,将节省 144.1 KB(减少 65%)。 如果压缩 mantaspersonalizadas.com/fonts/Cocktail-Shaker.ttf,将节省 62.6 KB(减少 49%)。 如果压缩 mantaspersonalizadas.com/fonts/Cocktail-Shaker.woff,将节省 2.6 KB(减少 5%)。

我使用 Apache 服务器,我将此代码添加到 .htacess:

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/opentype

# For Olders Browsers Which Can't Handle Compression
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

如何检查我的本地服务器是否启用了放气?

Chrome DevTools 不显示内容编码:

【问题讨论】:

    标签: .htaccess google-chrome pagespeed mod-deflate


    【解决方案1】:

    让我们来测试一下吧。

    对 example.com 的 Chrome 请求(包含 gzip):

    GET / HTTP/1.1
    Host: www.example.com
    Connection: keep-alive
    Cache-Control: max-age=0
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: en-US,en;q=0.8
    If-None-Match: "359670651"
    If-Modified-Since: Fri, 09 Aug 2013 23:54:35 GMT
    

    来自 example.com 的 Chrome 响应(不包含 gzip):

    HTTP/1.1 304 Not Modified
    Accept-Ranges: bytes
    Cache-Control: max-age=604800
    Date: Thu, 08 Dec 2016 14:06:04 GMT
    Etag: "359670651"
    Expires: Thu, 15 Dec 2016 14:06:04 GMT
    Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
    Server: ECS (iad/182A)
    Vary: Accept-Encoding
    X-Cache: HIT
    

    Curl 请求(克隆 Chrome 请求,包含 gzip):

    curl -I
        -H "GET / HTTP/1.1"
        -H "Host: www.example.com"
        -H "Connection: keep-alive"
        -H "Cache-Control: max-age=0"
        -H "Upgrade-Insecure-Requests: 1"
        -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36"
        -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
        -H "Accept-Encoding: gzip, deflate, sdch"
        -H "Accept-Language: en-US,en;q=0.8"
        -H "If-None-Match: "359670651""
        -H "If-Modified-Since: Fri, 09 Aug 2013 23:54:35 GMT"
        example.com
    

    Curl 响应(包含 gzip):

    HTTP/1.1 200 OK
    Content-Encoding: gzip
    Accept-Ranges: bytes
    Cache-Control: max-age=604800
    Content-Type: text/html
    Date: Thu, 08 Dec 2016 14:18:34 GMT
    Etag: "359670651"
    Expires: Thu, 15 Dec 2016 14:18:34 GMT
    Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
    Server: ECS (iad/182A)
    X-Cache: HIT
    x-ec-custom-error: 1
    Content-Length: 606
    

    结论

    Chrome 目前不显示内容编码。火狐也不行。我不知道为什么。

    现在我知道他们并没有向我展示所有内容,我再也不会信任响应标头信息。

    一直卷曲。

    【讨论】:

    • 在我的生产服务器搞砸了 2 小时后,我偶然发现了这个!我可以确认,截至 2017 年 10 月,Chrome+Firefox 甚至不会在 DevTools 中显示 主页 的压缩阅读器,但它们会为所有其他资源(例如 CSS、JS 甚至Ajax 加载页面)。见这里:s1.postimg.org/2rv29kk73j/z_Shot_1507239352.png 提议的 curl 技巧对我来说很可靠。
    【解决方案2】:

    Chrome 浏览器似乎有问题,虽然 deflate 在服务器上不起作用,但我也遇到了同样的问题,但后来我尝试了 Firefox 并使用了在标题中正确显示 Content-Encoding 的 firebug。

    Chrome中下载的文件大小也没有显示未压缩的文件大小,而Firefox显示了正确的压缩文件大小。

    【讨论】:

      【解决方案3】:

      作为@Jay stated

      Chrome 目前不显示内容编码。火狐也不行。我不知道为什么。

      截至 2018 年 12 月,Chrome 和 Firefox 中的行为相同,而 Firebug 消失了。

      Firefox Developer Edition 对我有用,在响应标头部分显示正确的内容编码([F12] -> 网络选项卡 -> 选择资源 -> 标头选项卡)。

      Screenshot: content encoding shows up in Firefox Developer Tools

      【讨论】:

        最近更新 更多