Add automatic cache-busting for static files

Use file modification time hash for base.js and tufte.css to
automatically bust browser cache when files are updated.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-03 23:21:57 -05:00
parent bc1a70dd3b
commit 1f4bf35e20
2 changed files with 19 additions and 2 deletions
+17
View File
@@ -261,6 +261,23 @@ register_filters(templates.env)
# Add global template variables
templates.env.globals['disable_analytics'] = os.getenv("DISABLE_ANALYTICS", "false").lower() == "true"
# Cache-busting for static files using file modification time
import hashlib
_static_hashes = {}
def static_hash(filename):
"""Generate a short hash based on file modification time for cache busting."""
if filename not in _static_hashes:
filepath = static_dir / filename
if filepath.exists():
mtime = int(filepath.stat().st_mtime)
_static_hashes[filename] = hashlib.md5(str(mtime).encode()).hexdigest()[:8]
else:
_static_hashes[filename] = "0"
return _static_hashes[filename]
templates.env.globals['static_hash'] = static_hash
# Initialize templates for route modules
init_api_templates(templates)
init_resources_templates(templates)
+2 -2
View File
@@ -28,7 +28,7 @@
<meta name="twitter:description" content="{% block twitter_description %}{% block description_plain %}Study the Authorized King James Version Bible{% endblock %}{% endblock %}"/>
<meta name="twitter:image" content="{% block twitter_image %}https://kjvstudy.org/static/og-image.png{% endblock %}"/>
<link rel="stylesheet" href="/static/tufte.css"/>
<link rel="stylesheet" href="/static/tufte.css?v={{ static_hash('tufte.css') }}"/>
<link rel="manifest" href="/static/manifest.json"/>
<!-- Analytics opt-out detection (kr=1 parameter) -->
@@ -1577,7 +1577,7 @@
</nav>
<script src="/static/base.js?v=20251203"></script>
<script src="/static/base.js?v={{ static_hash('base.js') }}"></script>
<script type="text/javascript">
{% if not disable_analytics %}
// Gauges analytics (respects kr=1 opt-out)