Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 855a6a1

Browse filesBrowse files
authored
Merge pull request #23799 from meeseeksmachine/auto-backport-of-pr-23790-on-v3.6.x
Backport PR #23790 on branch v3.6.x (DOC: Add cache busting to all static assets)
2 parents a8c423b + bde8d65 commit 855a6a1
Copy full SHA for 855a6a1

File tree

Expand file treeCollapse file tree

1 file changed

+50
-15
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+50
-15
lines changed

‎doc/conf.py

Copy file name to clipboardExpand all lines: doc/conf.py
+50-15Lines 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', 'dev', '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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.