hubl/src/sw.js

115 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-02-08 13:25:02 +00:00
const CACHE_NAME = 'hubl-store-cug7431';
2020-10-28 16:16:34 +00:00
2020-08-24 12:50:22 +00:00
self.addEventListener('install', function (e) {
2020-10-28 16:38:54 +00:00
self.skipWaiting();
2020-08-24 12:50:22 +00:00
e.waitUntil(
2020-10-28 16:16:34 +00:00
caches.open(CACHE_NAME).then(function (cache) {
2020-08-24 12:50:22 +00:00
return cache.addAll([
2020-10-28 11:27:10 +00:00
'/locales/es.json',
'/locales/fr.json',
2020-08-24 12:50:22 +00:00
'/scripts/index.js',
'/syles/index.css',
'/index.html',
'/'
]);
2020-10-28 16:38:54 +00:00
})
2020-10-28 16:31:43 +00:00
);
2020-08-24 12:50:22 +00:00
});
2020-10-28 16:16:34 +00:00
self.addEventListener('activate', function (e) {
// invalidate older versions
e.waitUntil(
caches.keys()
.then(function (keyList) {
return Promise.all(keyList.map(function (key) {
if (key !== CACHE_NAME && key !== (CACHE_NAME + "-cdn") && key !== (CACHE_NAME + "-api")) {
return caches.delete(key);
}
}));
}));
self.clients.claim();
});
2020-12-14 10:24:35 +00:00
if(process.env.NODE_ENV === 'production'){
self.addEventListener('fetch', function (event) {
let requestURL = new URL(event.request.url);
if (requestURL.origin == location.origin) {
// Static asset, cache then network
event.respondWith(
caches.open(CACHE_NAME).then(function (cache) {
return cache.match(event.request).then(function (response) {
var fetchPromise = fetch(event.request).then(function (networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
2020-10-28 16:16:34 +00:00
});
2020-12-14 10:24:35 +00:00
}),
);
2020-10-28 16:16:34 +00:00
} else {
if (
2020-12-14 10:24:35 +00:00
event.request.method == 'POST' ||
event.request.method == 'PUT'
2020-10-28 16:16:34 +00:00
) {
// disabled: lead to cors errors
2020-12-14 10:24:35 +00:00
// // POST/PUT to api, rewrite the cache
2020-10-28 16:16:34 +00:00
// event.respondWith(
2020-12-14 10:24:35 +00:00
// caches.open(CACHE_NAME + '-api').then(function (cache) {
// return fetch(event.request).then(function (response) {
2020-10-28 16:16:34 +00:00
// cache.put(event.request, response.clone());
// return response;
2020-12-14 10:24:35 +00:00
// })
// }));
2020-10-28 16:16:34 +00:00
// api: no cache
event.respondWith(fetch(event.request));
2020-12-14 10:24:35 +00:00
} else if (
/matomo/.test(requestURL.origin) ||
/sentry/.test(requestURL.origin) ||
/jabber/.test(requestURL.origin) ||
/xmpp/.test(requestURL.origin)
) {
// analytics, always distant
event.respondWith(fetch(event.request));
} else {
if (
/unpkg/.test(requestURL.origin) ||
/skypack/.test(requestURL.origin) ||
/jspm/.test(requestURL.origin) ||
/jsdeliver/.test(requestURL.origin) ||
/cdn/.test(requestURL.origin) ||
/googleapis/.test(requestURL.origin)
) {
// cdn: cache then network
event.respondWith(
caches.open(CACHE_NAME + '-cdn').then(function (cache) {
return cache.match(event.request).then(function (response) {
var fetchPromise = fetch(event.request).then(function (networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
});
}),
);
} else {
// disabled: lead to cors errors
// // api: distant then cache
// event.respondWith(
// fetch(event.request)
// .then((response) => {
// caches.open(CACHE_NAME + '-api').then(function (cache) {
// cache.put(event.request, response.clone());
// return response;
// });
// })
// .catch(() => {
// return caches.match(event.request);
// })
// );
// api: no cache
event.respondWith(fetch(event.request));
}
2020-10-28 16:16:34 +00:00
}
}
2020-12-14 10:24:35 +00:00
});
}