Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ› Fixes flash of default theme before external stylesheets load

Alicia Sykes712a9bb2d6d2
diff --git a/docs/theming.md b/docs/theming.md
index 6db90e89..6a576107 100644
--- a/docs/theming.md
+++ b/docs/theming.md
@@ -109,7 +109,7 @@ For example, if the pages name was "CFT Toolbox", and you wanted to target `.ite
## Loading External Stylesheets
The URI of a stylesheet, either local or hosted on a remote CDN can be passed into the config file. The attribute `appConfig.externalStyleSheet` accepts either a string, or an array of strings. You can also pass custom font stylesheets here, they must be in a CSS format (for example, `https://fonts.googleapis.com/css2?family=Cutive+Mono`).
-This is handled in [`App.vue`](https://github.com/Lissy93/dashy/blob/master/src/App.vue).
+This is handled in [`Theming.js`](https://github.com/Lissy93/dashy/blob/master/src/utils/Theming.js).
For example:
diff --git a/src/App.vue b/src/App.vue
index 37979bdc..79ef9bda 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -150,33 +150,14 @@ export default {
injectCustomStyles(usersCss) {
const style = document.createElement('style');
style.textContent = usersCss;
- document.head.append(style);
+ document.head.insertBefore(style, document.querySelector('[data-external-source]'));
},
- /* Applies user-defined custom CSS and external stylesheets from appConfig */
+ /* Applies user-defined custom CSS from appConfig */
applyCustomStyles() {
if (this.appConfig.customCss) {
const cleanedCss = this.appConfig.customCss.replace(/<\/?[^>]+(>|$)/g, '');
this.injectCustomStyles(cleanedCss);
}
- if (this.appConfig.externalStyleSheet) {
- // Remove any previously injected external stylesheets to avoid duplicates bug
- document.querySelectorAll('[data-external-source]').forEach((el) => el.remove());
- // Then add the external stylesheet(s)
- const externals = this.appConfig.externalStyleSheet;
- const urls = Array.isArray(externals) ? externals : [externals];
- urls.forEach((url) => {
- if (typeof url !== 'string' || (!url.startsWith('http') && !url.startsWith('/'))) {
- ErrorHandler(`Invalid external stylesheet URL: ${url}`);
- return;
- }
- const link = document.createElement('link');
- link.setAttribute('rel', 'stylesheet');
- link.setAttribute('type', 'text/css');
- link.setAttribute('href', url);
- link.setAttribute('data-external-source', 'dashy');
- document.head.appendChild(link);
- });
- }
},
/* Hide splash screen, either after 2 seconds, or immediately based on user preference */
hideSplash() {
diff --git a/src/main.js b/src/main.js
index 52788e21..d3aa30e2 100644
--- a/src/main.js
+++ b/src/main.js
@@ -21,6 +21,7 @@ import { initKeycloakAuth, isKeycloakEnabled } from '@/utils/auth/KeycloakAuth';
import { initHeaderAuth, isHeaderAuthEnabled } from '@/utils/auth/HeaderAuth';
import { initOidcAuth, isOidcEnabled, isOidcLoginPageEnabled } from '@/utils/auth/OidcAuth';
import { isLoggedIn } from '@/utils/auth/Auth';
+import { loadExternalStyleSheets } from '@/utils/Theming';
import Keys from '@/utils/StoreMutations';
import ErrorHandler from '@/utils/logging/ErrorHandler';
import Toast from '@/utils/Toast';
@@ -58,8 +59,11 @@ serviceWorker();
// Checks if user enabled error reporting, and if so will initialize it
ErrorReporting(app, router);
-// Mount the app
-const mount = () => app.mount('#app');
+// Mount the app, once any external stylesheets are ready
+const mount = () => {
+ loadExternalStyleSheets(store.getters.appConfig)
+ .finally(() => app.mount('#app'));
+};
/* Handle failures of third-party auth initialization */
const handleAuthFailure = (provider, err) => {
diff --git a/src/utils/Theming.js b/src/utils/Theming.js
index 30d1fbaf..9ffd52c2 100644
--- a/src/utils/Theming.js
+++ b/src/utils/Theming.js
@@ -6,6 +6,7 @@ import { localStorageKeys, mainCssVars, builtInThemes } from '@/utils/config/def
import ErrorHandler from '@/utils/logging/ErrorHandler';
const EXTERNAL_STYLE_ID = 'user-defined-stylesheet';
+const EXTERNAL_SHEETS_TIMEOUT = 3000;
const html = () => document.documentElement;
/* Map of { label: href } for stylesheets declared in appConfig.externalStyleSheet */
@@ -20,6 +21,30 @@ export const getExternalThemes = (appConfig) => {
return {};
};
+/* Inject every appConfig.externalStyleSheet, resolving once all have loaded/failed, or timed out */
+export const loadExternalStyleSheets = (appConfig) => {
+ const ext = appConfig?.externalStyleSheet;
+ if (!ext) return Promise.resolve();
+ document.querySelectorAll('[data-external-source]').forEach((el) => el.remove());
+ const urls = Array.isArray(ext) ? ext : [ext];
+ const loads = urls.map((url) => {
+ if (typeof url !== 'string' || (!url.startsWith('http') && !url.startsWith('/'))) {
+ ErrorHandler(`Invalid external stylesheet URL: ${url}`);
+ return null;
+ }
+ const link = document.createElement('link');
+ link.rel = 'stylesheet';
+ link.type = 'text/css';
+ link.href = url;
+ link.dataset.externalSource = 'dashy';
+ const loaded = new Promise((resolve) => { link.onload = resolve; link.onerror = resolve; });
+ document.head.appendChild(link);
+ return loaded;
+ });
+ const timeout = new Promise((resolve) => { setTimeout(resolve, EXTERNAL_SHEETS_TIMEOUT); });
+ return Promise.race([Promise.all(loads), timeout]);
+};
+
/* Names of user-defined themes (appConfig.cssThemes). Always returns an array. */
export const getExtraThemeNames = (appConfig) => {
const t = appConfig?.cssThemes;