fix: enable SW on localhost and optimize pre-cache sequence for faster feedback
This commit is contained in:
17
public/sw.js
17
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
|
||||
});
|
||||
|
||||
@@ -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() {
|
||||
<div className="flex items-center gap-1.5 px-2.5 py-1 bg-amber-600/10 border border-amber-600/20 rounded-full">
|
||||
<Loader2 size={10} className="text-amber-600 animate-spin" />
|
||||
<span className="text-[9px] font-black uppercase tracking-widest text-amber-600">
|
||||
{progress > 0 ? `Lade Offline-Daten... ${progress}%` : 'Initialisiere...'}
|
||||
{progress > 0 ? `Lade Offline-Daten... ${progress}%` : 'Offline-Modus wird vorbereitet...'}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user