2018-10-20 20:17:13 +00:00
|
|
|
//This is the service worker with the combined offline experience (Offline page + Offline copy of pages)
|
|
|
|
|
|
|
|
//Install stage sets up the offline page in the cache and opens a new cache
|
2021-03-17 02:09:50 +00:00
|
|
|
self.addEventListener('install', function (event) {
|
2018-10-20 20:17:13 +00:00
|
|
|
event.waitUntil(preLoad());
|
|
|
|
});
|
|
|
|
|
2021-09-08 21:55:01 +00:00
|
|
|
const cacheName = "cache-xesite-2.3.0-1";
|
2019-05-22 01:47:09 +00:00
|
|
|
|
2021-03-17 02:09:50 +00:00
|
|
|
var preLoad = function () {
|
|
|
|
console.log('[PWA Builder] Install Event processing');
|
|
|
|
return caches.open(cacheName).then(function (cache) {
|
|
|
|
console.log('[PWA Builder] Cached index and offline page during Install');
|
|
|
|
return cache.addAll(['/blog/', '/blog', '/', '/contact', '/resume', '/talks', '/gallery', '/signalboost']);
|
|
|
|
});
|
2018-11-13 15:44:45 +00:00
|
|
|
};
|
2018-10-20 20:17:13 +00:00
|
|
|
|
2021-03-17 02:09:50 +00:00
|
|
|
self.addEventListener('fetch', function (event) {
|
|
|
|
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('[PWA Builder] The service worker is serving the asset.');
|
|
|
|
event.respondWith(checkResponse(event.request).catch(function () {
|
|
|
|
return returnFromCache(event.request);
|
|
|
|
}));
|
|
|
|
event.waitUntil(addToCache(event.request));
|
2018-10-20 20:17:13 +00:00
|
|
|
});
|
|
|
|
|
2021-03-17 02:09:50 +00:00
|
|
|
var checkResponse = function (request) {
|
|
|
|
return new Promise(function (fulfill, reject) {
|
|
|
|
fetch(request).then(function (response) {
|
|
|
|
if (response.status !== 404) {
|
|
|
|
fulfill(response);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
}, reject);
|
|
|
|
});
|
2018-10-20 20:17:13 +00:00
|
|
|
};
|
|
|
|
|
2021-03-17 02:09:50 +00:00
|
|
|
var addToCache = function (request) {
|
2019-05-22 01:47:09 +00:00
|
|
|
return caches.open(cacheName).then(function (cache) {
|
2018-10-20 20:17:13 +00:00
|
|
|
return fetch(request).then(function (response) {
|
2019-02-03 14:50:38 +00:00
|
|
|
console.log('[PWA Builder] add page to offline: ' + response.url);
|
2018-10-20 20:17:13 +00:00
|
|
|
return cache.put(request, response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-17 02:09:50 +00:00
|
|
|
var returnFromCache = function (request) {
|
2019-05-22 01:47:09 +00:00
|
|
|
return caches.open(cacheName).then(function (cache) {
|
2018-10-20 20:17:13 +00:00
|
|
|
return cache.match(request).then(function (matching) {
|
2021-03-17 02:09:50 +00:00
|
|
|
if (!matching || matching.status == 404) {
|
|
|
|
return cache.match('offline.html');
|
|
|
|
} else {
|
|
|
|
return matching;
|
|
|
|
}
|
2018-10-20 20:17:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|