Skip to content

Commit f916681

Browse files
Only load one CSS theme by default
To avoid generating a FOUC at startup, this commit uses `document.write` to load the stylesheet initially. Co-Authored-By: Guillaume Gomez <[email protected]>
1 parent 7c306f6 commit f916681

File tree

4 files changed

+99
-53
lines changed

4 files changed

+99
-53
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,35 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
649649
</noscript>\
650650
<link rel=\"stylesheet\" \
651651
href=\"{static_root_path}{settings_css}\">\
652-
<script defer src=\"{static_root_path}{settings_js}\"></script>",
652+
<script defer src=\"{static_root_path}{settings_js}\"></script>\
653+
<link rel=\"preload\" href=\"{static_root_path}{theme_light_css}\" \
654+
as=\"style\">\
655+
<link rel=\"preload\" href=\"{static_root_path}{theme_dark_css}\" \
656+
as=\"style\">\
657+
<link rel=\"preload\" href=\"{static_root_path}{theme_ayu_css}\" \
658+
as=\"style\">",
653659
static_root_path = page.get_static_root_path(),
654660
settings_css = static_files::STATIC_FILES.settings_css,
655661
settings_js = static_files::STATIC_FILES.settings_js,
656-
)
662+
theme_light_css = static_files::STATIC_FILES.theme_light_css,
663+
theme_dark_css = static_files::STATIC_FILES.theme_dark_css,
664+
theme_ayu_css = static_files::STATIC_FILES.theme_ayu_css,
665+
);
666+
// Pre-load all theme CSS files, so that switching feels seamless.
667+
//
668+
// When loading settings.html as a popover, the equivalent HTML is
669+
// generated in main.js.
670+
for file in &shared.style_files {
671+
if let Ok(theme) = file.basename() {
672+
write!(
673+
buf,
674+
"<link rel=\"preload\" href=\"{root_path}{theme}{suffix}.css\" \
675+
as=\"style\">",
676+
root_path = page.static_root_path.unwrap_or(""),
677+
suffix = page.resource_suffix,
678+
);
679+
}
680+
}
657681
},
658682
&shared.style_files,
659683
);

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
// Local js definitions:
22
/* global addClass, getSettingValue, hasClass, searchState */
3-
/* global onEach, onEachLazy, removeClass */
3+
/* global onEach, onEachLazy, removeClass, getVar */
44

55
"use strict";
66

