@@ -216,6 +216,15 @@ var updateSystemTheme = (function() {
216
216
} ;
217
217
} ) ( ) ;
218
218
219
+ function switchToSavedTheme ( ) {
220
+ switchTheme (
221
+ window . currentTheme ,
222
+ window . mainTheme ,
223
+ getSettingValue ( "theme" ) || "light" ,
224
+ false
225
+ ) ;
226
+ }
227
+
219
228
if ( getSettingValue ( "use-system-theme" ) !== "false" && window . matchMedia ) {
220
229
// update the preferred dark theme if the user is already using a dark theme
221
230
// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
@@ -228,10 +237,20 @@ if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
228
237
// call the function to initialize the theme at least once!
229
238
updateSystemTheme ( ) ;
230
239
} else {
231
- switchTheme (
232
- window . currentTheme ,
233
- window . mainTheme ,
234
- getSettingValue ( "theme" ) || "light" ,
235
- false
236
- ) ;
240
+ switchToSavedTheme ( ) ;
237
241
}
242
+
243
+ // If we navigate away (for example to a settings page), and then use the back or
244
+ // forward button to get back to a page, the theme may have changed in the meantime.
245
+ // But scripts may not be re-loaded in such a case due to the bfcache
246
+ // (https://web.dev/bfcache/). The "pageshow" event triggers on such navigations.
247
+ // Use that opportunity to update the theme.
248
+ // We use a setTimeout with a 0 timeout here to put the change on the event queue.
249
+ // For some reason, if we try to change the theme while the `pageshow` event is
250
+ // running, it sometimes fails to take effect. The problem manifests on Chrome,
251
+ // specifically when talking to a remote website with no caching.
252
+ window . addEventListener ( "pageshow" , function ( ev ) {
253
+ if ( ev . persisted ) {
254
+ setTimeout ( switchToSavedTheme , 0 ) ;
255
+ }
256
+ } ) ;
0 commit comments