From 27c764f0f1bd842f42ed855b5b15585c9baf2537 Mon Sep 17 00:00:00 2001 From: ACX <8075870+acx10@users.noreply.github.com> Date: Fri, 23 Jan 2026 20:51:22 -0700 Subject: [PATCH] feat(pwa): add offline handling and fallback support (#2444) Co-authored-by: acx10 --- booklore-ui/ngsw-config.json | 1 + booklore-ui/src/app/app.component.html | 29 ++++++++++++++++--- booklore-ui/src/app/app.component.scss | 16 ++++++++++ booklore-ui/src/app/app.component.ts | 18 ++++++++++++ .../src/app/core/security/auth-initializer.ts | 16 ++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/booklore-ui/ngsw-config.json b/booklore-ui/ngsw-config.json index 69edd2878..a5814f5f2 100644 --- a/booklore-ui/ngsw-config.json +++ b/booklore-ui/ngsw-config.json @@ -8,6 +8,7 @@ "resources": { "files": [ "/favicon.ico", + "/assets/favicon.svg", "/index.csr.html", "/index.html", "/manifest.webmanifest", diff --git a/booklore-ui/src/app/app.component.html b/booklore-ui/src/app/app.component.html index 6f0f0d716..98abd356e 100644 --- a/booklore-ui/src/app/app.component.html +++ b/booklore-ui/src/app/app.component.html @@ -1,10 +1,31 @@ @if (loading) {
- -

Loading Booklore…

-

Please wait while we get things ready.

-
+ + @if (offline) { +

You're Offline

+

BookLore needs a connection to the server to load.

+

Check your network and try again.

+ } @else { +

Loading Booklore…

+

Please wait while we get things ready.

+
+ } +
+
+} @else if (offline) { +
+
+ +

You're Offline

+

Your connection was lost. Some features may not work.

+
} @else { diff --git a/booklore-ui/src/app/app.component.scss b/booklore-ui/src/app/app.component.scss index 397661ebe..e2966d16e 100644 --- a/booklore-ui/src/app/app.component.scss +++ b/booklore-ui/src/app/app.component.scss @@ -47,3 +47,19 @@ p { transform: rotate(360deg); } } + +.retry-btn { + margin-top: 1rem; + padding: 0.6rem 1.5rem; + background-color: #818cf8; + color: #fff; + border: none; + border-radius: 6px; + font-size: 0.95rem; + cursor: pointer; + transition: background-color 0.2s; + + &:hover { + background-color: #6366f1; + } +} diff --git a/booklore-ui/src/app/app.component.ts b/booklore-ui/src/app/app.component.ts index 32871b47e..54d3320d1 100644 --- a/booklore-ui/src/app/app.component.ts +++ b/booklore-ui/src/app/app.component.ts @@ -28,6 +28,7 @@ import {scan, withLatestFrom} from 'rxjs/operators'; export class AppComponent implements OnInit, OnDestroy { loading = true; + offline = !navigator.onLine; private subscriptions: Subscription[] = []; private subscriptionsInitialized = false; @@ -43,6 +44,9 @@ export class AppComponent implements OnInit, OnDestroy { private libraryLoadingService = inject(LibraryLoadingService); ngOnInit(): void { + window.addEventListener('online', this.onOnline); + window.addEventListener('offline', this.onOffline); + this.authInit.initialized$.subscribe(ready => { this.loading = !ready; if (ready && !this.subscriptionsInitialized) { @@ -52,6 +56,18 @@ export class AppComponent implements OnInit, OnDestroy { }); } + private onOnline = () => { + this.offline = false; + }; + + private onOffline = () => { + this.offline = true; + }; + + reload(): void { + window.location.reload(); + } + private setupWebSocketSubscriptions(): void { this.subscriptions.push( this.rxStompService.watch('/user/queue/book-add').pipe( @@ -125,6 +141,8 @@ export class AppComponent implements OnInit, OnDestroy { } ngOnDestroy(): void { + window.removeEventListener('online', this.onOnline); + window.removeEventListener('offline', this.onOffline); this.subscriptions.forEach(sub => sub.unsubscribe()); this.libraryLoadingService.hide(); } diff --git a/booklore-ui/src/app/core/security/auth-initializer.ts b/booklore-ui/src/app/core/security/auth-initializer.ts index 99f7da75d..4948a3278 100644 --- a/booklore-ui/src/app/core/security/auth-initializer.ts +++ b/booklore-ui/src/app/core/security/auth-initializer.ts @@ -8,6 +8,7 @@ const OIDC_BYPASS_KEY = 'booklore-oidc-bypass'; const OIDC_ERROR_COUNT_KEY = 'booklore-oidc-error-count'; const MAX_OIDC_RETRIES = 3; const OIDC_TIMEOUT_MS = 5000; +const SETTINGS_TIMEOUT_MS = 10000; function withTimeout(promise: Promise, timeoutMs: number): Promise { return Promise.race([ @@ -26,8 +27,23 @@ export function initializeAuthFactory() { const authInitService = inject(AuthInitializationService); return new Promise((resolve) => { + if (!navigator.onLine) { + console.warn('[Auth] App is offline, skipping auth initialization'); + authInitService.markAsInitialized(); + resolve(); + return; + } + + const settingsTimeout = setTimeout(() => { + console.warn('[Auth] Public settings fetch timed out, falling back to local auth'); + sub.unsubscribe(); + authInitService.markAsInitialized(); + resolve(); + }, SETTINGS_TIMEOUT_MS); + const sub = appSettingsService.publicAppSettings$.subscribe(publicSettings => { if (publicSettings) { + clearTimeout(settingsTimeout); const forceLocalOnly = new URLSearchParams(window.location.search).get('localOnly') === 'true'; const oidcBypassed = localStorage.getItem(OIDC_BYPASS_KEY) === 'true'; const errorCount = parseInt(localStorage.getItem(OIDC_ERROR_COUNT_KEY) || '0', 10);