starlette icon indicating copy to clipboard operation
starlette copied to clipboard

refactor: make GZipMiddleware excluded content types configurable

Open shreyas-dev opened this issue 2 months ago • 2 comments

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.

shreyas-dev avatar Oct 15 '25 10:10 shreyas-dev

What is the issue you are having?

Kludex avatar Oct 18 '25 19:10 Kludex

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.

petoknm avatar Nov 04 '25 16:11 petoknm