Skip to content

Commit bad4bf5

Browse files
authored
Rollup merge of #142497 - GuillaumeGomez:fix-random-search-failure, r=lolbinarycat
Fix random failure when JS code is executed when the whole file was not read yet Very randomly (and rarely), when I arrived on a page with `?search=something` in the URL, I got this error: ![Screenshot From 2025-06-14 11-27-46](https://github.com/user-attachments/assets/4b61b067-4e80-49c1-9a45-cff1509bf86a) Moving the `initSearch` function at the bottom to ensure everything has been loaded fixes the issue. PS: Sorry for the noise. Pushed to the wrong branch and #142496 closed. ><
2 parents 1e2ac53 + 2eccf18 commit bad4bf5

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5394,43 +5394,6 @@ function updateCrate(ev) {
53945394
search(true);
53955395
}
53965396

5397-
// @ts-expect-error
5398-
function initSearch(searchIndx) {
5399-
rawSearchIndex = searchIndx;
5400-
if (typeof window !== "undefined") {
5401-
// @ts-expect-error
5402-
docSearch = new DocSearch(rawSearchIndex, ROOT_PATH, searchState);
5403-
registerSearchEvents();
5404-
// If there's a search term in the URL, execute the search now.
5405-
if (window.searchState.getQueryStringParams().search) {
5406-
search();
5407-
}
5408-
} else if (typeof exports !== "undefined") {
5409-
// @ts-expect-error
5410-
docSearch = new DocSearch(rawSearchIndex, ROOT_PATH, searchState);
5411-
exports.docSearch = docSearch;
5412-
exports.parseQuery = DocSearch.parseQuery;
5413-
}
5414-
}
5415-
5416-
if (typeof exports !== "undefined") {
5417-
exports.initSearch = initSearch;
5418-
}
5419-
5420-
if (typeof window !== "undefined") {
5421-
// @ts-expect-error
5422-
window.initSearch = initSearch;
5423-
// @ts-expect-error
5424-
if (window.searchIndex !== undefined) {
5425-
// @ts-expect-error
5426-
initSearch(window.searchIndex);
5427-
}
5428-
} else {
5429-
// Running in Node, not a browser. Run initSearch just to produce the
5430-
// exports.
5431-
initSearch(new Map());
5432-
}
5433-
54345397
// Parts of this code are based on Lucene, which is licensed under the
54355398
// Apache/2.0 license.
54365399
// More information found here:
@@ -5909,3 +5872,44 @@ Lev1TParametricDescription.prototype.toStates3 = /*3 bits per value */ new Int32
59095872
Lev1TParametricDescription.prototype.offsetIncrs3 = /*2 bits per value */ new Int32Array([
59105873
0xa0fc0000,0x5555ba08,0x55555555,
59115874
]);
5875+
5876+
// ====================
5877+
// WARNING: Nothing should be added below this comment: we need the `initSearch` function to
5878+
// be called ONLY when the whole file has been parsed and loaded.
5879+
5880+
// @ts-expect-error
5881+
function initSearch(searchIndx) {
5882+
rawSearchIndex = searchIndx;
5883+
if (typeof window !== "undefined") {
5884+
// @ts-expect-error
5885+
docSearch = new DocSearch(rawSearchIndex, ROOT_PATH, searchState);
5886+
registerSearchEvents();
5887+
// If there's a search term in the URL, execute the search now.
5888+
if (window.searchState.getQueryStringParams().search) {
5889+
search();
5890+
}
5891+
} else if (typeof exports !== "undefined") {
5892+
// @ts-expect-error
5893+
docSearch = new DocSearch(rawSearchIndex, ROOT_PATH, searchState);
5894+
exports.docSearch = docSearch;
5895+
exports.parseQuery = DocSearch.parseQuery;
5896+
}
5897+
}
5898+
5899+
if (typeof exports !== "undefined") {
5900+
exports.initSearch = initSearch;
5901+
}
5902+
5903+
if (typeof window !== "undefined") {
5904+
// @ts-expect-error
5905+
window.initSearch = initSearch;
5906+
// @ts-expect-error
5907+
if (window.searchIndex !== undefined) {
5908+
// @ts-expect-error
5909+
initSearch(window.searchIndex);
5910+
}
5911+
} else {
5912+
// Running in Node, not a browser. Run initSearch just to produce the
5913+
// exports.
5914+
initSearch(new Map());
5915+
}

0 commit comments

Comments
 (0)