Skip to content

Commit ab41e2b

Browse files
committed
rustdoc: avoid calling document.write after the page loads
1 parent 7e86fd6 commit ab41e2b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,18 @@ function switchTheme(newThemeName, saveTheme) {
131131
const newHref = getVar("root-path") + newThemeName +
132132
getVar("resource-suffix") + ".css";
133133
if (!window.currentTheme) {
134-
document.write(`<link rel="stylesheet" id="themeStyle" href="${newHref}">`);
135-
window.currentTheme = document.getElementById("themeStyle");
134+
// If we're in the middle of loading, document.write blocks
135+
// rendering, but if we are done, it would blank the page.
136+
if (document.readyState === "loading") {
137+
document.write(`<link rel="stylesheet" id="themeStyle" href="${newHref}">`);
138+
window.currentTheme = document.getElementById("themeStyle");
139+
} else {
140+
window.currentTheme = document.createElement("link");
141+
window.currentTheme.rel = "stylesheet";
142+
window.currentTheme.id = "themeStyle";
143+
window.currentTheme.href = newHref;
144+
document.documentElement.appendChild(window.currentTheme);
145+
}
136146
} else if (newHref !== window.currentTheme.href) {
137147
window.currentTheme.href = newHref;
138148
}

0 commit comments

Comments
 (0)