Skip to content

Embedded JS: Conditionally inject jQuery #9861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"tests"
],
"dependencies": {
"jquery": "2.0.3",
"jquery": "3.6.3",
"underscore": "~1.7.0",
"readthedocs-client": "https://github.com/agjohnson/readthedocs-client-js.git",
"sphinx-rtd-theme": "https://github.com/readthedocs/sphinx_rtd_theme.git#0.3.1",
Expand All @@ -25,6 +25,6 @@
"xss": "~0.3.1"
},
"resolutions": {
"jquery": "2.0.3"
"jquery": "3.6.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function get() {
var defaults = {
api_host: 'https://readthedocs.org',
ad_free: false,
proxied_static_path: '/_/static/',
};

Object.assign(config, defaults, window.READTHEDOCS_DATA);
Expand Down
39 changes: 35 additions & 4 deletions readthedocs/core/static-src/core/js/readthedocs-doc-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,43 @@ const footer = require('./doc-embed/footer.js');
const sphinx = require('./doc-embed/sphinx');
const search = require('./doc-embed/search');
const { domReady } = require('./doc-embed/utils');
const rtddata = require('./doc-embed/rtd-data');

/*
* Inject JQuery if isn't present already.
*
* Parts of this script rely on JQuery (mainly the flyout menu injection),
* since Sphinx no longer includes it, and other tools may not include it,
* we must inject it if isn't found before executing our script.
*/
function injectJQuery(init) {
if (window.jQuery) {
init()
return
}
console.debug("JQuery not found. Injecting.");
let rtd = rtddata.get();
let script = document.createElement("script");
script.type = "text/javascript";
script.src = rtd.proxied_static_path + "vendor/jquery.js";
script.onload = function () {
// Set jQuery to its expected globals.
window.$ = require("jquery");
window.jQuery = window.$;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure if this is the way of doing it... but just adding the script from vendor/jquery.js doesn't add the global variables :/ /cc @agjohnson

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this is normal. Relying on every module to be a global is an antipattern that doesn't work well with various forms of module importing -- commonJS, AMD, ES6 module imports, etc. Modern JS would tend to use jQuery as a module instead.

Your change is about what I'd do.

init();
};
document.head.appendChild(script);
}


(function () {
domReady(function () {
footer.init();
sphinx.init();
search.init();
sponsorship.init();
// Block on jQuery loading before we run any of our code.
injectJQuery(function() {
footer.init();
sphinx.init();
search.init();
sponsorship.init();
})
});
}());
2 changes: 1 addition & 1 deletion readthedocs/core/static/core/js/readthedocs-doc-embed.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion readthedocs/doc_builder/backends/mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
'global_analytics_code': (
None if self.project.analytics_disabled else settings.GLOBAL_ANALYTICS_CODE
),
'user_analytics_code': analytics_code,
"user_analytics_code": analytics_code,
"proxied_static_path": self.project.proxied_static_path,
"proxied_api_host": self.project.proxied_api_host,
Comment on lines +282 to +283
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were missing including these for mkdocs, but it's only relevant when using a path different from _/, so

}

data_ctx = {
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/static/vendor/jquery-migrate-standalone.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading