about summary refs log tree commit diff
path: root/src/librustdoc/html/static
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-01-24 12:29:57 +0100
committerGitHub <noreply@github.com>2022-01-24 12:29:57 +0100
commitee35ed3e1a828b47478d70b569dc979d9119aabb (patch)
tree803ed2ddd148edebde6594ce72a6e809a1bf7b8c /src/librustdoc/html/static
parentd1aa2f7b0b652026b404788a4e7af557753d662f (diff)
parentb30725eab7cbc898dbdb851d5977a352d1dd2187 (diff)
downloadrust-ee35ed3e1a828b47478d70b569dc979d9119aabb.tar.gz
rust-ee35ed3e1a828b47478d70b569dc979d9119aabb.zip
Rollup merge of #93253 - jsha:theme-on-show, r=GuillaumeGomez
Update theme on pageshow event

When a user goes forward or back, the page may be rendered from the back/forward cache (https://web.dev/bfcache/) rather than from scratch. If they have changed theme in the meantime, that means seeing an incorrect theme on the page they went forward or back to. The `pageshow` event fires on such navigations, so we can update the theme based on that event.

Demo: https://rustdoc.crud.net/jsha/theme-on-show/std/string/trait.ToString.html

r? `@GuillaumeGomez`
Diffstat (limited to 'src/librustdoc/html/static')
-rw-r--r--src/librustdoc/html/static/js/storage.js31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/librustdoc/html/static/js/storage.js b/src/librustdoc/html/static/js/storage.js
index 2394df4c715..06bb34eb115 100644
--- a/src/librustdoc/html/static/js/storage.js
+++ b/src/librustdoc/html/static/js/storage.js
@@ -216,6 +216,15 @@ var updateSystemTheme = (function() {
     };
 })();
 
+function switchToSavedTheme() {
+    switchTheme(
+        window.currentTheme,
+        window.mainTheme,
+        getSettingValue("theme") || "light",
+        false
+    );
+}
+
 if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
     // update the preferred dark theme if the user is already using a dark theme
     // See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
@@ -228,10 +237,20 @@ if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
     // call the function to initialize the theme at least once!
     updateSystemTheme();
 } else {
-    switchTheme(
-        window.currentTheme,
-        window.mainTheme,
-        getSettingValue("theme") || "light",
-        false
-    );
+    switchToSavedTheme();
 }
+
+// If we navigate away (for example to a settings page), and then use the back or
+// forward button to get back to a page, the theme may have changed in the meantime.
+// But scripts may not be re-loaded in such a case due to the bfcache
+// (https://web.dev/bfcache/). The "pageshow" event triggers on such navigations.
+// Use that opportunity to update the theme.
+// We use a setTimeout with a 0 timeout here to put the change on the event queue.
+// For some reason, if we try to change the theme while the `pageshow` event is
+// running, it sometimes fails to take effect. The problem manifests on Chrome,
+// specifically when talking to a remote website with no caching.
+window.addEventListener("pageshow", function(ev) {
+    if (ev.persisted) {
+        setTimeout(switchToSavedTheme, 0);
+    }
+});