mirror of
https://github.com/kennethreitz/kjvstudy.org.git
synced 2026-06-05 23:00:16 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user