From d780938d3bcfcd37b34798d88005aa63af565767 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Mon, 5 Dec 2022 22:33:50 +1100 Subject: [PATCH] Minify html response --- app.py | 15 +++++++++++++++ requirements.txt | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 2ada443..3d33530 100644 --- a/app.py +++ b/app.py @@ -7,6 +7,7 @@ from decorators import validate_person, validate_topset, validate_workout from db import DataBase from utils import get_people_and_exercise_rep_maxes, convert_str_to_date, get_earliest_and_latest_workout_date, filter_workout_topsets, get_exercise_ids_from_workouts, first_and_last_visible_days_in_month from flask_htmx import HTMX +from htmlmin.main import minify app = Flask(__name__) app.config.from_pyfile('config.py') @@ -15,6 +16,20 @@ db = DataBase(app) htmx = HTMX(app) +@app.after_request +def response_minify(response): + """ + minify html response to decrease site traffic + """ + if response.content_type == u'text/html; charset=utf-8': + response.set_data( + minify(response.get_data(as_text=True)) + ) + + return response + return response + + @ app.route("/") def dashboard(): all_topsets = db.get_all_topsets() diff --git a/requirements.txt b/requirements.txt index 790df20..62a7815 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ Jinja2==3.0.1 jinja-partials==0.1.1 psycopg2-binary==2.9.3 flask-htmx==0.2.0 -python-dateutil==2.8.2 \ No newline at end of file +python-dateutil==2.8.2 +htmlmin==0.1.12 \ No newline at end of file