Skip to content

Commit 38e0335

Browse files
fix(api): only compress > 10kb (#404)
1 parent dd5077d commit 38e0335

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

crates/api/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ use tokio::{
3232
use tower::ServiceBuilder;
3333
use tower_http::{
3434
catch_panic::CatchPanicLayer,
35-
compression::CompressionLayer,
35+
compression::{
36+
CompressionLayer,
37+
predicate::{NotForContentType, Predicate, SizeAbove},
38+
},
3639
cors::CorsLayer,
3740
decompression::RequestDecompressionLayer,
3841
sensitive_headers::SetSensitiveHeadersLayer,
@@ -147,7 +150,15 @@ impl Application {
147150
.layer(SetSensitiveHeadersLayer::new(sensitive_headers))
148151
.layer(CorsLayer::permissive())
149152
.layer(RequestDecompressionLayer::new())
150-
.layer(CompressionLayer::new())
153+
// Only compress responses larger than 10KB, and add the default content-type filtering
154+
.layer(
155+
CompressionLayer::default().compress_when(
156+
SizeAbove::new(10 * 1024)
157+
.and(NotForContentType::GRPC)
158+
.and(NotForContentType::IMAGES)
159+
.and(NotForContentType::SSE),
160+
),
161+
)
151162
// Catch panics and convert them into responses.
152163
.layer(CatchPanicLayer::new())
153164
.layer(

0 commit comments

Comments
 (0)