Skip to content

Instantly share code, notes, and snippets.

@moughamir
Created February 6, 2021 23:55
Show Gist options
  • Save moughamir/df782391057295e00d912baa3f36c54f to your computer and use it in GitHub Desktop.
Save moughamir/df782391057295e00d912baa3f36c54f to your computer and use it in GitHub Desktop.
sw
"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