docs(main): 注明 CompressionLayer 默认跳过 image/*
Some checks failed
CI / check (push) Failing after 5m37s
CI / build (push) Has been skipped

避免后续误以为压缩层会对 WebP/PNG/JPEG 等已是压缩格式的图片再次
压缩而浪费 CPU。tower-http 的 DefaultPredicate 开箱即跳过 image/*
(例外 image/svg+xml)、gRPC、SSE 及 <32 字节响应,无需额外配置。

纯文档性注释,无行为变更。
This commit is contained in:
xfy 2026-06-29 11:16:30 +08:00
parent 74f8c212f6
commit b923851284

View File

@ -108,6 +108,15 @@ fn parse_compression_algorithms(env: &str) -> Option<CompressionAlgorithms> {
/// 根据 COMPRESSION_ALGORITHMS 环境变量构造 CompressionLayer。
/// 未设置或设置为 "all" 时启用全部算法;设置为 ""、"none" 或 "off" 时禁用。
///
/// CompressionLayer 使用 tower-http 的 `DefaultPredicate`,开箱即用即:
/// - 跳过 `image/*` content-typeWebP/PNG/JPEG/GIF 等已是压缩格式,再压浪费 CPU
/// 唯一例外是 `image/svg+xml`,作为 XML 文本可被压缩);
/// - 跳过 gRPC 与 `text/event-stream`SSE
/// - 跳过小于 32 字节的响应。
///
/// 因此无需在此处对图片响应做额外的 content-type 过滤。另:图片实际挂在
/// `static_routes`(无中间件),根本不经此层,详见下方路由 merge 处。
#[cfg(feature = "server")]
fn compression_layer_from_env() -> Option<tower_http::compression::CompressionLayer> {
use tower_http::compression::CompressionLayer;