proper cache busting

This commit is contained in:
2025-06-04 11:47:35 +02:00
parent 0f9f9d1b73
commit 88c3bc1b5b
20 changed files with 100 additions and 45 deletions

49
utils/asset_utils.py Normal file
View File

@@ -0,0 +1,49 @@
import os
import hashlib
from functools import lru_cache
from flask import current_app
@lru_cache(maxsize=128)
def get_file_hash(filepath):
"""
Get the MD5 hash of a file's contents.
Uses LRU cache to avoid re-reading files that haven't changed.
Args:
filepath: The path to the file
Returns:
str: The MD5 hash of the file contents
"""
try:
with open(filepath, 'rb') as f:
return hashlib.md5(f.read()).hexdigest()
except (IOError, OSError):
return None
def get_asset_version(filename):
"""
Get the version hash for a static asset.
Args:
filename: The filename of the static asset
Returns:
str: The version hash or None if file not found
"""
if not filename:
return None
# Get the absolute path to the static file
static_dir = os.path.join(current_app.root_path, 'static')
filepath = os.path.join(static_dir, filename)
# Get the file hash
file_hash = get_file_hash(filepath)
# If file not found or error reading, return None
if not file_hash:
return None
# Return first 8 characters of hash as version
return file_hash[:8]