SHARE
TWEET

service-worker.js

heri-code Dec 8th, 2019 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const CACHE_NAME = 'firstpwa-2';
  2. var urlsToCache = [
  3.     '/',
  4.     '/nav.html',
  5.     '/index.html',
  6.     '/pages/home.html',
  7.     '/pages/bundesliga.html',
  8.     '/css/materialize.min.css',
  9.     '/js/materialize.min.js',
  10.     '/js/script.js',
  11.     '/js/api.js',
  12.     '/images/2002.png',
  13.     '/icon.png'
  14. ];
  15.  
  16. self.addEventListener('install', function(event){
  17.  
  18.     event.waitUntil(
  19.         caches.open(CACHE_NAME)
  20.         .then(function(cache) {
  21.             return cache.addAll(urlsToCache);
  22.         })
  23.     );
  24. })
  25.  
  26. self.addEventListener('activate', function(event){
  27.     event.waitUntil(
  28.         caches.keys()
  29.         .then(function(cacheNames) {
  30.             return Promise.all(
  31.                 cacheNames.map(function(cacheName){
  32.                     if(cacheName != CACHE_NAME){   
  33.                         console.log("ServiceWorker: cache " + cacheName + " dihapus");
  34.                         return caches.delete(cacheName);
  35.                     }
  36.                 })
  37.             );
  38.         })
  39.     );
  40. })
  41.  
  42. self.addEventListener("fetch", function(event){
  43.     var base_url="https://api.football-data.org/"
  44.     if (event.request.url.indexOf(base_url)> -1){
  45.         event.respondWith(
  46.             caches.open(CACHE_NAME).then(function(cache){
  47.                 return fetch(event.request).then(function(response){
  48.                     cache.put(event.request.url, response.clone());
  49.                 })
  50.             })
  51.         );
  52.     }else{
  53.         event.respondWith(
  54.             caches.match(event.request, {ignoreSearch:true}).then(function(response){
  55.                 return response || fetch (event.request);
  56.             })
  57.         )
  58.     }
  59. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top