diff --git a/CHANGES.md b/CHANGES.md index 33a5fe48f..3f8fb4fbc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # Release Notes +## 0.3.10 (TBD) + +### titiler.application + +- switch to `starlette_cramjam` compression middleware (ref: https://github.com/developmentseed/titiler/issues/369) + ## 0.3.9 (2021-09-07) ### titiler.core diff --git a/src/titiler/application/setup.py b/src/titiler/application/setup.py index 9f07687ca..afb3f55ac 100644 --- a/src/titiler/application/setup.py +++ b/src/titiler/application/setup.py @@ -9,11 +9,11 @@ "rio-cogeo>=2.2", "titiler.core", "titiler.mosaic", - "brotli-asgi>=1.0.0", + "starlette-cramjam>=0.1.0,<0.2", "python-dotenv", ] extra_reqs = { - "test": ["pytest", "pytest-cov", "pytest-asyncio", "requests"], + "test": ["pytest", "pytest-cov", "pytest-asyncio", "requests", "brotlipy"], "server": ["uvicorn[standard]>=0.12.0,<0.14.0"], } diff --git a/src/titiler/application/tests/routes/test_cog.py b/src/titiler/application/tests/routes/test_cog.py index 860ae9472..04375bde3 100644 --- a/src/titiler/application/tests/routes/test_cog.py +++ b/src/titiler/application/tests/routes/test_cog.py @@ -194,6 +194,15 @@ def test_tile(rio, app): assert response.status_code == 200 assert response.headers["content-encoding"] == "br" + # Exclude png from compression middleware + headers = {"Accept-Encoding": "br, gzip"} + response = app.get( + "/cog/tiles/8/87/48.png?url=https://myurl.com/cog.tif&nodata=0&return_mask=false", + headers=headers, + ) + assert response.status_code == 200 + assert "content-encoding" not in response.headers + # Test gzip fallback headers = {"Accept-Encoding": "gzip"} response = app.get( diff --git a/src/titiler/application/titiler/application/main.py b/src/titiler/application/titiler/application/main.py index 0153187ab..5b2fca43e 100644 --- a/src/titiler/application/titiler/application/main.py +++ b/src/titiler/application/titiler/application/main.py @@ -2,8 +2,6 @@ import logging -from brotli_asgi import BrotliMiddleware - from titiler.application.custom import templates from titiler.application.routers import cog, mosaic, stac, tms from titiler.application.settings import ApiSettings @@ -22,6 +20,7 @@ from starlette.middleware.cors import CORSMiddleware from starlette.requests import Request from starlette.responses import HTMLResponse +from starlette_cramjam.middleware import CompressionMiddleware logging.getLogger("botocore.credentials").disabled = True logging.getLogger("botocore.utils").disabled = True @@ -62,7 +61,18 @@ allow_headers=["*"], ) -app.add_middleware(BrotliMiddleware, minimum_size=0, gzip_fallback=True) +app.add_middleware( + CompressionMiddleware, + minimum_size=0, + exclude_mediatype={ + "image/jpeg", + "image/jpg", + "image/png", + "image/jp2", + "image/webp", + }, +) + app.add_middleware( CacheControlMiddleware, cachecontrol=api_settings.cachecontrol,