Skip to content

Commit abe3554

Browse files
authored
Add notes about the GZip middleware (#2880)
1 parent a9a8dab commit abe3554

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

docs/middleware.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ Handles GZip responses for any request that includes `"gzip"` in the `Accept-Enc
178178

179179
The middleware will handle both standard and streaming responses.
180180

181+
??? info "Buffer on streaming responses"
182+
On streaming responses, the middleware will buffer the response before compressing it.
183+
184+
The idea is that we don't want to compress every small chunk of data, as it would be inefficient.
185+
Instead, we buffer the response until it reaches a certain size, and then compress it.
186+
187+
This may cause a delay in the response, as the middleware waits for the buffer to fill up before compressing it.
188+
181189
```python
182190
from starlette.applications import Starlette
183191
from starlette.middleware import Middleware
@@ -198,7 +206,8 @@ The following arguments are supported:
198206
* `minimum_size` - Do not GZip responses that are smaller than this minimum size in bytes. Defaults to `500`.
199207
* `compresslevel` - Used during GZip compression. It is an integer ranging from 1 to 9. Defaults to `9`. Lower value results in faster compression but larger file sizes, while higher value results in slower compression but smaller file sizes.
200208

201-
The middleware won't GZip responses that already have a `Content-Encoding` set, to prevent them from being encoded twice.
209+
The middleware won't GZip responses that already have either a `Content-Encoding` set, to prevent them from
210+
being encoded twice, or a `Content-Type` set to `text/event-stream`, to avoid compressing server-sent events.
202211

203212
## BaseHTTPMiddleware
204213

0 commit comments

Comments
 (0)