Add cache-busting version parameter to custom.js URL.

Prevents stale JS from running against new templates after deploys.
Uses the git commit hash as a query parameter on the script URL.
This commit is contained in:
russell@unturf.com 2026-02-08 04:48:29 -05:00
parent ac5970044e
commit e6af04bfc6
2 changed files with 32 additions and 1 deletions

View file

@ -29,12 +29,39 @@ import re
from pkg_resources import iter_entry_points
import logging
import os
import subprocess
log = logging.getLogger(__name__)
JINJA2_EXTENSION = ".j2"
def _get_static_version():
"""Return a short git hash for cache-busting static assets."""
_this_dir = os.path.dirname(os.path.abspath(__file__))
for candidate in [
os.path.join(_this_dir, "..", "commit-hash.txt"),
os.path.join(os.sys.prefix, "commit-hash.txt"),
]:
try:
with open(candidate) as f:
return f.read().strip()[:7]
except (IOError, OSError):
pass
try:
return subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=_this_dir,
stderr=subprocess.DEVNULL,
).decode().strip()
except Exception:
return "0"
STATIC_VERSION = _get_static_version()
def get_int_or_bool_or_none_or_str(value):
"""
Given a string value pulled from a configuration file,
@ -415,6 +442,9 @@ def main(global_config, **settings):
return request.namespace.avatar_size
return request.app.get("avatar.size", 30)
def add_static_version(request):
return STATIC_VERSION
def add_stand_alone_mode(request):
if request.app.get("stand_alone_mode", "disabled") == "enabled":
return True
@ -562,6 +592,7 @@ def main(global_config, **settings):
config.add_request_method(add_marketing_domain, "marketing_domain", reify=True)
config.add_request_method(add_faq_home, "faq_home", reify=True)
config.add_request_method(add_saas_home, "saas_home", reify=True)
config.add_request_method(add_static_version, "static_version", reify=True)
config.add_request_method(add_stand_alone_mode, "stand_alone_mode", reify=True)
config.add_request_method(add_avatar_size, "avatar_size", reify=True)
config.add_request_method(add_theme, "theme", reify=True)

View file

@ -6,7 +6,7 @@
<script>var csrf_token = "{{ request.session.get_csrf_token() | default("none", true) }}";</script>
<script src="/static/js/iframe-resizer/iframeResizer.contentWindow.min.js"></script>
<script src="/static/js/custom.js"></script>
<script src="/static/js/custom.js?v={{ request.static_version }}"></script>
{%- if request.mode == "basic" and request.namespace and request.namespace.name == request.domain and request.namespace.google_analytics_id %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ request.namespace.google_analytics_id }}"></script>
<script>