serowwanie staticow fix

This commit is contained in:
Mateusz Gruszczyński
2025-08-28 11:55:00 +02:00
parent 248d1dcd84
commit d42ce7fcc4
3 changed files with 22 additions and 11 deletions

View File

@@ -24,8 +24,10 @@ MAIN_ADMIN_PASSWORD=admin
# Blokuj boty (ustawia także X-Robots-Tag) (True/False) # Blokuj boty (ustawia także X-Robots-Tag) (True/False)
BLOCK_BOTS=True 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=max-age=600
CACHE_CONTROL_HEADER_STATIC=max-age=72000
USE_ETAGS=True
# Dodatkowe PRAGMA (opcjonalnie, jeśli chcesz dokładać własne) # Dodatkowe PRAGMA (opcjonalnie, jeśli chcesz dokładać własne)
PRAGMA_HEADER= PRAGMA_HEADER=

25
app.py
View File

@@ -429,6 +429,15 @@ def apply_headers(response):
for header, value in custom_headers.items(): for header, value in custom_headers.items():
response.headers[header] = str(value) 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 # Wykluczenia
if response.status_code in (301, 302, 303, 307, 308): if response.status_code in (301, 302, 303, 307, 308):
response.headers.pop("Vary", None) response.headers.pop("Vary", None)
@@ -445,21 +454,17 @@ def apply_headers(response):
response.headers.pop("Vary", None) response.headers.pop("Vary", None)
elif request.path.startswith("/admin"): elif request.path.startswith("/admin"):
response.headers.pop("Vary", None) response.headers.pop("Vary", None)
response.headers["Cache-Control"] = ( response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
"no-store, no-cache, must-revalidate, max-age=0"
)
else: else:
response.headers["Vary"] = "Cookie, Accept-Encoding" response.headers["Vary"] = "Cookie, Accept-Encoding"
default_cache = app.config.get("CACHE_CONTROL_HEADER") or "private, max-age=0" default_cache = app.config.get("CACHE_CONTROL_HEADER") or "private, max-age=0"
response.headers["Cache-Control"] = default_cache response.headers["Cache-Control"] = default_cache
# Blokowanie botów # Blokowanie botów (ale NIE dla /static/)
if app.config.get("BLOCK_BOTS", False): if app.config.get("BLOCK_BOTS", False) and not request.path.startswith("/static/"):
cc = ( cc_override = app.config.get("CACHE_CONTROL_HEADER")
app.config.get("CACHE_CONTROL_HEADER") if cc_override:
or "no-store, no-cache, must-revalidate, max-age=0" response.headers["Cache-Control"] = cc_override
)
response.headers["Cache-Control"] = cc
response.headers["X-Robots-Tag"] = ( response.headers["X-Robots-Tag"] = (
app.config.get("ROBOTS_TAG") or "noindex, nofollow, nosnippet, noarchive" app.config.get("ROBOTS_TAG") or "noindex, nofollow, nosnippet, noarchive"
) )

View File

@@ -38,6 +38,10 @@ class Config:
# Indeksowanie / cache / robots # Indeksowanie / cache / robots
BLOCK_BOTS = _get_bool("BLOCK_BOTS", True) BLOCK_BOTS = _get_bool("BLOCK_BOTS", True)
CACHE_CONTROL_HEADER = _get_str("CACHE_CONTROL_HEADER", "max-age=600") 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", "") PRAGMA_HEADER = _get_str("PRAGMA_HEADER", "")
ROBOTS_TAG = _get_str("ROBOTS_TAG", "noindex, nofollow, nosnippet, noarchive") ROBOTS_TAG = _get_str("ROBOTS_TAG", "noindex, nofollow, nosnippet, noarchive")