Skip to content

Commit 7ed657e

Browse files
authored
Embedded JS: Conditionally inject jQuery (#9861)
For injecting the flyout menu we are relying on a script from our theme, that script depends on jQuery, so it needs to be present for the flyout menu to work. https://github.com/readthedocs/readthedocs.org/blob/67c44a1f1b27dcc66bd14520d019459fcf86354d/readthedocs/core/static-src/core/js/doc-embed/sphinx.js#L43-L59 We have migrated most of our code to vanilla JS, but this piece isn't simple to migrate.
1 parent 26765fc commit 7ed657e

14 files changed

+52
-17
lines changed

bower.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"tests"
1515
],
1616
"dependencies": {
17-
"jquery": "2.0.3",
17+
"jquery": "3.6.3",
1818
"underscore": "~1.7.0",
1919
"readthedocs-client": "https://github.com/agjohnson/readthedocs-client-js.git",
2020
"sphinx-rtd-theme": "https://github.com/readthedocs/sphinx_rtd_theme.git#0.3.1",
@@ -25,6 +25,6 @@
2525
"xss": "~0.3.1"
2626
},
2727
"resolutions": {
28-
"jquery": "2.0.3"
28+
"jquery": "3.6.3"
2929
}
3030
}

readthedocs/core/static-src/core/js/doc-embed/rtd-data.js

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function get() {
5858
var defaults = {
5959
api_host: 'https://readthedocs.org',
6060
ad_free: false,
61+
proxied_static_path: '/_/static/',
6162
};
6263

6364
Object.assign(config, defaults, window.READTHEDOCS_DATA);

readthedocs/core/static-src/core/js/readthedocs-doc-embed.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,44 @@ const footer = require('./doc-embed/footer.js');
55
const sphinx = require('./doc-embed/sphinx');
66
const search = require('./doc-embed/search');
77
const { domReady } = require('./doc-embed/utils');
8+
const rtddata = require('./doc-embed/rtd-data');
9+
10+
/*
11+
* Inject JQuery if isn't present already.
12+
*
13+
* Parts of this script rely on JQuery (mainly the flyout menu injection),
14+
* since Sphinx no longer includes it, and other tools may not include it,
15+
* we must inject it if isn't found before executing our script.
16+
*/
17+
function injectJQuery(init) {
18+
if (window.jQuery) {
19+
init();
20+
return;
21+
}
22+
console.debug("JQuery not found. Injecting.");
23+
let rtd = rtddata.get();
24+
let script = document.createElement("script");
25+
script.type = "text/javascript";
26+
script.src = rtd.proxied_static_path + "vendor/jquery.js";
27+
script.onload = function () {
28+
// Set jQuery to its expected globals.
29+
/* eslint-disable global-require */
30+
window.$ = require("jquery");
31+
window.jQuery = window.$;
32+
init();
33+
};
34+
document.head.appendChild(script);
35+
}
36+
837

938
(function () {
1039
domReady(function () {
11-
footer.init();
12-
sphinx.init();
13-
search.init();
14-
sponsorship.init();
40+
// Block on jQuery loading before we run any of our code.
41+
injectJQuery(function () {
42+
footer.init();
43+
sphinx.init();
44+
search.init();
45+
sponsorship.init();
46+
});
1547
});
1648
}());

readthedocs/core/static/core/js/readthedocs-doc-embed.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readthedocs/doc_builder/backends/mkdocs.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
278278
'global_analytics_code': (
279279
None if self.project.analytics_disabled else settings.GLOBAL_ANALYTICS_CODE
280280
),
281-
'user_analytics_code': analytics_code,
281+
"user_analytics_code": analytics_code,
282+
"proxied_static_path": self.project.proxied_static_path,
283+
"proxied_api_host": self.project.proxied_api_host,
282284
}
283285

284286
data_ctx = {

readthedocs/static/vendor/jquery-migrate-standalone.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)