7-
// Get a value from the rustdoc-vars div, which is used to convey data from
8-
// Rust to the JS. If there is no such element, return null.
9-
function getVar(name) {
10-
const el = document.getElementById("rustdoc-vars");
11-
if (el) {
12-
return el.attributes["data-" + name].value;
13-
} else {
14-
return null;
15-
}
16-
}
17-
187
// Given a basename (e.g. "storage") and an extension (e.g. ".js"), return a URL
198
// for a resource under the root-path, with the resource-suffix.
209
function resourcePath(basename, extension) {
@@ -187,6 +176,15 @@ function loadCss(cssUrl) {
187176
document.getElementsByTagName("head")[0].appendChild(link);
188177
}
189178

179+
function preLoadCss(cssUrl) {
180+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload
181+
const link = document.createElement("link");
182+
link.href = cssUrl;
183+
link.rel = "preload";
184+
link.as = "style";
185+
document.getElementsByTagName("head")[0].appendChild(link);
186+
}
187+
190188
(function() {
191189
const isHelpPage = window.location.pathname.endsWith("/help.html");
192190

@@ -207,6 +205,23 @@ function loadCss(cssUrl) {
207205
// hopefully be loaded when the JS will generate the settings content.
208206
loadCss(getVar("static-root-path") + getVar("settings-css"));
209207
loadScript(getVar("static-root-path") + getVar("settings-js"));
208+
preLoadCss(getVar("static-root-path") + getVar("theme-light-css"));
209+
preLoadCss(getVar("static-root-path") + getVar("theme-dark-css"));
210+
preLoadCss(getVar("static-root-path") + getVar("theme-ayu-css"));
211+
// Pre-load all theme CSS files, so that switching feels seamless.
212+
//
213+
// When loading settings.html as a standalone page, the equivalent HTML is
214+
// generated in context.rs.
215+
setTimeout(() => {
216+
const themes = getVar("themes").split(",");
217+
for (const theme of themes) {
218+
// if there are no themes, do nothing
219+
// "".split(",") == [""]
220+
if (theme !== "") {
221+
preLoadCss(getVar("root-path") + theme + ".css");
222+
}
223+
}
224+
}, 0);
210225
};
211226

212227
window.searchState = {

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
const darkThemes = ["dark", "ayu"];
99
window.currentTheme = document.getElementById("themeStyle");
10-
window.mainTheme = document.getElementById("mainThemeStyle");
1110

1211
// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
1312
// If you update this line, then you also need to update the media query with the same
@@ -44,8 +43,6 @@ function getSettingValue(settingName) {
4443

4544
const localStoredTheme = getSettingValue("theme");
4645

47-
const savedHref = [];
48-
4946
// eslint-disable-next-line no-unused-vars
5047
function hasClass(elem, className) {
5148
return elem && elem.classList && elem.classList.contains(className);
@@ -125,30 +122,37 @@ function getCurrentValue(name) {
125122
}
126123
}
127124

128-
function switchTheme(styleElem, mainStyleElem, newThemeName, saveTheme) {
125+
// Get a value from the rustdoc-vars div, which is used to convey data from
126+
// Rust to the JS. If there is no such element, return null.
127+
const getVar = (function getVar(name) {
128+
const el = document.getElementById("rustdoc-vars");
129+
if (el) {
130+
return el.attributes["data-" + name].value;
131+
} else {
132+
return null;
133+
}
134+
});
135+
136+
function switchTheme(newThemeName, saveTheme) {
129137
// If this new value comes from a system setting or from the previously
130138
// saved theme, no need to save it.
131139
if (saveTheme) {
132140
updateLocalStorage("theme", newThemeName);
133141
}
134142

135-
if (savedHref.length === 0) {
136-
onEachLazy(document.getElementsByTagName("link"), el => {
137-
savedHref.push(el.href);
138-
});
143+
let newHref;
144+
145+
if (newThemeName === "light" || newThemeName === "dark" || newThemeName === "ayu") {
146+
newHref = getVar("static-root-path") + getVar("theme-" + newThemeName + "-css");
147+
} else {
148+
newHref = getVar("root-path") + newThemeName + getVar("resource-suffix") + ".css";
139149
}
140-
const newHref = savedHref.find(url => {
141-
const m = url.match(/static\.files\/(.*)-[a-f0-9]{16}\.css$/);
142-
if (m && m[1] === newThemeName) {
143-
return true;
144-
}
145-
const m2 = url.match(/\/([^/]*)\.css$/);
146-
if (m2 && m2[1].startsWith(newThemeName)) {
147-
return true;
148-
}
149-
});
150-
if (newHref && newHref !== styleElem.href) {
151-
styleElem.href = newHref;
150+
151+
if (!window.currentTheme) {
152+
document.write("<link rel=\"stylesheet\" id=\"themeStyle\" href=\"" + newHref + "\">");
153+
window.currentTheme = document.getElementById("themeStyle");
154+
} else if (newHref !== window.currentTheme.href) {
155+
window.currentTheme.href = newHref;
152156
}
153157
}
154158

@@ -164,7 +168,7 @@ const updateTheme = (function() {
164168
*/
165169
function updateTheme() {
166170
const use = (theme, saveTheme) => {
167-
switchTheme(window.currentTheme, window.mainTheme, theme, saveTheme);
171+
switchTheme(theme, saveTheme);
168172
};
169173

170174
// maybe the user has disabled the setting in the meantime!

src/librustdoc/html/templates/page.html

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,28 @@
1717
<link rel="stylesheet" {#+ #}
1818
href="{{static_root_path|safe}}{{files.rustdoc_css}}" {#+ #}
1919
id="mainThemeStyle"> {# #}
20-
<link rel="stylesheet" id="themeStyle" href="{{static_root_path|safe}}{{files.theme_light_css}}"> {# #}
21-
<link rel="stylesheet" disabled href="{{static_root_path|safe}}{{files.theme_dark_css}}"> {# #}
22-
<link rel="stylesheet" disabled href="{{static_root_path|safe}}{{files.theme_ayu_css}}"> {# #}
23-
{% for theme in themes %}
24-
<link rel="stylesheet" disabled href="{{page.root_path|safe}}{{theme}}{{page.resource_suffix}}.css"> {# #}
25-
{% endfor %}
2620
{% if !layout.default_settings.is_empty() %}
2721
<script id="default-settings" {#+ #}
2822
{%~ for (k, v) in layout.default_settings ~%}
2923
data-{{k}}="{{v}}"
3024
{% endfor %}
3125
></script> {# #}
3226
{% endif %}
27+
<div id="rustdoc-vars" {#+ #}
28+
data-root-path="{{page.root_path|safe}}" {#+ #}
29+
data-static-root-path="{{static_root_path|safe}}" {#+ #}
30+
data-current-crate="{{layout.krate}}" {#+ #}
31+
data-themes="{{themes|join(",") }}" {#+ #}
32+
data-resource-suffix="{{page.resource_suffix}}" {#+ #}
33+
data-rustdoc-version="{{rustdoc_version}}" {#+ #}
34+
data-search-js="{{files.search_js}}" {#+ #}
35+
data-settings-js="{{files.settings_js}}" {#+ #}
36+
data-settings-css="{{files.settings_css}}" {#+ #}
37+
data-theme-light-css="{{files.theme_light_css}}" {#+ #}
38+
data-theme-dark-css="{{files.theme_dark_css}}" {#+ #}
39+
data-theme-ayu-css="{{files.theme_ayu_css}}" {#+ #}
40+
> {# #}
41+
</div> {# #}
3342
<script src="{{static_root_path|safe}}{{files.storage_js}}"></script> {# #}
3443
{% if page.css_class.contains("crate") %}
3544
<script defer src="{{page.root_path|safe}}crates{{page.resource_suffix}}.js"></script> {# #}
@@ -44,6 +53,12 @@
4453
<script defer src="{{static_root_path|safe}}{{files.scrape_examples_js}}"></script> {# #}
4554
{% endif %}
4655
<noscript> {# #}
56+
<link rel="stylesheet" {#+ #}
57+
media="(prefers-color-scheme:light)" {#+ #}
58+
href="{{static_root_path|safe}}{{files.theme_light_css}}"> {# #}
59+
<link rel="stylesheet" {#+ #}
60+
media="(prefers-color-scheme:dark)" {#+ #}
61+
href="{{static_root_path|safe}}{{files.theme_dark_css}}"> {# #}
4762
<link rel="stylesheet" {#+ #}
4863
href="{{static_root_path|safe}}{{files.noscript_css}}"> {# #}
4964
</noscript> {# #}
@@ -132,17 +147,5 @@ <h2></h2> {# #}
132147
{% if page.css_class != "source" %}</div>{% endif %}
133148
</main> {# #}
134149
{{ layout.external_html.after_content|safe }}
135-
<div id="rustdoc-vars" {#+ #}
136-
data-root-path="{{page.root_path|safe}}" {#+ #}
137-
data-static-root-path="{{static_root_path|safe}}" {#+ #}
138-
data-current-crate="{{layout.krate}}" {#+ #}
139-
data-themes="{{themes|join(",") }}" {#+ #}
140-
data-resource-suffix="{{page.resource_suffix}}" {#+ #}
141-
data-rustdoc-version="{{rustdoc_version}}" {#+ #}
142-
data-search-js="{{files.search_js}}" {#+ #}
143-
data-settings-js="{{files.settings_js}}" {#+ #}
144-
data-settings-css="{{files.settings_css}}" {#+ #}
145-
> {# #}
146-
</div> {# #}
147150
</body> {# #}
148151
</html> {# #}

0 commit comments

Comments
 (0)