Web Performance Optimization & Lighthouse · 课时

压缩与传输编码

使用 gzip 和 Brotli 压缩、正确的内容类型以及预压缩,减少网络传输的数据量,让响应在各种连接上都能更快到达。

第 4 / 4 课13 个步骤

压缩与传输编码 是 CoddyKit 上的免费 Web Performance Optimization & Lighthouse 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web Performance Optimization & Lighthouse 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web Performance Optimization & Lighthouse 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Compress?

Text assets like HTML, CSS, JS, JSON, and SVG are highly compressible. Compressing them before transfer can cut payloads by 70-90%, directly speeding up load on every network.

How Negotiation Works

The browser advertises supported algorithms with the Accept-Encoding header; the server responds with the chosen one in Content-Encoding.

Accept-Encoding: br, gzip
Content-Encoding: br

gzip

gzip is universally supported and a safe baseline. It offers good compression with low CPU cost and is enabled on virtually every server and CDN.

Brotli

Brotli (br) typically compresses text 15-25% smaller than gzip. It is supported in all modern browsers over HTTPS, making it the preferred choice today.

Compress the Right Types

Compress text-based MIME types only. Already-compressed binaries like JPEG, PNG, WebP, MP4, and woff2 fonts gain nothing and waste CPU if recompressed.

Static Precompression

For static assets, precompress at build time at the highest level. The server then serves the .br or .gz file directly, avoiding per-request CPU.

# nginx serving precompressed files
brotli_static on;
gzip_static on;

Dynamic Compression Levels

For dynamic responses, use a lower compression level so CPU cost does not delay the response. High levels are best reserved for static, build-time compression.

Express Example

In Node, the compression middleware adds gzip/Brotli to dynamic responses with sensible defaults.

const compression = require('compression');
app.use(compression());

Verifying Compression

Inspect a response in the Network panel: check Content-Encoding: br or gzip and compare transferred size to resource size. Lighthouse flags uncompressed text assets.

Compression and HTTPS

Modern compression for sensitive content is served over HTTPS, which also avoids historic attacks. Brotli in browsers is only offered on secure connections.

Strategy Summary

  • Prefer Brotli, fall back to gzip.
  • Compress text types only.
  • Precompress static assets at max level.
  • Use lower levels for dynamic responses.

Quick Check

You enable maximum-level compression on dynamically generated API responses and notice slower responses under load. What is the better approach?

Recap

You learned to shrink transfers with gzip and Brotli, negotiated via Accept-Encoding. Compress text types only, precompress static assets at max level, use lower levels for dynamic responses, and verify with the Network panel.

免费开始

用 AI 导师学习 Web Performance Optimization & Lighthouse — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「压缩与传输编码」课时是免费的吗?

是的 — 「压缩与传输编码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web Performance Optimization & Lighthouse 课程的其余内容,请升级到 CoddyKit PRO。 Web Performance Optimization & Lighthouse 课程共包含 4 节课。

「压缩与传输编码」这节课中我会学到什么?

使用 gzip 和 Brotli 压缩、正确的内容类型以及预压缩,减少网络传输的数据量,让响应在各种连接上都能更快到达。 你通过在浏览器中直接运行的动手代码来练习 Web Performance Optimization & Lighthouse,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web Performance Optimization & Lighthouse 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web Performance Optimization & Lighthouse 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「压缩与传输编码」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web Performance Optimization & Lighthouse 课中编写并运行代码吗?

能。每节 Web Performance Optimization & Lighthouse 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. HTTP/2 与 HTTP/3 基础
  2. 浏览器与服务器缓存
  3. 内容分发网络(CDN)
  4. 压缩与传输编码
← 返回 Web Performance Optimization & Lighthouse