/* v33.3.5 — PWA Legacy Cache & Service Worker Forced Migration */ (function(){ 'use strict'; if(window.__v3335PwaMigrationReady) return; window.__v3335PwaMigrationReady = true; const TARGET_VERSION = '33.3.5'; const TARGET_SW = 'index.php?entity=pwa&page=sw&v=33_3_5'; const LEGACY_SW_PATTERNS = [ /\/sw\.js(?:\?|$)/i, /v=33_2_53/i, /v=33_3_[0-4]/i ]; const LEGACY_CACHE_PATTERNS = [ /^assoc-static-33\.2\./i, /^assoc-offline-33\.2\./i, /^assoc-static-33\.3\.[0-4]/i, /^assoc-offline-33\.3\.[0-4]/i ]; function ready(fn){ if(document.readyState !== 'loading') fn(); else document.addEventListener('DOMContentLoaded', fn, {once:true}); } async function clearLegacyCaches(){ if(!('caches' in window)) return []; const keys = await caches.keys(); const removed = []; for(const key of keys){ if(LEGACY_CACHE_PATTERNS.some(rx => rx.test(key))){ try{ if(await caches.delete(key)) removed.push(key); }catch(e){} } } return removed; } async function unregisterLegacyWorkers(){ if(!('serviceWorker' in navigator)) return []; const regs = await navigator.serviceWorker.getRegistrations(); const removed = []; for(const reg of regs){ let script = ''; try{ script = (reg.active && reg.active.scriptURL) || (reg.waiting && reg.waiting.scriptURL) || (reg.installing && reg.installing.scriptURL) || ''; }catch(e){} const isLegacy = LEGACY_SW_PATTERNS.some(rx => rx.test(script)) && !script.includes('entity=pwa&page=sw&v=33_3_5'); if(isLegacy){ try{ if(await reg.unregister()) removed.push(script || reg.scope); }catch(e){} } } return removed; } async function registerCurrentWorker(){ if(!('serviceWorker' in navigator) || location.protocol !== 'https:') return null; const swUrl = new URL(TARGET_SW, document.baseURI).href; const reg = await navigator.serviceWorker.register(swUrl, { scope: './', updateViaCache: 'none' }); try{ await reg.update(); }catch(e){} return reg; } function migrationStamp(){ try{ return localStorage.getItem('pwa_migrated_to') || ''; }catch(e){ return ''; } } function saveMigrationStamp(){ try{ localStorage.setItem('pwa_migrated_to', TARGET_VERSION); }catch(e){} } async function migrate(){ if(location.protocol !== 'https:' || !('serviceWorker' in navigator)) return; const stamp = migrationStamp(); const regs = await navigator.serviceWorker.getRegistrations(); const hasLegacy = regs.some(reg => { let script=''; try{ script=(reg.active&®.active.scriptURL) ||(reg.waiting&®.waiting.scriptURL) ||(reg.installing&®.installing.scriptURL) ||''; }catch(e){} return LEGACY_SW_PATTERNS.some(rx=>rx.test(script)) && !script.includes('entity=pwa&page=sw&v=33_3_5'); }); if(stamp === TARGET_VERSION && !hasLegacy){ try{ await registerCurrentWorker(); }catch(e){} return; } try{ await unregisterLegacyWorkers(); }catch(e){} try{ await clearLegacyCaches(); }catch(e){} try{ const reg = await registerCurrentWorker(); saveMigrationStamp(); // If there is a waiting worker, activate it immediately. if(reg && reg.waiting){ try{ reg.waiting.postMessage({type:'SKIP_WAITING'}); }catch(e){} } // One controlled refresh maximum, after migration. try{ if(navigator.serviceWorker.controller){ const key='pwa_migration_reload_33_3_5'; if(sessionStorage.getItem(key)!=='1'){ sessionStorage.setItem(key,'1'); setTimeout(()=>location.reload(),350); } } }catch(e){} }catch(err){ console.warn('PWA migration v33.3.5:', err); } } ready(migrate); })();