From 1f4bf35e20e97fcae3ac8a55f6fe43aeeccaa3dc Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 3 Dec 2025 23:21:57 -0500 Subject: [PATCH] Add automatic cache-busting for static files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- kjvstudy_org/server.py | 17 +++++++++++++++++ kjvstudy_org/templates/base.html | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/kjvstudy_org/server.py b/kjvstudy_org/server.py index 6918455..71fb150 100644 --- a/kjvstudy_org/server.py +++ b/kjvstudy_org/server.py @@ -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) diff --git a/kjvstudy_org/templates/base.html b/kjvstudy_org/templates/base.html index 0c5c107..36a2f9a 100644 --- a/kjvstudy_org/templates/base.html +++ b/kjvstudy_org/templates/base.html @@ -28,7 +28,7 @@ - + @@ -1577,7 +1577,7 @@ - +