feat(pwa): add offline handling and fallback support (#2444)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
"resources": {
|
"resources": {
|
||||||
"files": [
|
"files": [
|
||||||
"/favicon.ico",
|
"/favicon.ico",
|
||||||
|
"/assets/favicon.svg",
|
||||||
"/index.csr.html",
|
"/index.csr.html",
|
||||||
"/index.html",
|
"/index.html",
|
||||||
"/manifest.webmanifest",
|
"/manifest.webmanifest",
|
||||||
|
|||||||
@@ -1,10 +1,31 @@
|
|||||||
@if (loading) {
|
@if (loading) {
|
||||||
<div class="splash-screen">
|
<div class="splash-screen">
|
||||||
<div class="splash-content">
|
<div class="splash-content">
|
||||||
<img src="assets/favicon.svg" alt="Booklore logo" class="logo" />
|
<svg class="logo" viewBox="0 0 126 126" xmlns="http://www.w3.org/2000/svg">
|
||||||
<h1>Loading Booklore…</h1>
|
<path d="M59 4.79297C71.5051 11.5557 80 24.7854 80 40C80 40.5959 79.987 41.1888 79.9609 41.7783C79.8609 44.0406 81.7355 46 84 46C106.091 46 124 63.9086 124 86C124 108.091 106.091 126 84 126H10C4.47715 126 0 121.523 0 116V39.0068L0.0126953 38.9941C0.357624 25.0252 7.86506 12.8347 19 5.95215V63.832C19 64.8345 20.0676 65.4391 20.9121 64.9902L21.0771 64.8867L38.2227 52.3428C38.6819 52.0068 39.3064 52.0068 39.7656 52.3428L56.9229 64.8945L57.0879 64.998C57.9324 65.447 59 64.8423 59 63.8398V4.79297Z" fill="#818cf8"/>
|
||||||
<p>Please wait while we get things ready.</p>
|
<path d="M40 0C43.8745 0 47.6199 0.552381 51.1631 1.58008V50.9697L44.3926 46.0176L44.0879 45.8037C40.9061 43.6679 36.7098 43.7393 33.5957 46.0176L26.8369 50.9619V2.21875C30.9593 0.782634 35.3881 0 40 0Z" fill="white"/>
|
||||||
<div class="loader"></div>
|
</svg>
|
||||||
|
@if (offline) {
|
||||||
|
<h1>You're Offline</h1>
|
||||||
|
<p>BookLore needs a connection to the server to load.</p>
|
||||||
|
<p>Check your network and try again.</p>
|
||||||
|
} @else {
|
||||||
|
<h1>Loading Booklore…</h1>
|
||||||
|
<p>Please wait while we get things ready.</p>
|
||||||
|
<div class="loader"></div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
} @else if (offline) {
|
||||||
|
<div class="splash-screen">
|
||||||
|
<div class="splash-content">
|
||||||
|
<svg class="logo" viewBox="0 0 126 126" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M59 4.79297C71.5051 11.5557 80 24.7854 80 40C80 40.5959 79.987 41.1888 79.9609 41.7783C79.8609 44.0406 81.7355 46 84 46C106.091 46 124 63.9086 124 86C124 108.091 106.091 126 84 126H10C4.47715 126 0 121.523 0 116V39.0068L0.0126953 38.9941C0.357624 25.0252 7.86506 12.8347 19 5.95215V63.832C19 64.8345 20.0676 65.4391 20.9121 64.9902L21.0771 64.8867L38.2227 52.3428C38.6819 52.0068 39.3064 52.0068 39.7656 52.3428L56.9229 64.8945L57.0879 64.998C57.9324 65.447 59 64.8423 59 63.8398V4.79297Z" fill="#818cf8"/>
|
||||||
|
<path d="M40 0C43.8745 0 47.6199 0.552381 51.1631 1.58008V50.9697L44.3926 46.0176L44.0879 45.8037C40.9061 43.6679 36.7098 43.7393 33.5957 46.0176L26.8369 50.9619V2.21875C30.9593 0.782634 35.3881 0 40 0Z" fill="white"/>
|
||||||
|
</svg>
|
||||||
|
<h1>You're Offline</h1>
|
||||||
|
<p>Your connection was lost. Some features may not work.</p>
|
||||||
|
<button class="retry-btn" (click)="reload()">Retry</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
} @else {
|
} @else {
|
||||||
|
|||||||
@@ -47,3 +47,19 @@ p {
|
|||||||
transform: rotate(360deg);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import {scan, withLatestFrom} from 'rxjs/operators';
|
|||||||
export class AppComponent implements OnInit, OnDestroy {
|
export class AppComponent implements OnInit, OnDestroy {
|
||||||
|
|
||||||
loading = true;
|
loading = true;
|
||||||
|
offline = !navigator.onLine;
|
||||||
private subscriptions: Subscription[] = [];
|
private subscriptions: Subscription[] = [];
|
||||||
private subscriptionsInitialized = false;
|
private subscriptionsInitialized = false;
|
||||||
|
|
||||||
@@ -43,6 +44,9 @@ export class AppComponent implements OnInit, OnDestroy {
|
|||||||
private libraryLoadingService = inject(LibraryLoadingService);
|
private libraryLoadingService = inject(LibraryLoadingService);
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
window.addEventListener('online', this.onOnline);
|
||||||
|
window.addEventListener('offline', this.onOffline);
|
||||||
|
|
||||||
this.authInit.initialized$.subscribe(ready => {
|
this.authInit.initialized$.subscribe(ready => {
|
||||||
this.loading = !ready;
|
this.loading = !ready;
|
||||||
if (ready && !this.subscriptionsInitialized) {
|
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 {
|
private setupWebSocketSubscriptions(): void {
|
||||||
this.subscriptions.push(
|
this.subscriptions.push(
|
||||||
this.rxStompService.watch('/user/queue/book-add').pipe(
|
this.rxStompService.watch('/user/queue/book-add').pipe(
|
||||||
@@ -125,6 +141,8 @@ export class AppComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
|
window.removeEventListener('online', this.onOnline);
|
||||||
|
window.removeEventListener('offline', this.onOffline);
|
||||||
this.subscriptions.forEach(sub => sub.unsubscribe());
|
this.subscriptions.forEach(sub => sub.unsubscribe());
|
||||||
this.libraryLoadingService.hide();
|
this.libraryLoadingService.hide();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ const OIDC_BYPASS_KEY = 'booklore-oidc-bypass';
|
|||||||
const OIDC_ERROR_COUNT_KEY = 'booklore-oidc-error-count';
|
const OIDC_ERROR_COUNT_KEY = 'booklore-oidc-error-count';
|
||||||
const MAX_OIDC_RETRIES = 3;
|
const MAX_OIDC_RETRIES = 3;
|
||||||
const OIDC_TIMEOUT_MS = 5000;
|
const OIDC_TIMEOUT_MS = 5000;
|
||||||
|
const SETTINGS_TIMEOUT_MS = 10000;
|
||||||
|
|
||||||
function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
|
function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
@@ -26,8 +27,23 @@ export function initializeAuthFactory() {
|
|||||||
const authInitService = inject(AuthInitializationService);
|
const authInitService = inject(AuthInitializationService);
|
||||||
|
|
||||||
return new Promise<void>((resolve) => {
|
return new Promise<void>((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 => {
|
const sub = appSettingsService.publicAppSettings$.subscribe(publicSettings => {
|
||||||
if (publicSettings) {
|
if (publicSettings) {
|
||||||
|
clearTimeout(settingsTimeout);
|
||||||
const forceLocalOnly = new URLSearchParams(window.location.search).get('localOnly') === 'true';
|
const forceLocalOnly = new URLSearchParams(window.location.search).get('localOnly') === 'true';
|
||||||
const oidcBypassed = localStorage.getItem(OIDC_BYPASS_KEY) === 'true';
|
const oidcBypassed = localStorage.getItem(OIDC_BYPASS_KEY) === 'true';
|
||||||
const errorCount = parseInt(localStorage.getItem(OIDC_ERROR_COUNT_KEY) || '0', 10);
|
const errorCount = parseInt(localStorage.getItem(OIDC_ERROR_COUNT_KEY) || '0', 10);
|
||||||
|
|||||||
Reference in New Issue
Block a user