fix: resolve critical app functionality issues

Complete implementation of fixes for queue processing, SSE connection display, service worker installation, and failing tests.

Key Changes:
- Fix queue processor startup with proper import and subscription mechanism
- Implement centralized API error handling middleware for proper HTTP status codes
- Enhance service worker configuration for PWA compliance and reliability
- Fix SSE connection display with reactive state management
- Add comprehensive test coverage and health check endpoints

Results:
- All 169 tests now passing (previously 16 failing)
- Queue items process immediately from pending to success/error states
- Real-time SSE connection status with auto-reconnection logic
- Proper PWA functionality with working service worker registration
- API endpoints return correct HTTP status codes (400/404/409) instead of 500 errors

This resolves the critical issues preventing core app functionality and enables proper production deployment.
This commit is contained in:
Giancarmine Salucci
2025-12-22 04:27:59 +01:00
parent b60f96a75e
commit 93aa25a31c
25 changed files with 3243 additions and 559 deletions

View File

@@ -1,21 +1,116 @@
/// <reference types="vite/client" />
/// <reference lib="webworker" />
// Standard workbox imports - let the build process handle these
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching';
import { NavigationRoute, registerRoute } from 'workbox-routing';
declare let self: ServiceWorkerGlobalScope;
// PWA Workbox caching
precacheAndRoute(self.__WB_MANIFEST);
cleanupOutdatedCaches();
// Handle navigation requests
const handler = createHandlerBoundToURL('/');
const navigationRoute = new NavigationRoute(handler, {
denylist: [/^\/api/]
// Global error handler for service worker
self.addEventListener('error', (event) => {
console.error('[SW] Global error:', event.error);
console.error('[SW] Error details:', {
message: event.message,
filename: event.filename,
lineno: event.lineno,
colno: event.colno,
error: event.error
});
});
registerRoute(navigationRoute);
// Unhandled promise rejection handler
self.addEventListener('unhandledrejection', (event) => {
console.error('[SW] Unhandled promise rejection:', event.reason);
event.preventDefault(); // Prevent default browser behavior
});
console.log('[SW] Service worker script loading...');
// Get the workbox manifest - this will be injected by the build process
const workboxManifest = self.__WB_MANIFEST;
// Wrap workbox initialization in try-catch with granular error handling
try {
console.log('[SW] Initializing workbox...');
// Check if workbox functions are available
if (typeof precacheAndRoute !== 'function' || typeof cleanupOutdatedCaches !== 'function') {
throw new Error('Workbox functions not available');
}
// Detect environment - in production, workbox manifest should be injected
const isDevelopment = !workboxManifest || (workboxManifest && workboxManifest.length === 0);
console.log(`[SW] Running in ${isDevelopment ? 'development' : 'production'} mode`);
// Enhanced manifest validation with detailed logging
if (!workboxManifest) {
if (isDevelopment) {
console.info('[SW] Workbox manifest not injected - running in development mode, precaching disabled');
} else {
console.warn('[SW] Workbox manifest not found in production build - this may be a build issue');
}
} else if (!Array.isArray(workboxManifest)) {
console.error('[SW] Workbox manifest exists but is invalid format:', typeof workboxManifest);
} else if (workboxManifest.length === 0) {
console.warn('[SW] Workbox manifest is empty - no assets to precache');
} else {
console.log(`[SW] Workbox manifest found with ${workboxManifest.length} entries`);
console.debug('[SW] Manifest entries:', workboxManifest.slice(0, 5)); // Log first 5 for debugging
try {
precacheAndRoute(workboxManifest);
console.log('[SW] Precaching completed successfully');
} catch (precacheError) {
console.error('[SW] Error during precaching:', precacheError);
}
}
// Always try to cleanup outdated caches
try {
cleanupOutdatedCaches();
console.log('[SW] Cache cleanup completed');
} catch (cleanupError) {
console.error('[SW] Error during cache cleanup:', cleanupError);
}
// Handle navigation requests with additional error handling
try {
console.log('[SW] Setting up navigation routing...');
if (typeof createHandlerBoundToURL === 'function' && typeof NavigationRoute === 'function' && typeof registerRoute === 'function') {
const handler = createHandlerBoundToURL('/');
const navigationRoute = new NavigationRoute(handler, {
denylist: [/^\/api/]
});
registerRoute(navigationRoute);
console.log('[SW] Navigation routing configured successfully');
} else {
throw new Error('Navigation routing functions not available');
}
} catch (routingError) {
console.error('[SW] Error setting up navigation routing:', routingError);
// Continue without navigation routing if it fails
}
console.log('[SW] Workbox initialization completed');
} catch (error) {
console.error('[SW] Critical error initializing workbox:', error);
console.error('[SW] Error details:', {
name: error.name,
message: error.message,
stack: error.stack
});
// In development mode, this is expected behavior
if (!workboxManifest || (Array.isArray(workboxManifest) && workboxManifest.length === 0)) {
console.info('[SW] Continuing with limited functionality in development mode');
} else {
console.error('[SW] Production build should have workbox manifest - check build configuration');
}
// Continue with service worker registration even if workbox fails
// This allows push notifications and other features to still work
}
// Push notification handling
self.addEventListener('push', (event) => {