Skip to content

Commit b5ce9c4

Browse files
authored
Rollup merge of #84150 - jsha:defer-search-js, r=GuillaumeGomez
rustdoc: move some search code into search.js This reduces main.s from 3094 lines to 1587. Also it saves some bytes of download in the case where search isn't used. There were a fair number of variables that needed to be accessible in both main.js and search.js, but I didn't want to put too many symbols in the global namespace, so I consolidated much of the search-related state and functions into a new object `window.searchState`. Demo at https://hoffman-andrews.com/rust/move-search/std/?search=foo
2 parents 5dae27a + 6f1f3eb commit b5ce9c4

File tree

6 files changed

+1682
-1693
lines changed

6 files changed

+1682
-1693
lines changed

src/librustdoc/html/layout.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ crate fn render<T: Print, S: Print>(
113113
<section class=\"footer\"></section>\
114114
{after_content}\
115115
<div id=\"rustdoc-vars\" data-root-path=\"{root_path}\" data-current-crate=\"{krate}\" \
116-
data-search-js=\"{root_path}search-index{suffix}.js\"></div>
116+
data-search-index-js=\"{root_path}search-index{suffix}.js\" \
117+
data-search-js=\"{static_root_path}search{suffix}.js\"></div>
117118
<script src=\"{static_root_path}main{suffix}.js\"></script>\
118119
{extra_scripts}\
119120
</body>\

src/librustdoc/html/render/write_shared.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ pub(super) fn write_shared(
222222
&format!(" = {}", serde_json::to_string(&themes).unwrap()),
223223
),
224224
)?;
225+
write_minify("search.js", static_files::SEARCH_JS)?;
225226
write_minify("settings.js", static_files::SETTINGS_JS)?;
226227
if cx.shared.include_sources {
227228
write_minify("source-script.js", static_files::sidebar::SOURCE_SCRIPT)?;
@@ -409,7 +410,7 @@ pub(super) fn write_shared(
409410
write_crate("search-index.js", &|| {
410411
let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
411412
v.push_str(&all_indexes.join(",\\\n"));
412-
v.push_str("\\\n}');\ninitSearch(searchIndex);");
413+
v.push_str("\\\n}');\nif (window.initSearch) {window.initSearch(searchIndex)};");
413414
Ok(v.into_bytes())
414415
})?;
415416

0 commit comments

Comments
 (0)