From 76e7f59f25705cd048a1ae23d6499f0cd488928f Mon Sep 17 00:00:00 2001 From: Tugamer89 <61603718+Tugamer89@users.noreply.github.com> Date: Tue, 26 May 2026 05:03:18 +0000 Subject: [PATCH] perf: add gzip compression middleware to reduce payload size This commit adds `GZipMiddleware` to the FastAPI application to compress HTTP responses larger than 1000 bytes. This significantly reduces network transfer times and bandwidth for API payloads and static files, improving the overall application performance and loading speed. Impact: Reduces the size of transmitted static assets (like JS/CSS) and JSON responses, leading to faster load times. Measurement: Verify by checking the `Content-Encoding: gzip` header on network requests using curl (e.g. `curl -s -I -H "Accept-Encoding: gzip" http://127.0.0.1:8000/static/js/main.js`). Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.py b/main.py index ea35f07..ad76bfc 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import uvicorn from fastapi import FastAPI +from fastapi.middleware.gzip import GZipMiddleware from fastapi.staticfiles import StaticFiles from fastapi_csrf_protect import CsrfProtect from securecookies import SecureCookiesMiddleware @@ -31,6 +32,9 @@ def get_config(): https_only=not settings.DEBUG, ) +# Middleware per la compressione GZip +app.add_middleware(GZipMiddleware, minimum_size=1000) + # Middleware per gestire il "remember me" app.add_middleware(PreventSessionOverwriteMiddleware)