Skip to content

Commit be56e7f

Browse files
committed
Add cache busting on CSS/JavaScript
This is a copy of matplotlib/matplotlib#23790.
1 parent 80dd51d commit be56e7f

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

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

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

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

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

0 commit comments

Comments
 (0)