Invalidate CSS cache when updated

This commit is contained in:
Thomas Sileo 2022-08-13 15:35:39 +02:00
parent 59688ad5f6
commit ce80e98d33
3 changed files with 12 additions and 1 deletions

View file

@ -1,3 +1,4 @@
import hashlib
import os
import secrets
from pathlib import Path
@ -25,6 +26,14 @@ try:
except ImportError:
pass
# Force reloading cache when the CSS is updated
CSS_HASH = "none"
try:
css_data = (ROOT_DIR / "app" / "static" / "css" / "main.css").read_bytes()
CSS_HASH = hashlib.md5(css_data, usedforsecurity=False).hexdigest()
except FileNotFoundError:
pass
VERSION = f"2.0.0+{VERSION_COMMIT}"
USER_AGENT = f"microblogpub/{VERSION}"

View file

@ -26,6 +26,7 @@ from app.actor import LOCAL_ACTOR
from app.ap_object import Attachment
from app.ap_object import Object
from app.config import BASE_URL
from app.config import CSS_HASH
from app.config import DEBUG
from app.config import VERSION
from app.config import generate_csrf_token
@ -102,6 +103,7 @@ async def render_template(
"request": request,
"debug": DEBUG,
"microblogpub_version": VERSION,
"css_hash": CSS_HASH,
"is_admin": is_admin,
"csrf_token": generate_csrf_token(),
"highlight_css": HIGHLIGHT_CSS,

View file

@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="/static/css/main.css">
<link rel="stylesheet" href="/static/css/main.css?v={{ css_hash }}">
<link rel="alternate" title="{{ local_actor.display_name}}'s microblog" type="application/json" href="{{ url_for("json_feed") }}" />
<link rel="alternate" href="{{ url_for("rss_feed") }}" type="application/rss+xml" title="{{ local_actor.display_name}}'s microblog">
<link rel="alternate" href="{{ url_for("atom_feed") }}" type="application/atom+xml" title="{{ local_actor.display_name}}'s microblog">