﻿/* global self, caches, fetch, URL, Response */

var prefix = 'udbjorg';

var version = 5;
var previousVersion = version - 1;
var offline_version = "1.0.0";

var CACHE_NAME = `${prefix}-v${version}`;
var PREVIOUS_CACHE_NAME = `${prefix}-v${previousVersion}`;
var OFFLINE_PAGE = `/offline/offline-${offline_version}.html`;

var urlsToCache = [];
var ignoreUrl = function (url) {
    const uri = new URL(url); 
    return false;
};

self.addEventListener('fetch', function(event) {
    if (ignoreUrl(event.request.url) || event.request.method !== 'GET') {
        return;
    }
    
  event.respondWith(
    caches.open(CACHE_NAME).then(function(cache) {
      return cache.match(event.request).then(function(response) {
        var fetchPromise = fetch(event.request).then(function(networkResponse) {
        if(networkResponse.status>400){
            cache.put(event.request, networkResponse.clone());
        }
          return networkResponse;
        })
        return response || fetchPromise;
      })
    })
  );
});


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

self.addEventListener('activate', function (event) {    
    var cacheWhitelist = [CACHE_NAME, PREVIOUS_CACHE_NAME];
    event.waitUntil(
        caches.keys().then(function (cacheNames) {
            return Promise.all(
                cacheNames.map(function (cacheName) {
                    if (cacheWhitelist.indexOf(cacheName) === -1) {
                        return caches.delete(cacheName);
                    }
                })
            );
        }).then(() => self.clients.claim())
    );

});

@@ -86,9 +86,9 @@
 n = 
-5
+6
 ;%0Ava

update version