Skip to content

Commit 426cd40

Browse files
Better concurrency handling for search JS script loading
1 parent 4d1bd0d commit 426cd40

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

src/librustdoc/html/render/write_shared.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ pub(super) fn write_shared(
328328
v.push_str(
329329
r#"\
330330
]'));
331-
if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
332-
if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
331+
if (typeof exports !== 'undefined') exports.searchIndex = searchIndex;
332+
else runSearchIfFullyLoaded();
333333
"#,
334334
);
335335
Ok(v.into_bytes())

src/librustdoc/html/static/js/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ function preLoadCss(cssUrl) {
188188
function loadScript(url) {
189189
const script = document.createElement("script");
190190
script.src = url;
191+
script.setAttribute('async', '');
191192
document.head.append(script);
192193
}
193194

src/librustdoc/html/static/js/search.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -3354,9 +3354,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
33543354

33553355
if (typeof window !== "undefined") {
33563356
window.initSearch = initSearch;
3357-
if (window.searchIndex !== undefined) {
3358-
initSearch(window.searchIndex);
3359-
}
3357+
runSearchIfFullyLoaded();
33603358
} else {
33613359
// Running in Node, not a browser. Run initSearch just to produce the
33623360
// exports.

src/librustdoc/html/static/js/storage.js

+8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ function onEach(arr, func) {
6161
return false;
6262
}
6363

64+
let nbSearchScriptLoaded = 0;
65+
function runSearchIfFullyLoaded() {
66+
nbSearchScriptLoaded += 1;
67+
if (nbSearchScriptLoaded === 2) {
68+
window.initSearch(window.searchIndex);
69+
}
70+
}
71+
6472
/**
6573
* Turn an HTMLCollection or a NodeList into an Array, then run a callback
6674
* for every element. This is useful because iterating over an HTMLCollection

0 commit comments

Comments
 (0)