fix: robust SW status tracking and polling for mobile production
This commit is contained in:
25
public/sw.js
25
public/sw.js
@@ -1,4 +1,4 @@
|
||||
const CACHE_NAME = 'whisky-vault-v12-offline';
|
||||
const CACHE_NAME = 'whisky-vault-v13-offline';
|
||||
|
||||
// CONFIG: Assets
|
||||
const STATIC_ASSETS = [
|
||||
@@ -12,15 +12,21 @@ const CORE_PAGES = [
|
||||
'/',
|
||||
];
|
||||
|
||||
// Global state to track progress even when UI is not listening
|
||||
// Global state to track progress
|
||||
let currentProgress = 0;
|
||||
let isPrecacheFinished = false;
|
||||
|
||||
// Helper: Broadcast to all clients
|
||||
// Helper: Broadcast to ALL clients (including those being installed)
|
||||
async function broadcast(message) {
|
||||
try {
|
||||
const clients = await self.clients.matchAll({ includeUncontrolled: true, type: 'window' });
|
||||
clients.forEach(client => client.postMessage(message));
|
||||
clients.forEach(client => {
|
||||
try {
|
||||
client.postMessage(message);
|
||||
} catch (err) {
|
||||
// Ignore message delivery errors
|
||||
}
|
||||
});
|
||||
} catch (e) { }
|
||||
}
|
||||
|
||||
@@ -43,23 +49,22 @@ async function fetchWithTimeout(url, timeoutMs = 30000) {
|
||||
|
||||
// 🏗️ INSTALL: Build the Offline-Modus
|
||||
self.addEventListener('install', (event) => {
|
||||
// Take over immediately
|
||||
self.skipWaiting();
|
||||
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME).then(async (cache) => {
|
||||
console.log('🏗️ PWA: Building Offline-Modus v12...');
|
||||
|
||||
// 💡 WAIT A MOMENT: Give the UI time to mount and register listeners
|
||||
// In dev mode, the app takes a second to boot up.
|
||||
await new Promise(resolve => setTimeout(resolve, 1500));
|
||||
console.log('🏗️ PWA: Building Offline-Modus v13...');
|
||||
|
||||
const items = [...STATIC_ASSETS, ...CORE_PAGES];
|
||||
const total = items.length;
|
||||
let loaded = 0;
|
||||
|
||||
// Start broadcasting 0% immediately
|
||||
broadcast({ type: 'OFFLINE_PROGRESS', progress: 0 });
|
||||
|
||||
for (const url of items) {
|
||||
try {
|
||||
// Start with manifest and icons (fast) then the app shell (slow in dev)
|
||||
const res = await fetchWithTimeout(url);
|
||||
if (res && res.ok) {
|
||||
await cache.put(url, res);
|
||||
|
||||
Reference in New Issue
Block a user