const CACHE_NAME='news-portal-v1';
const FILES_TO_CACHE=['/','/manifest.json'];

self.addEventListener('install',function(event){
    event.waitUntil(caches.open(CACHE_NAME).then(function(cache){
        return cache.addAll(FILES_TO_CACHE);
    }));
    self.skipWaiting();
});

self.addEventListener('activate',function(event){
    event.waitUntil(caches.keys().then(function(keys){
        return Promise.all(keys.map(function(key){
            if(key!==CACHE_NAME){
                return caches.delete(key);
            }
        }));
    }));
    self.clients.claim();
});

self.addEventListener('fetch',function(event){
    if(event.request.method!=='GET'){
        return;
    }
    event.respondWith(fetch(event.request).catch(function(){
        return caches.match(event.request).then(function(response){
            return response||caches.match('/');
        });
    }));
});