2021-02-18 03:12:01 +01:00
|
|
|
// caches polyfill because it is not added to native yet!
|
|
|
|
var CACHE_NAME = `${UINAME}-sw`;
|
2021-08-18 20:51:15 +02:00
|
|
|
var debug = process.env.NODE_ENV === "development" ? true : false;
|
2021-02-18 03:12:01 +01:00
|
|
|
var version = `${UIVERSION}-sw`;
|
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
var log = require("../libs/log");
|
|
|
|
var caches = require("../../../thirdparty/serviceworker-caches");
|
2021-02-18 03:12:01 +01:00
|
|
|
|
|
|
|
if (debug) {
|
2021-08-18 20:51:15 +02:00
|
|
|
log("SW: debug is on");
|
2021-02-18 03:12:01 +01:00
|
|
|
log(`SW: CACHE_NAME: ${CACHE_NAME}`);
|
|
|
|
//log(`SW: appDomain: ${appDomain}`);
|
|
|
|
//log(`SW: lang: ${lang}`);
|
|
|
|
}
|
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
if (typeof CACHE_NAME !== "string") {
|
|
|
|
throw new Error("Cache Name cannot be empty");
|
2021-02-18 03:12:01 +01:00
|
|
|
}
|
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
self.addEventListener("fetch", (event) => {
|
2021-02-18 03:12:01 +01:00
|
|
|
// skip non-get
|
2021-08-18 20:51:15 +02:00
|
|
|
if (event.request.method !== "GET") {
|
2021-02-18 03:12:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Parse the url
|
|
|
|
const requestURL = new URL(event.request.url);
|
|
|
|
|
|
|
|
//Check for our own urls
|
|
|
|
/*if (requestURL.origin !== location.origin) {
|
2021-08-09 18:04:04 +02:00
|
|
|
log('SW: skip external ' + event.request.url);
|
|
|
|
return;
|
|
|
|
}*/
|
2021-02-18 03:12:01 +01:00
|
|
|
|
|
|
|
//Skip admin url's
|
|
|
|
if (
|
2021-08-18 20:51:15 +02:00
|
|
|
requestURL.pathname.indexOf("admin") >= 0 ||
|
|
|
|
requestURL.pathname.indexOf("Security") >= 0 ||
|
|
|
|
requestURL.pathname.indexOf("/dev") >= 0
|
2021-02-18 03:12:01 +01:00
|
|
|
) {
|
|
|
|
log(`SW: skip admin ${event.request.url}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Test for images
|
|
|
|
/*if (/\.(jpg|jpeg|png|gif|webp)$/.test(requestURL.pathname)) {
|
2021-08-09 18:04:04 +02:00
|
|
|
log('SW: skip image ' + event.request.url);
|
|
|
|
//For now we skip images but change this later to maybe some caching and/or an offline fallback
|
|
|
|
return;
|
|
|
|
}*/
|
2021-02-18 03:12:01 +01:00
|
|
|
|
|
|
|
// Clone the request for fetch and cache
|
|
|
|
// A request is a stream and can be consumed only once.
|
|
|
|
const fetchRequest = event.request.clone(),
|
|
|
|
cacheRequest = event.request.clone();
|
|
|
|
|
|
|
|
// Respond with content from fetch or cache
|
|
|
|
event.respondWith(
|
|
|
|
// Try fetch
|
|
|
|
fetch(fetchRequest)
|
2021-08-18 20:51:15 +02:00
|
|
|
// when fetch is successful, we update the cache
|
2021-02-18 03:12:01 +01:00
|
|
|
.then((response) => {
|
|
|
|
// A response is a stream and can be consumed only once.
|
|
|
|
// Because we want the browser to consume the response,
|
|
|
|
// as well as cache to consume the response, we need to
|
|
|
|
// clone it so we have 2 streams
|
|
|
|
const responseToCache = response.clone();
|
|
|
|
|
|
|
|
// and update the cache
|
|
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
|
|
// Clone the request again to use it
|
|
|
|
// as the key for our cache
|
|
|
|
const cacheSaveRequest = event.request.clone();
|
|
|
|
cache.put(cacheSaveRequest, responseToCache);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return the response stream to be consumed by browser
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
// when fetch times out or fails
|
2021-02-18 03:12:01 +01:00
|
|
|
.catch((err) => {
|
2021-08-18 20:51:15 +02:00
|
|
|
log("SW: fetch failed");
|
2021-02-18 03:12:01 +01:00
|
|
|
// Return the promise which
|
|
|
|
// resolves on a match in cache for the current request
|
|
|
|
// or rejects if no matches are found
|
|
|
|
return caches.match(cacheRequest);
|
2021-08-18 20:51:15 +02:00
|
|
|
})
|
2021-02-18 03:12:01 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now we need to clean up resources in the previous versions
|
|
|
|
// of Service Worker scripts
|
2021-08-18 20:51:15 +02:00
|
|
|
self.addEventListener("activate", (event) => {
|
2021-02-18 03:12:01 +01:00
|
|
|
log(`SW: activated: ${version}`);
|
|
|
|
// Destroy the cache
|
|
|
|
event.waitUntil(caches.delete(CACHE_NAME));
|
|
|
|
});
|
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
self.addEventListener("install", (e) => {
|
2021-02-18 03:12:01 +01:00
|
|
|
log(`SW: installing version: ${version}`);
|
|
|
|
});
|