Skip to content

Commit d82ee5b

Browse files
authored
Merge pull request #63 from QuLogic/cache-busting
Add cache busting on CSS/JavaScript
2 parents 2803109 + be56e7f commit d82ee5b

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

docs/conf.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import datetime
2+
import subprocess
3+
from urllib.parse import urlsplit, urlunsplit
24

35
# -- Project information -----------------------------------------------------
46

@@ -31,8 +33,61 @@
3133
# This pattern also affects html_static_path and html_extra_path.
3234
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
3335

36+
# General substitutions.
37+
try:
38+
SHA = subprocess.check_output(
39+
['git', 'rev-parse', 'HEAD']).decode('utf-8').strip()
40+
# Catch the case where git is not installed locally, and use the setuptools_scm
41+
# version number instead.
42+
except (subprocess.CalledProcessError, FileNotFoundError):
43+
import hashlib
44+
import matplotlib
45+
import mpl_sphinx_theme
46+
import pydata_sphinx_theme
47+
SHA = hashlib.sha256(
48+
(f'{matplotlib.__version__} '
49+
f'{mpl_sphinx_theme.__version__} '
50+
f'{pydata_sphinx_theme.__version__}').encode('utf-8')).hexdigest()
51+
SHA = SHA[:20]
52+
53+
3454
# -- Options for HTML output -------------------------------------------------
3555

56+
def add_html_cache_busting(app, pagename, templatename, context, doctree):
57+
"""
58+
Add cache busting query on CSS and JavaScript assets.
59+
60+
This adds the Matplotlib version as a query to the link reference in the
61+
HTML, if the path is not absolute (i.e., it comes from the `_static`
62+
directory) and doesn't already have a query.
63+
"""
64+
from sphinx.builders.html import Stylesheet, JavaScript
65+
66+
css_tag = context['css_tag']
67+
js_tag = context['js_tag']
68+
69+
def css_tag_with_cache_busting(css):
70+
if isinstance(css, Stylesheet) and css.filename is not None:
71+
url = urlsplit(css.filename)
72+
if not url.netloc and not url.query:
73+
url = url._replace(query=SHA)
74+
css = Stylesheet(urlunsplit(url), priority=css.priority,
75+
**css.attributes)
76+
return css_tag(css)
77+
78+
def js_tag_with_cache_busting(js):
79+
if isinstance(js, JavaScript) and js.filename is not None:
80+
url = urlsplit(js.filename)
81+
if not url.netloc and not url.query:
82+
url = url._replace(query=SHA)
83+
js = JavaScript(urlunsplit(url), priority=js.priority,
84+
**js.attributes)
85+
return js_tag(js)
86+
87+
context['css_tag'] = css_tag_with_cache_busting
88+
context['js_tag'] = js_tag_with_cache_busting
89+
90+
3691
html_css_files = ['css/normalize.css', 'css/landing.css']
3792
html_theme = "mpl_sphinx_theme"
3893
html_favicon = "_static/favicon.ico"
@@ -54,3 +109,10 @@
54109

55110
# Prefix added to all the URLs generated in the 404 page.
56111
notfound_urls_prefix = '/'
112+
113+
114+
# -----------------------------------------------------------------------------
115+
# Sphinx setup
116+
# -----------------------------------------------------------------------------
117+
def setup(app):
118+
app.connect('html-page-context', add_html_cache_busting, priority=1000)

0 commit comments

Comments
 (0)