Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e87416b

Browse files
committed
DOC: Add cache busting to all static assets
We have seen both in `/stable` and `/3.6.0`, some styling is broken because old CSS is cached. CSS might change from updating sphinx-gallery, mpl-sphinx-theme, pydata-sphinx-theme, etc. Adding a versioned query breaks the cache. It's a bit over-eager to base it on Matplotlib version and not the file contents (since those dependencies may not have updated), but this should work well enough.
1 parent 81ca82c commit e87416b

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

doc/conf.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import shutil
1717
import subprocess
1818
import sys
19+
from urllib.parse import urlsplit, urlunsplit
1920
import warnings
2021

2122
import matplotlib
@@ -248,10 +249,6 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
248249
except (subprocess.CalledProcessError, FileNotFoundError):
249250
SHA = matplotlib.__version__
250251

251-
html_context = {
252-
"sha": SHA,
253-
}
254-
255252
project = 'Matplotlib'
256253
copyright = (
257254
'2002–2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom '
@@ -312,16 +309,50 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
312309

313310
github_project_url = "https://github.com/matplotlib/matplotlib/"
314311

312+
315313
# Options for HTML output
316314
# -----------------------
317315

316+
def add_html_cache_busting(app, pagename, templatename, context, doctree):
317+
"""
318+
Add cache busting query on CSS and JavaScript assets.
319+
320+
This adds the Matplotlib version as a query to the link reference in the
321+
HTML, if the path is not absolute (i.e., it comes from the `_static`
322+
directory) and doesn't already have a query.
323+
"""
324+
from sphinx.builders.html import Stylesheet, JavaScript
325+
326+
css_tag = context['css_tag']
327+
js_tag = context['js_tag']
328+
329+
def css_tag_with_cache_busting(css):
330+
if isinstance(css, Stylesheet) and css.filename is not None:
331+
url = urlsplit(css.filename)
332+
if not url.netloc and not url.query:
333+
url = url._replace(query=SHA)
334+
css = Stylesheet(urlunsplit(url), priority=css.priority,
335+
**css.attributes)
336+
return css_tag(css)
337+
338+
def js_tag_with_cache_busting(js):
339+
if isinstance(js, JavaScript) and js.filename is not None:
340+
url = urlsplit(js.filename)
341+
if not url.netloc and not url.query:
342+
url = url._replace(query=SHA)
343+
js = JavaScript(urlunsplit(url), priority=js.priority,
344+
**js.attributes)
345+
return js_tag(js)
346+
347+
context['css_tag'] = css_tag_with_cache_busting
348+
context['js_tag'] = js_tag_with_cache_busting
349+
350+
318351
# The style sheet to use for HTML and HTML Help pages. A file of that name
319352
# must exist either in Sphinx' static/ path, or in one of the custom paths
320353
# given in html_static_path.
321-
# html_style = 'matplotlib.css'
322-
# html_style = f"mpl.css?{SHA}"
323354
html_css_files = [
324-
f"mpl.css?{SHA}",
355+
"mpl.css",
325356
]
326357

327358
html_theme = "mpl_sphinx_theme"
@@ -574,14 +605,6 @@ def matplotlib_reduced_latex_scraper(block, block_vars, gallery_conf,
574605
# https://github.com/sphinx-doc/sphinx/issues/3176
575606
# graphviz_output_format = 'svg'
576607

577-
578-
def setup(app):
579-
if any(st in version for st in ('post', 'alpha', 'beta')):
580-
bld_type = 'dev'
581-
else:
582-
bld_type = 'rel'
583-
app.add_config_value('releaselevel', bld_type, 'env')
584-
585608
# -----------------------------------------------------------------------------
586609
# Source code links
587610
# -----------------------------------------------------------------------------
@@ -649,3 +672,15 @@ def linkcode_resolve(domain, info):
649672
f"/{tag}/lib/{fn}{linespec}")
650673
else:
651674
extensions.append('sphinx.ext.viewcode')
675+
676+
677+
# -----------------------------------------------------------------------------
678+
# Sphinx setup
679+
# -----------------------------------------------------------------------------
680+
def setup(app):
681+
if any(st in version for st in ('post', 'alpha', 'beta')):
682+
bld_type = 'dev'
683+
else:
684+
bld_type = 'rel'
685+
app.add_config_value('releaselevel', bld_type, 'env')
686+
app.connect('html-page-context', add_html_cache_busting, priority=1000)

0 commit comments

Comments
 (0)