Skip to content

Commit 27dcebe

Browse files
committed
Improve loading of crates.js and sidebar-items.js
Now that the "All Crates" dropdown is only rendered on the search results page, there is no need to load crates.js on most pages. Load it only on crate pages. Also, add the `defer` attribute so it does not block page rendering. For sidebar-items.js, move the script tag to `<head>`. Since it already has the defer attribute it won't block loading. The defer attribute does preserve ordering between scripts, so instead of the callback on load, it can set a global variable on load, which is slightly simpler. Also, since it is required to finish rendering the page, beginning its load earlier is better. Remove generation and handling of sidebar-vars. Everything there can be computed with information available in JS via other means. Remove the "other" wrapper in the sidebar. It was unnecessary. Remove excess script fields
1 parent bb8c2f4 commit 27dcebe

File tree

11 files changed

+91
-141
lines changed

11 files changed

+91
-141
lines changed

src/librustdoc/html/layout.rs

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ pub(crate) struct Page<'a> {
3131
pub(crate) description: &'a str,
3232
pub(crate) keywords: &'a str,
3333
pub(crate) resource_suffix: &'a str,
34-
pub(crate) extra_scripts: &'a [&'a str],
35-
pub(crate) static_extra_scripts: &'a [&'a str],
3634
}
3735

3836
impl<'a> Page<'a> {

src/librustdoc/html/render/context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ impl<'tcx> Context<'tcx> {
211211
description: &desc,
212212
keywords: &keywords,
213213
resource_suffix: &clone_shared.resource_suffix,
214-
extra_scripts: &[],
215-
static_extra_scripts: &[],
216214
};
217215
let mut page_buffer = Buffer::html();
218216
print_item(self, it, &mut page_buffer, &page);
@@ -568,8 +566,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
568566
description: "List of all items in this crate",
569567
keywords: BASIC_KEYWORDS,
570568
resource_suffix: &shared.resource_suffix,
571-
extra_scripts: &[],
572-
static_extra_scripts: &[],
573569
};
574570
let sidebar = if shared.cache.crate_version.is_some() {
575571
format!("<h2 class=\"location\">Crate {}</h2>", crate_name)
@@ -693,7 +689,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
693689
else { unreachable!() };
694690
let items = self.build_sidebar_items(module);
695691
let js_dst = self.dst.join(&format!("sidebar-items{}.js", self.shared.resource_suffix));
696-
let v = format!("initSidebarItems({});", serde_json::to_string(&items).unwrap());
692+
let v = format!("window.SIDEBAR_ITEMS = {};", serde_json::to_string(&items).unwrap());
697693
self.shared.fs.write(js_dst, v)?;
698694
}
699695
Ok(())

src/librustdoc/html/render/mod.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use rustc_middle::middle::stability;
5656
use rustc_middle::ty;
5757
use rustc_middle::ty::TyCtxt;
5858
use rustc_span::{
59-
symbol::{kw, sym, Symbol},
59+
symbol::{sym, Symbol},
6060
BytePos, FileName, RealFileName,
6161
};
6262
use serde::ser::SerializeSeq;
@@ -1738,8 +1738,6 @@ pub(crate) fn render_impl_summary(
17381738
}
17391739

17401740
fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
1741-
let parentlen = cx.current.len() - if it.is_mod() { 1 } else { 0 };
1742-
17431741
if it.is_struct()
17441742
|| it.is_trait()
17451743
|| it.is_primitive()
@@ -1800,21 +1798,6 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
18001798
write!(buffer, "<h2 class=\"location\"><a href=\"index.html\">In {}</a></h2>", path);
18011799
}
18021800

1803-
// Sidebar refers to the enclosing module, not this module.
1804-
let relpath = if it.is_mod() && parentlen != 0 { "./" } else { "" };
1805-
write!(
1806-
buffer,
1807-
"<div id=\"sidebar-vars\" data-name=\"{name}\" data-ty=\"{ty}\" data-relpath=\"{path}\">\
1808-
</div>",
1809-
name = it.name.unwrap_or(kw::Empty),
1810-
ty = it.type_(),
1811-
path = relpath
1812-
);
1813-
write!(
1814-
buffer,
1815-
"<script defer src=\"{}sidebar-items{}.js\"></script>",
1816-
relpath, cx.shared.resource_suffix
1817-
);
18181801
// Closes sidebar-elems div.
18191802
buffer.write_str("</div>");
18201803
}

src/librustdoc/html/render/write_shared.rs

-2
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,6 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
475475
description: "List of crates",
476476
keywords: BASIC_KEYWORDS,
477477
resource_suffix: &shared.resource_suffix,
478-
extra_scripts: &[],
479-
static_extra_scripts: &[],
480478
};
481479

