starlette
starlette copied to clipboard
refactor: make GZipMiddleware excluded content types configurable
Summary
This PR refactors the GZipMiddleware to make excluded content types configurable via a constructor parameter instead of using a hardcoded constant.
Checklist
- [x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
- [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
- [x] I've updated the documentation accordingly.
What is the issue you are having?
The DEFAULT_EXCLUDED_CONTENT_TYPES variable is sometimes not what users want. In my case, I want event-stream to be compressed because I'm using data-star and returning a lot of very compressible html.
A workaround I've been using for now has been to either edit the variable directly in the package in my virtual enviroment or:
from fastapi.middleware.gzip import GZipMiddleware
from starlette.middleware import gzip
app = FastAPI()
app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=5)
gzip.DEFAULT_EXCLUDED_CONTENT_TYPES = ()
which is not pretty IMO... would like this to be configurable the same way minimum_size and compresslevel are.