Skip to content
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

Include jquery in the theme #1299

Closed
wants to merge 6 commits into from
Closed

Include jquery in the theme #1299

wants to merge 6 commits into from

Conversation

stsewd
Copy link
Member

@stsewd stsewd commented Jun 20, 2022

Sphinx is removing jquery on the 6.x version.

I have avoided using jquery before the main call to the theme,
and on the main call from the theme load jquery if isn't present.

I have removed the require statement, since the jquery object is global,
and could not be defined at the start of the script.

Related: #1253

Sphinx is removing jquery on the 6.x version.
Our theme depends a lot on jquery.
@stsewd stsewd changed the title Avoid using JQuery inside the templates Load jquery if isn't found Jun 21, 2022
@stsewd stsewd marked this pull request as ready for review June 21, 2022 22:47
@stsewd stsewd requested a review from agjohnson June 21, 2022 22:53
src/theme.js Outdated
console.log("JQuery not found. Injecting.");
var script = document.createElement("script");
script.type = 'text/javascript';
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
Copy link
Collaborator

Choose a reason for hiding this comment

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

I probably wouldn't refer to a Google hosted file here, and instead would bundle a jQuery release with the theme.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, we just bundle jquery with the theme instead of conditionally injecting it?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ericholscher was +1 on injecting it conditionally.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, we just bundle jquery with the theme instead of conditionally injecting it?

Yup. The concerns here are privacy, security, and creating a dependency downstream on a third party service. Package maintainers likely need the file locally as well.

The historical benefits of hosting JS directly from a CDN are no longer all relevant, and probably don't outweigh the privacy/security negatives this brings:

https://httptoolkit.tech/blog/public-cdn-risks/

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to bundle jquery with the theme

src/theme.js Outdated Show resolved Hide resolved
src/theme.js Outdated
*
* @param {function} fn - Function to be executed after jquery has been loaded.
*/
function injectJQuery(fn) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Webpack does this natively. We use this import method in other places for similar conditional imports.

https://webpack.js.org/api/module-methods/#import-1

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't find any other places where the import method is used in our theme.

$(function () {
$("[data-toggle='rst-debug-badge']").on("click", function () {
// Add debug actions to flyout menu.
document.addEventListener("DOMContentLoaded", function () {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we create a domReady() helper function as we are doing in the community repository?

Copy link
Member Author

Choose a reason for hiding this comment

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

seems simpler to just use the event listener? If you mean to try to load jquery here as well, there is no need, since jquery isn't used till you press the "toggle" button, at that point jquery is already included/loaded.

Copy link
Member

Choose a reason for hiding this comment

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

I'm just saying that instead of repeating over and over the document.addEventListener("DOMContentLoaded", function () { part we could create a helper function and reuse on the whole file.

src/theme.js Outdated
console.log("JQuery not found. Injecting.");
var script = document.createElement("script");
script.type = 'text/javascript';
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
Copy link
Member

Choose a reason for hiding this comment

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

@humitos humitos requested a review from agjohnson June 27, 2022 11:29
@stsewd stsewd changed the title Load jquery if isn't found Include jquery in the theme Jul 5, 2022
@benjaoming
Copy link
Contributor

benjaoming commented Aug 19, 2022

This PR fixes an important issue, but I'm not including it for the 1.1 release, since there are alternative plans to depend on a new project sphinx-jquery and ship jquery in this extension. These plans should be released in the 2.0 series soon enough that it's before Sphinx 6 is released!

@stsewd stsewd closed this Aug 24, 2022
@stsewd stsewd deleted the avoid-js branch August 24, 2022 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants