Skip to content

Better concurrency handling for search JS script loading #118984

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

Closed
Show file tree
Hide file tree
Changes from all 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 src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ pub(super) fn write_shared(
v.push_str(
r#"\
]'));
if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
if (typeof exports !== 'undefined') exports.searchIndex = searchIndex;
else runSearchIfFullyLoaded();
"#,
);
Ok(v.into_bytes())
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ function preLoadCss(cssUrl) {
function loadScript(url) {
const script = document.createElement("script");
script.src = url;
script.setAttribute('async', '');
document.head.append(script);
}

Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3354,9 +3354,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\

if (typeof window !== "undefined") {
window.initSearch = initSearch;
if (window.searchIndex !== undefined) {
initSearch(window.searchIndex);
}
runSearchIfFullyLoaded();
} else {
// Running in Node, not a browser. Run initSearch just to produce the
// exports.
Expand Down
9 changes: 9 additions & 0 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ function onEach(arr, func) {
return false;
}

let nbSearchScriptLoaded = 0;
// eslint-disable-next-line no-unused-vars
function runSearchIfFullyLoaded() {
nbSearchScriptLoaded += 1;
if (nbSearchScriptLoaded === 2) {
window.initSearch(window.searchIndex);
}
}

/**
* Turn an HTMLCollection or a NodeList into an Array, then run a callback
* for every element. This is useful because iterating over an HTMLCollection
Expand Down