const CACHE = 'gree-controller-__GREE_ASSET_CACHE__'; const SCOPE = new URL(self.registration.scope).pathname.replace(/\/$/, ''); const path = value => `${SCOPE}${value.startsWith('/') ? value : `/${value}`}` || '/'; const ASSETS = [path('__GREE_STYLES_ASSET__'), path('__GREE_APP_ASSET__'), path('__GREE_THEME_INIT_ASSET__'), path('__GREE_LANG_INIT_ASSET__'), path('/manifest.webmanifest')]; self.addEventListener('install', event => event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(ASSETS)).then(() => self.skipWaiting()))); self.addEventListener('activate', event => event.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(key => key !== CACHE).map(key => caches.delete(key)))).then(() => self.clients.claim()))); self.addEventListener('fetch', event => { const url = new URL(event.request.url); if ( event.request.method !== 'GET' || (url.protocol !== 'http:' && url.protocol !== 'https:') || url.origin !== self.location.origin || url.pathname.startsWith(path('/api/')) || url.pathname.startsWith(path('/lang/')) || event.request.mode === 'navigate' ) return; event.respondWith((async () => { const cached = await caches.match(event.request); if (cached) return cached; const response = await fetch(event.request); if (response.ok && response.type === 'basic') { try { const cache = await caches.open(CACHE); await cache.put(event.request, response.clone()); } catch (_) { // Cache failures must not break the network response. } } return response; })()); });