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!

✨ Add enableAuthProxyCompat to recover SW from auth proxy expiry (#2189)

Alicia Sykes5f233e83dfa2
diff --git a/docs/configuring.md b/docs/configuring.md
index 4b4d2b11..9ba10278 100644
--- a/docs/configuring.md
+++ b/docs/configuring.md
@@ -144,6 +144,7 @@ For more info, see the[Multi-Page docs](/docs/pages-and-sections.md#multi-page-s
**`disableSmartSort`** | `boolean` | _Optional_ | For the most-used and last-used app sort functions to work, a basic open-count is stored in local storage. If you do not want this to happen, then disable smart sort here, but you wil no longer be able to use these sort options. Defaults to `false`.
**`disableUpdateChecks`** | `boolean` | _Optional_ | If set to true, Dashy will not check for updates. Defaults to `false`.
**`enableServiceWorker`** | `boolean` | _Optional_ | Service workers cache web applications to improve load times and offer basic offline functionality, and are disabled by default in Dashy. The service worker can sometimes cause older content to be cached, requiring the app to be hard-refreshed. If you do not want SW functionality, or are having issues with caching, set this property to `false` to disable all service workers.
+**`enableAuthProxyCompat`** | `boolean` | _Optional_ | Only relevant when `enableServiceWorker` is `true` and when Dashy sits behind an authentication proxy (such as Cloudflare Zero Trust or Authelia). Set this to `true` to detect that redirect on load, unregister the service worker, and reload so re-authentication can proceed. Defaults to `false`.
**`disableContextMenu`** | `boolean` | _Optional_ | If set to `true`, the custom right-click context menu will be disabled. Defaults to `false`.
**[⬆️ Back to Top](#configuring)**
diff --git a/src/utils/InitServiceWorker.js b/src/utils/InitServiceWorker.js
index ab322d2f..eeb3f673 100644
--- a/src/utils/InitServiceWorker.js
+++ b/src/utils/InitServiceWorker.js
@@ -1,11 +1,14 @@
import { load as yamlLoad } from 'js-yaml';
import request from '@/utils/request';
import i18n from '@/utils/i18n';
+import { serviceEndpoints } from '@/utils/config/defaults';
import { statusMsg, statusErrorMsg } from '@/utils/logging/CoolConsole';
import { toast } from '@/utils/Toast';
const SW_LABEL = 'Service Worker Status';
const UPDATE_CHECK_INTERVAL_MS = 60 * 60 * 1000; // hourly
+const AUTH_PROXY_COMPAT_KEY = 'dashy-auth-proxy-compat'; // mirrors appConfig.enableAuthProxyCompat
+const AUTH_PROXY_RELOAD_KEY = 'dashy-auth-proxy-reloaded'; // one reload per tab, guards against loops
/* Loads conf.yml and returns the parsed object, or null on failure */
const loadAppConfig = async () => {
@@ -28,6 +31,44 @@ const unregisterAll = async () => {
} catch { /* no-op */ }
};
+/* Remember the opt-in so it's known on later loads even when conf.yml comes from cache or fails */
+const rememberAuthProxyCompat = (enabled) => {
+ try {
+ if (enabled) localStorage.setItem(AUTH_PROXY_COMPAT_KEY, '1');
+ else localStorage.removeItem(AUTH_PROXY_COMPAT_KEY);
+ } catch { /* storage unavailable */ }
+};
+
+const isReloadGuardSet = () => {
+ try { return !!sessionStorage.getItem(AUTH_PROXY_RELOAD_KEY); } catch { return true; }
+};
+const setReloadGuard = (on) => {
+ try {
+ if (on) sessionStorage.setItem(AUTH_PROXY_RELOAD_KEY, '1');
+ else sessionStorage.removeItem(AUTH_PROXY_RELOAD_KEY);
+ } catch { /* no-op */ }
+};
+
+/**
+ * If enabled (with appConfig.enableAuthProxyCompat), then check for expired sessions,
+ * and then drop the cached SW and trigger a reload so the proxy can re-authenticate user
+ */
+const recoverFromAuthProxy = async () => {
+ try { if (localStorage.getItem(AUTH_PROXY_COMPAT_KEY) !== '1') return false; } catch { return false; }
+ if (!navigator.serviceWorker.controller) { setReloadGuard(false); return false; }
+ if (isReloadGuardSet()) return false;
+ let res;
+ try {
+ res = await fetch(serviceEndpoints.getUser, { cache: 'no-store', redirect: 'manual' });
+ } catch { return false; } // network error / offline - keep SW for offline use
+ if (res.type !== 'opaqueredirect') { setReloadGuard(false); return false; } // valid session - reset
+ setReloadGuard(true);
+ statusMsg(SW_LABEL, 'Auth proxy redirect detected - unregistering SW and reloading.');
+ await unregisterAll();
+ window.location.reload();
+ return true;
+};
+
/* Sticky toast with a Refresh action that swaps in the new SW and reloads */
const promptForUpdate = (updateSW) => {
const t = i18n.global.t;
@@ -44,7 +85,14 @@ const initServiceWorker = async () => {
if (!('serviceWorker' in navigator)) return;
const conf = await loadAppConfig();
- if (!conf) return; // network/parse failed β€” leave any existing SW alone
+ if (conf?.appConfig) {
+ rememberAuthProxyCompat(conf.appConfig.enableServiceWorker && conf.appConfig.enableAuthProxyCompat);
+ }
+
+ // Probe for an auth-proxy session expiry
+ if (await recoverFromAuthProxy()) return;
+
+ if (!conf) return; // network/parse failed - leave any existing SW alone
if (!conf.appConfig?.enableServiceWorker) {
await unregisterAll();
diff --git a/src/utils/config/ConfigSchema.json b/src/utils/config/ConfigSchema.json
index d6ed991d..21f68b80 100644
--- a/src/utils/config/ConfigSchema.json
+++ b/src/utils/config/ConfigSchema.json
@@ -756,6 +756,12 @@
"default": false,
"description": "If set to true, then service workers will be used to cache page contents"
},
+ "enableAuthProxyCompat": {
+ "title": "Enable Auth Proxy Compatibility",
+ "type": "boolean",
+ "default": false,
+ "description": "If set to true, when an authentication proxy (e.g. Cloudflare Zero Trust, Authelia) redirects after session expiry, the service worker is unregistered and the page reloaded so the login redirect can proceed. Only relevant when enableServiceWorker is true"
+ },
"disableContextMenu": {
"title": "Disable Context Menus",
"type": "boolean",