Created
February 6, 2021 23:55
-
-
Save moughamir/df782391057295e00d912baa3f36c54f to your computer and use it in GitHub Desktop.
sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/** | |
* Service Worker of MoNetWork | |
*/ | |
const cacheName = "m.monetwork.ma"; | |
const startPage = "https://m.monetwork.ma"; | |
const offlinePage = "https://m.monetwork.ma"; | |
const filesToCache = [startPage, offlinePage]; | |
const neverCacheUrls = []; | |
// Install | |
self.addEventListener("install", function (e) { | |
console.log("MoNetWork service worker installation"); | |
e.waitUntil( | |
caches.open(cacheName).then(function (cache) { | |
console.log("MoNetWork service worker caching dependencies"); | |
filesToCache.map(function (url) { | |
return cache.add(url).catch(function (reason) { | |
return console.log("MoNetWork: " + String(reason) + " " + url); | |
}); | |
}); | |
}) | |
); | |
}); | |
// Activate | |
self.addEventListener("activate", function (e) { | |
console.log("MoNetWork service worker activation"); | |
e.waitUntil( | |
caches.keys().then(function (keyList) { | |
return Promise.all( | |
keyList.map(function (key) { | |
if (key !== cacheName) { | |
console.log("MoNetWork old cache removed", key); | |
return caches.delete(key); | |
} | |
}) | |
); | |
}) | |
); | |
return self.clients.claim(); | |
}); | |
// Fetch | |
self.addEventListener("fetch", function (e) { | |
// Return if the current request url is in the never cache list | |
if (!neverCacheUrls.every(checkNeverCacheList, e.request.url)) { | |
console.log("MoNetWork: Current request is excluded from cache."); | |
return; | |
} | |
// Return if request url protocal isn't http or https | |
if (!e.request.url.match(/^(http|https):\/\//i)) return; | |
// Return if request url is from an external domain. | |
if (new URL(e.request.url).origin !== location.origin) return; | |
// For POST requests, do not use the cache. Serve offline page if offline. | |
if (e.request.method !== "GET") { | |
e.respondWith( | |
fetch(e.request).catch(function () { | |
return caches.match(offlinePage); | |
}) | |
); | |
return; | |
} | |
// Revving strategy | |
if (e.request.mode === "navigate" && navigator.onLine) { | |
e.respondWith( | |
fetch(e.request).then(function (response) { | |
return caches.open(cacheName).then(function (cache) { | |
cache.put(e.request, response.clone()); | |
return response; | |
}); | |
}) | |
); | |
return; | |
} | |
e.respondWith( | |
caches | |
.match(e.request) | |
.then(function (response) { | |
return ( | |
response || | |
fetch(e.request).then(function (response) { | |
return caches.open(cacheName).then(function (cache) { | |
cache.put(e.request, response.clone()); | |
return response; | |
}); | |
}) | |
); | |
}) | |
.catch(function () { | |
return caches.match(offlinePage); | |
}) | |
); | |
}); | |
// Check if current url is in the neverCacheUrls list | |
function checkNeverCacheList(url) { | |
if (this.match(url)) { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment