From d42ce7fcc4ee88d5fa201e0f8864c908a98fbb27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Thu, 28 Aug 2025 11:55:00 +0200 Subject: [PATCH] serowwanie staticow fix --- .env.example | 4 +++- app.py | 25 +++++++++++++++---------- config.py | 4 ++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 15980da..0cacd24 100644 --- a/.env.example +++ b/.env.example @@ -24,8 +24,10 @@ MAIN_ADMIN_PASSWORD=admin # Blokuj boty (ustawia także X-Robots-Tag) (True/False) BLOCK_BOTS=True -# Wartość nagłówka Cache-Control dla stron publicznych +# Wartość nagłówka Cache-Control dla stron publicznych i elementóœ statycznych CACHE_CONTROL_HEADER=max-age=600 +CACHE_CONTROL_HEADER_STATIC=max-age=72000 +USE_ETAGS=True # Dodatkowe PRAGMA (opcjonalnie, jeśli chcesz dokładać własne) PRAGMA_HEADER= diff --git a/app.py b/app.py index 9d5c3f7..1752f33 100644 --- a/app.py +++ b/app.py @@ -429,6 +429,15 @@ def apply_headers(response): for header, value in custom_headers.items(): response.headers[header] = str(value) + if request.path.startswith("/static/"): + response.headers.pop("Content-Disposition", None) + response.headers["Vary"] = "Accept-Encoding" + response.headers["Cache-Control"] = app.config.get("CACHE_CONTROL_HEADER_STATIC") + if app.config.get("USE_ETAGS", True) and "ETag" not in response.headers: + response.add_etag() + response.make_conditional(request) + return response + # Wykluczenia if response.status_code in (301, 302, 303, 307, 308): response.headers.pop("Vary", None) @@ -445,21 +454,17 @@ def apply_headers(response): response.headers.pop("Vary", None) elif request.path.startswith("/admin"): response.headers.pop("Vary", None) - response.headers["Cache-Control"] = ( - "no-store, no-cache, must-revalidate, max-age=0" - ) + response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0" else: response.headers["Vary"] = "Cookie, Accept-Encoding" default_cache = app.config.get("CACHE_CONTROL_HEADER") or "private, max-age=0" response.headers["Cache-Control"] = default_cache - # Blokowanie botów - if app.config.get("BLOCK_BOTS", False): - cc = ( - app.config.get("CACHE_CONTROL_HEADER") - or "no-store, no-cache, must-revalidate, max-age=0" - ) - response.headers["Cache-Control"] = cc + # Blokowanie botów (ale NIE dla /static/) + if app.config.get("BLOCK_BOTS", False) and not request.path.startswith("/static/"): + cc_override = app.config.get("CACHE_CONTROL_HEADER") + if cc_override: + response.headers["Cache-Control"] = cc_override response.headers["X-Robots-Tag"] = ( app.config.get("ROBOTS_TAG") or "noindex, nofollow, nosnippet, noarchive" ) diff --git a/config.py b/config.py index 760e65c..c1e7372 100644 --- a/config.py +++ b/config.py @@ -38,6 +38,10 @@ class Config: # Indeksowanie / cache / robots BLOCK_BOTS = _get_bool("BLOCK_BOTS", True) CACHE_CONTROL_HEADER = _get_str("CACHE_CONTROL_HEADER", "max-age=600") + CACHE_CONTROL_HEADER_STATIC = _get_str("CACHE_CONTROL_HEADER_STATIC", "max-age=3600") + + USE_ETAGS = _get_bool("USE_ETAGS", True) + PRAGMA_HEADER = _get_str("PRAGMA_HEADER", "") ROBOTS_TAG = _get_str("ROBOTS_TAG", "noindex, nofollow, nosnippet, noarchive")