Skip to content

Commit 1cead06

Browse files
Rollup merge of rust-lang#98310 - jsha:defer-source-sidebar, r=GuillaumeGomez
rustdoc: optimize loading of source sidebar The source sidebar has a setting to remember whether it should be open or closed. Previously, this setting was handled in source-script.js, which is loaded with `defer`, meaning it is often run after the document is rendered. Since CSS renders the source sidebar as closed by default, changing this after the initial render results in a relayout. Instead, handle the setting in storage.js, which is the first script to load and is the only script that blocks render. This avoids a relayout and means navigating between files with the sidebar open is faster. Demo: https://rustdoc.crud.net/jsha/defer-source-sidebar/src/alloc/ffi/c_str.rs.html r? ```@GuillaumeGomez```
2 parents 2af00b6 + b37a05b commit 1cead06

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

src/librustdoc/html/static/css/rustdoc.css

+23-12
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,20 @@ nav.sub {
387387
overflow-y: hidden;
388388
}
389389

390+
.rustdoc.source .sidebar .sidebar-logo {
391+
display: none;
392+
}
393+
390394
.source .sidebar > *:not(#sidebar-toggle) {
391395
opacity: 0;
392396
visibility: hidden;
393397
}
394398

395-
.source .sidebar.expanded {
399+
.source-sidebar-expanded .source .sidebar {
396400
overflow-y: auto;
397401
}
398402

399-
.source .sidebar.expanded > *:not(#sidebar-toggle) {
403+
.source-sidebar-expanded .source .sidebar > *:not(#sidebar-toggle) {
400404
opacity: 1;
401405
visibility: visible;
402406
}
@@ -1682,11 +1686,11 @@ details.rustdoc-toggle[open] > summary.hideme::after {
16821686

16831687
/* When we expand the sidebar on the source code page, we hide the logo on the left of the
16841688
search bar to have more space. */
1685-
.sidebar.expanded + main .width-limiter .sub-logo-container.rust-logo {
1689+
.source-sidebar-expanded .source .sidebar + main .width-limiter .sub-logo-container.rust-logo {
16861690
display: none;
16871691
}
16881692

1689-
.source .sidebar.expanded {
1693+
.source-sidebar-expanded .source .sidebar {
16901694
width: 300px;
16911695
}
16921696
}
@@ -1766,7 +1770,7 @@ details.rustdoc-toggle[open] > summary.hideme::after {
17661770
}
17671771

17681772
.sidebar.shown,
1769-
.sidebar.expanded,
1773+
.source-sidebar-expanded .source .sidebar,
17701774
.sidebar:focus-within {
17711775
left: 0;
17721776
}
@@ -1889,11 +1893,7 @@ details.rustdoc-toggle[open] > summary.hideme::after {
18891893
left: -11px;
18901894
}
18911895

1892-
.sidebar.expanded #sidebar-toggle {
1893-
font-size: 1.5rem;
1894-
}
1895-
1896-
.sidebar:not(.expanded) #sidebar-toggle {
1896+
#sidebar-toggle {
18971897
position: fixed;
18981898
left: 1px;
18991899
top: 100px;
@@ -1910,6 +1910,14 @@ details.rustdoc-toggle[open] > summary.hideme::after {
19101910
border-left: 0;
19111911
}
19121912

1913+
.source-sidebar-expanded #sidebar-toggle {
1914+
left: unset;
1915+
top: unset;
1916+
width: unset;
1917+
border-top-right-radius: unset;
1918+
border-bottom-right-radius: unset;
1919+
}
1920+
19131921
#source-sidebar {
19141922
z-index: 11;
19151923
}
@@ -1952,7 +1960,7 @@ details.rustdoc-toggle[open] > summary.hideme::after {
19521960
padding-left: 2em;
19531961
}
19541962

1955-
.source .sidebar.expanded {
1963+
.source-sidebar-expanded .source .sidebar {
19561964
max-width: 100vw;
19571965
width: 100vw;
19581966
}
@@ -2010,9 +2018,12 @@ details.rustdoc-toggle[open] > summary.hideme::after {
20102018
width: 35px;
20112019
}
20122020

2013-
.sidebar:not(.expanded) #sidebar-toggle {
2021+
#sidebar-toggle {
20142022
top: 10px;
20152023
}
2024+
.source-sidebar-expanded #sidebar-toggle {
2025+
top: unset;
2026+
}
20162027
}
20172028