482480
let content = format!(

src/librustdoc/html/sources.rs

-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ impl SourceCollector<'_, '_> {
203203
description: &desc,
204204
keywords: BASIC_KEYWORDS,
205205
resource_suffix: &shared.resource_suffix,
206-
extra_scripts: &[&format!("source-files{}", shared.resource_suffix)],
207-
static_extra_scripts: &[&format!("source-script{}", shared.resource_suffix)],
208206
};
209207
let v = layout::render(
210208
&shared.layout,

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

+61-69
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,18 @@ function showMain() {
6666
(function() {
6767
window.rootPath = getVar("root-path");
6868
window.currentCrate = getVar("current-crate");
69-
window.searchJS = resourcePath("search", ".js");
70-
window.searchIndexJS = resourcePath("search-index", ".js");
71-
window.settingsJS = resourcePath("settings", ".js");
72-
const sidebarVars = document.getElementById("sidebar-vars");
73-
if (sidebarVars) {
74-
window.sidebarCurrent = {
75-
name: sidebarVars.attributes["data-name"].value,
76-
ty: sidebarVars.attributes["data-ty"].value,
77-
relpath: sidebarVars.attributes["data-relpath"].value,
78-
};
79-
// FIXME: It would be nicer to generate this text content directly in HTML,
80-
// but with the current code it's hard to get the right information in the right place.
81-
const mobileLocationTitle = document.querySelector(".mobile-topbar h2.location");
82-
const locationTitle = document.querySelector(".sidebar h2.location");
83-
if (mobileLocationTitle && locationTitle) {
84-
mobileLocationTitle.innerHTML = locationTitle.innerHTML;
85-
}
86-
}
8769
}());
8870

71+
function setMobileTopbar() {
72+
// FIXME: It would be nicer to generate this text content directly in HTML,
73+
// but with the current code it's hard to get the right information in the right place.
74+
const mobileLocationTitle = document.querySelector(".mobile-topbar h2.location");
75+
const locationTitle = document.querySelector(".sidebar h2.location");
76+
if (mobileLocationTitle && locationTitle) {
77+
mobileLocationTitle.innerHTML = locationTitle.innerHTML;
78+
}
79+
}
80+
8981
// Gets the human-readable string for the virtual-key code of the
9082
// given KeyboardEvent, ev.
9183
//
@@ -227,7 +219,7 @@ function loadCss(cssFileName) {
227219
// Sending request for the CSS and the JS files at the same time so it will
228220
// hopefully be loaded when the JS will generate the settings content.
229221
loadCss("settings");
230-
loadScript(window.settingsJS);
222+
loadScript(resourcePath("settings", ".js"));
231223
};
232224

233225
window.searchState = {
@@ -304,8 +296,8 @@ function loadCss(cssFileName) {
304296
function loadSearch() {
305297
if (!searchLoaded) {
306298
searchLoaded = true;
307-
loadScript(window.searchJS);
308-
loadScript(window.searchIndexJS);
299+
loadScript(resourcePath("search", ".js"));
300+
loadScript(resourcePath("search-index", ".js"));
309301
}
310302
}
311303

@@ -485,40 +477,11 @@ function loadCss(cssFileName) {
485477
document.addEventListener("keypress", handleShortcut);
486478
document.addEventListener("keydown", handleShortcut);
487479

488-
// delayed sidebar rendering.
489-
window.initSidebarItems = items => {
490-
const sidebar = document.getElementsByClassName("sidebar-elems")[0];
491-
let others;
492-
const current = window.sidebarCurrent;
493-
494-
function addSidebarCrates(crates) {
495-
if (!hasClass(document.body, "crate")) {
496-
// We only want to list crates on the crate page.
497-
return;
498-
}
499-
// Draw a convenient sidebar of known crates if we have a listing
500-
const div = document.createElement("div");
501-
div.className = "block crate";
502-
div.innerHTML = "<h3>Crates</h3>";
503-
const ul = document.createElement("ul");
504-
div.appendChild(ul);
505-
506-
for (const crate of crates) {
507-
let klass = "crate";
508-
if (window.rootPath !== "./" && crate === window.currentCrate) {
509-
klass += " current";
510-
}
511-
const link = document.createElement("a");
512-
link.href = window.rootPath + crate + "/index.html";
513-
link.className = klass;
514-
link.textContent = crate;
515-
516-
const li = document.createElement("li");
517-
li.appendChild(link);
518-
ul.appendChild(li);
519-
}
520-
others.appendChild(div);
480+
function addSidebarItems() {
481+
if (!window.SIDEBAR_ITEMS) {
482+
return;
521483
}
484+
const sidebar = document.getElementsByClassName("sidebar-elems")[0];
522485

523486
/**
524487
* Append to the sidebar a "block" of links - a heading along with a list (`<ul>`) of items.
@@ -529,7 +492,7 @@ function loadCss(cssFileName) {
529492
* "Modules", or "Macros".
530493
*/
531494
function block(shortty, id, longty) {
532-
const filtered = items[shortty];
495+
const filtered = window.SIDEBAR_ITEMS[shortty];
533496
if (!filtered) {
534497
return;
535498
}
@@ -546,17 +509,18 @@ function loadCss(cssFileName) {
546509
const desc = item[1]; // can be null
547510

548511
let klass = shortty;
549-
if (name === current.name && shortty === current.ty) {
550-
klass += " current";
551-
}
552512
let path;
553513
if (shortty === "mod") {
554514
path = name + "/index.html";
555515
} else {
556516
path = shortty + "." + name + ".html";
557517
}
518+
const current_page = document.location.href.split("/").pop();
519+
if (path === current_page) {
520+
klass += " current";
521+
}
558522
const link = document.createElement("a");
559-
link.href = current.relpath + path;
523+
link.href = path;
560524
link.title = desc;
561525
link.className = klass;
562526
link.textContent = name;
@@ -565,14 +529,10 @@ function loadCss(cssFileName) {
565529
ul.appendChild(li);
566530
}
567531
div.appendChild(ul);
568-
others.appendChild(div);
532+
sidebar.appendChild(div);
569533
}
570534

571535
if (sidebar) {
572-
others = document.createElement("div");
573-
others.className = "others";
574-
sidebar.appendChild(others);
575-
576536
const isModule = hasClass(document.body, "mod");
577537
if (!isModule) {
578538
block("primitive", "primitives", "Primitive Types");
@@ -590,12 +550,8 @@ function loadCss(cssFileName) {
590550
block("keyword", "keywords", "Keywords");
591551
block("traitalias", "trait-aliases", "Trait Aliases");
592552
}
593-
594-
// `crates{version}.js` should always be loaded before this script, so we can use
595-
// it safely.
596-
addSidebarCrates(window.ALL_CRATES);
597553
}
598-
};
554+
}
599555

600556
window.register_implementors = imp => {
601557
const implementors = document.getElementById("implementors-list");
@@ -680,6 +636,39 @@ function loadCss(cssFileName) {
680636
window.register_implementors(window.pending_implementors);
681637
}
682638

639+
function addSidebarCrates() {
640+
if (!window.ALL_CRATES) {
641+
return;
642+
}
643+
const sidebarElems = document.getElementsByClassName("sidebar-elems")[0];
644+
if (!sidebarElems) {
645+
return;
646+
}
647+
// Draw a convenient sidebar of known crates if we have a listing
648+
const div = document.createElement("div");
649+
div.className = "block crate";
650+
div.innerHTML = "<h3>Crates</h3>";
651+
const ul = document.createElement("ul");
652+
div.appendChild(ul);
653+
654+
for (const crate of window.ALL_CRATES) {
655+
let klass = "crate";
656+
if (window.rootPath !== "./" && crate === window.currentCrate) {
657+
klass += " current";
658+
}
659+
const link = document.createElement("a");
660+
link.href = window.rootPath + crate + "/index.html";
661+
link.className = klass;
662+
link.textContent = crate;
663+
664+
const li = document.createElement("li");
665+
li.appendChild(link);
666+
ul.appendChild(li);
667+
}
668+
sidebarElems.appendChild(div);
669+
}
670+
671+
683672
function labelForToggleButton(sectionIsCollapsed) {
684673
if (sectionIsCollapsed) {
685674
// button will expand the section
@@ -924,6 +913,9 @@ function loadCss(cssFileName) {
924913
buildHelperPopup = () => {};
925914
};
926915

916+
setMobileTopbar();
917+
addSidebarItems();
918+
addSidebarCrates();
927919
onHashChange(null);
928920
window.addEventListener("hashchange", onHashChange);
929921
searchState.setup();

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1719,10 +1719,11 @@ function initSearch(rawSearchIndex) {
17191719
}
17201720

17211721
let crates = "";
1722-
if (window.ALL_CRATES.length > 1) {
1722+
const crates_list = Object.keys(rawSearchIndex);
1723+
if (crates_list.length > 1) {
17231724
crates = " in <select id=\"crate-search\"><option value=\"All crates\">" +
17241725
"All crates</option>";
1725-
for (const c of window.ALL_CRATES) {
1726+
for (const c of crates_list) {
17261727
crates += `<option value="${c}" ${c === filterCrates && "selected"}>${c}</option>`;
17271728
}
17281729
crates += "</select>";

0 commit comments

Comments
 (0)