From f0cb661d91853a57083159cb53a0d03c582ee9a1 Mon Sep 17 00:00:00 2001 From: robin Date: Sun, 21 Dec 2025 00:18:38 +0100 Subject: [PATCH] fix: enable SW on localhost and optimize pre-cache sequence for faster feedback --- public/sw.js | 17 +++++++++-------- src/components/OfflineIndicator.tsx | 6 +++--- src/components/PWARegistration.tsx | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/public/sw.js b/public/sw.js index 80738d8..be32ea2 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,4 +1,4 @@ -const CACHE_NAME = 'whisky-vault-v10-bunker'; // Professional Bunker v10 +const CACHE_NAME = 'whisky-vault-v11-offline'; // Professional Offline-Modus v11 // CONFIG: Core Pages & Static Assets const CORE_PAGES = [ @@ -23,13 +23,13 @@ async function broadcast(message) { } // Helper: Fetch with Timeout & AbortController -async function fetchWithTimeout(url, timeoutMs = 15000) { +async function fetchWithTimeout(url, timeoutMs = 25000) { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), timeoutMs); try { const response = await fetch(url, { signal: controller.signal, - cache: 'no-store' // Ensure we get fresh bits for the bunker + cache: 'no-store' // Ensure we get fresh bits }); clearTimeout(id); return response; @@ -39,15 +39,16 @@ async function fetchWithTimeout(url, timeoutMs = 15000) { } } -// 🏗️ INSTALL: Build the Bunker +// 🏗️ INSTALL: Build the Offline-Modus self.addEventListener('install', (event) => { // Immediate takeover self.skipWaiting(); event.waitUntil( caches.open(CACHE_NAME).then(async (cache) => { - console.log('🏗️ PWA: Building Bunker v10...'); - const items = [...CORE_PAGES, ...STATIC_ASSETS]; + console.log('🏗️ PWA: Building Offline-Modus v11...'); + // Load small static assets first for instant progress bar feedback + const items = [...STATIC_ASSETS, ...CORE_PAGES]; const total = items.length; let loaded = 0; @@ -94,9 +95,9 @@ self.addEventListener('activate', (event) => { // 💬 MESSAGE: Handle status checks self.addEventListener('message', (event) => { - if (event.data?.type === 'CHECK_BUNKER_STATUS') { + if (event.data?.type === 'CHECK_OFFLINE_STATUS') { event.source.postMessage({ - type: 'BUNKER_STATUS', + type: 'OFFLINE_STATUS', isReady: true, version: CACHE_NAME }); diff --git a/src/components/OfflineIndicator.tsx b/src/components/OfflineIndicator.tsx index fc0f1e5..b0ffd12 100644 --- a/src/components/OfflineIndicator.tsx +++ b/src/components/OfflineIndicator.tsx @@ -20,7 +20,7 @@ export default function OfflineIndicator() { if (event.data?.type === 'PRECACHE_PROGRESS') { setProgress(event.data.progress); } - if (event.data?.type === 'PRECACHE_COMPLETE' || event.data?.type === 'BUNKER_STATUS') { + if (event.data?.type === 'PRECACHE_COMPLETE' || event.data?.type === 'OFFLINE_STATUS') { setIsReady(true); localStorage.setItem('whisky_bunker_ready', 'true'); } @@ -32,7 +32,7 @@ export default function OfflineIndicator() { if ('serviceWorker' in navigator) { navigator.serviceWorker.addEventListener('message', handleMessage); if (navigator.serviceWorker.controller) { - navigator.serviceWorker.controller.postMessage({ type: 'CHECK_BUNKER_STATUS' }); + navigator.serviceWorker.controller.postMessage({ type: 'CHECK_OFFLINE_STATUS' }); } } @@ -72,7 +72,7 @@ export default function OfflineIndicator() {
- {progress > 0 ? `Lade Offline-Daten... ${progress}%` : 'Initialisiere...'} + {progress > 0 ? `Lade Offline-Daten... ${progress}%` : 'Offline-Modus wird vorbereitet...'}
); diff --git a/src/components/PWARegistration.tsx b/src/components/PWARegistration.tsx index 7d70423..5c1c9ba 100644 --- a/src/components/PWARegistration.tsx +++ b/src/components/PWARegistration.tsx @@ -4,7 +4,7 @@ import { useEffect } from 'react'; export default function PWARegistration() { useEffect(() => { - if ('serviceWorker' in navigator && window.location.hostname !== 'localhost') { + if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker .register('/sw.js')