20182029
.method-toggle summary,

src/librustdoc/html/static/js/source-script.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,13 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
6363
}
6464

6565
function toggleSidebar() {
66-
const sidebar = document.querySelector("nav.sidebar");
6766
const child = this.children[0];
6867
if (child.innerText === ">") {
69-
sidebar.classList.add("expanded");
68+
addClass(document.documentElement, "source-sidebar-expanded");
7069
child.innerText = "<";
7170
updateLocalStorage("source-sidebar-show", "true");
7271
} else {
73-
sidebar.classList.remove("expanded");
72+
removeClass(document.documentElement, "source-sidebar-expanded");
7473
child.innerText = ">";
7574
updateLocalStorage("source-sidebar-show", "false");
7675
}
@@ -103,11 +102,6 @@ function createSourceSidebar() {
103102

104103
const sidebar = document.createElement("div");
105104
sidebar.id = "source-sidebar";
106-
if (getCurrentValue("source-sidebar-show") !== "true") {
107-
container.classList.remove("expanded");
108-
} else {
109-
container.classList.add("expanded");
110-
}
111105

112106
let hasFoundFile = false;
113107

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

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// storage.js is loaded in the `<head>` of all rustdoc pages and doesn't
2+
// use `async` or `defer`. That means it blocks further parsing and rendering
3+
// of the page: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script.
4+
// This makes it the correct place to act on settings that affect the display of
5+
// the page, so we don't see major layout changes during the load of the page.
16
"use strict";
27

38
const darkThemes = ["dark", "ayu"];
@@ -236,6 +241,12 @@ if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
236241
switchToSavedTheme();
237242
}
238243

244+
if (getSettingValue("source-sidebar-show") === "true") {
245+
// At this point in page load, `document.body` is not available yet.
246+
// Set a class on the `<html>` element instead.
247+
addClass(document.documentElement, "source-sidebar-expanded");
248+
}
249+
239250
// If we navigate away (for example to a settings page), and then use the back or
240251
// forward button to get back to a page, the theme may have changed in the meantime.
241252
// But scripts may not be re-loaded in such a case due to the bfcache

src/test/rustdoc-gui/sidebar-source-code.goml

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ assert-css: ("nav.sidebar", {"width": "50px"})
88
// We now click on the button to expand the sidebar.
99
click: (10, 10)
1010
// We wait for the sidebar to be expanded.
11-
wait-for-css: ("nav.sidebar.expanded", {"width": "300px"})
12-
assert-css: ("nav.sidebar.expanded a", {"font-size": "14px"})
11+
wait-for-css: (".source-sidebar-expanded nav.sidebar", {"width": "300px"})
12+
assert-css: (".source-sidebar-expanded nav.sidebar a", {"font-size": "14px"})
1313
// We collapse the sidebar.
1414
click: (10, 10)
1515
// We ensure that the class has been removed.
16-
wait-for: "nav.sidebar:not(.expanded)"
16+
wait-for: "html:not(.expanded)"
1717
assert: "nav.sidebar"
1818

1919
// We now switch to mobile mode.
@@ -22,11 +22,11 @@ size: (600, 600)
2222
assert-css: ("nav.sidebar", {"width": "1px"})
2323
// We expand the sidebar.
2424
click: "#sidebar-toggle"
25-
assert-css: ("nav.sidebar.expanded", {"width": "600px"})
25+
assert-css: (".source-sidebar-expanded nav.sidebar", {"width": "600px"})
2626
// We collapse the sidebar.
2727
click: (10, 10)
2828
// We ensure that the class has been removed.
29-
assert-false: "nav.sidebar.expanded"
29+
assert-false: ".source-sidebar-expanded"
3030
assert: "nav.sidebar"
3131

3232
// Check that the topbar is not visible

src/test/rustdoc-gui/source-code-page.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)
3232

3333
// First we "open" it.
3434
click: "#sidebar-toggle"
35-
assert: ".sidebar.expanded"
35+
assert: ".source-sidebar-expanded"
3636

3737
// We check that the first entry of the sidebar is collapsed (which, for whatever reason,
3838
// is number 2 and not 1...).

0 commit comments

Comments
 (0)