From b923851284064aa66527007129d55696e7385ba2 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 29 Jun 2026 11:16:30 +0800 Subject: [PATCH] =?UTF-8?q?docs(main):=20=E6=B3=A8=E6=98=8E=20CompressionL?= =?UTF-8?q?ayer=20=E9=BB=98=E8=AE=A4=E8=B7=B3=E8=BF=87=20image/*?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免后续误以为压缩层会对 WebP/PNG/JPEG 等已是压缩格式的图片再次 压缩而浪费 CPU。tower-http 的 DefaultPredicate 开箱即跳过 image/* (例外 image/svg+xml)、gRPC、SSE 及 <32 字节响应,无需额外配置。 纯文档性注释,无行为变更。 --- src/main.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main.rs b/src/main.rs index 8dd9c84..fea3db8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,6 +108,15 @@ fn parse_compression_algorithms(env: &str) -> Option { /// 根据 COMPRESSION_ALGORITHMS 环境变量构造 CompressionLayer。 /// 未设置或设置为 "all" 时启用全部算法;设置为 ""、"none" 或 "off" 时禁用。 +/// +/// CompressionLayer 使用 tower-http 的 `DefaultPredicate`,开箱即用即: +/// - 跳过 `image/*` content-type(WebP/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 { use tower_http::compression::CompressionLayer;