From e64f53ad3327fb9fc2273b9c8660cff233a5d985 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 6 Dec 2019 11:56:12 +0200 Subject: [PATCH] Apply user theme as soon as possible --- client/js/loading-error-handlers.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/client/js/loading-error-handlers.js b/client/js/loading-error-handlers.js index 922175fb..1260137b 100644 --- a/client/js/loading-error-handlers.js +++ b/client/js/loading-error-handlers.js @@ -61,6 +61,29 @@ document.getElementById("loading").remove(); }; + // Apply user theme as soon as possible, before any other code loads + // This prevents flash of white while other code loads and socket connects + try { + const userSettings = JSON.parse(localStorage.getItem("settings")); + const themeEl = document.getElementById("theme"); + + if ( + typeof userSettings.theme === "string" && + themeEl.dataset.serverTheme !== userSettings.theme + ) { + themeEl.attributes.href.value = `themes/${userSettings.theme}.css`; + } + + if ( + typeof userSettings.userStyles === "string" && + !/[?&]nocss/.test(window.location.search) + ) { + document.getElementById("user-specified-css").innerHTML = userSettings.userStyles; + } + } catch (e) { + // + } + // Trigger early service worker registration if ("serviceWorker" in navigator) { navigator.serviceWorker.register("service-worker.js");