2020-05-09 19:28:18 +03:00
|
|
|
---
|
|
|
|
layout: compress
|
2021-04-19 10:01:07 +03:00
|
|
|
permalink: '/sw.js'
|
2021-01-25 01:20:51 +03:00
|
|
|
# PWA service worker
|
2020-05-09 19:28:18 +03:00
|
|
|
---
|
|
|
|
|
2021-04-10 09:51:04 +03:00
|
|
|
self.importScripts('{{ "/assets/js/data/swcache.js" | relative_url }}');
|
2020-05-09 19:28:18 +03:00
|
|
|
|
2023-09-27 16:13:15 +03:00
|
|
|
const cacheName = 'chirpy-{{ "now" | date: "%s" }}';
|
2021-04-10 09:51:04 +03:00
|
|
|
|
|
|
|
function verifyDomain(url) {
|
2023-09-10 16:45:27 +03:00
|
|
|
for (const domain of allowedDomains) {
|
|
|
|
const regex = RegExp(`^http(s)?:\/\/${domain}\/`);
|
|
|
|
if (regex.test(url)) {
|
|
|
|
return true;
|
2021-04-10 09:51:04 +03:00
|
|
|
}
|
2023-09-10 16:45:27 +03:00
|
|
|
}
|
2021-04-10 09:51:04 +03:00
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
return false;
|
2021-04-10 09:51:04 +03:00
|
|
|
}
|
2020-05-09 19:28:18 +03:00
|
|
|
|
2020-05-11 20:01:12 +03:00
|
|
|
function isExcluded(url) {
|
2023-09-10 16:45:27 +03:00
|
|
|
for (const item of denyUrls) {
|
|
|
|
if (url === item) {
|
|
|
|
return true;
|
2020-05-11 20:01:12 +03:00
|
|
|
}
|
2023-09-10 16:45:27 +03:00
|
|
|
}
|
|
|
|
return false;
|
2020-05-11 20:01:12 +03:00
|
|
|
}
|
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
self.addEventListener('install', (event) => {
|
|
|
|
event.waitUntil(
|
|
|
|
caches.open(cacheName).then((cache) => {
|
|
|
|
return cache.addAll(resource);
|
|
|
|
})
|
|
|
|
);
|
2020-05-09 19:28:18 +03:00
|
|
|
});
|
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
self.addEventListener('activate', (event) => {
|
|
|
|
event.waitUntil(
|
|
|
|
caches.keys().then((keyList) => {
|
|
|
|
return Promise.all(
|
|
|
|
keyList.map((key) => {
|
|
|
|
if (key !== cacheName) {
|
|
|
|
return caches.delete(key);
|
|
|
|
}
|
2022-06-04 18:54:06 +03:00
|
|
|
})
|
2023-09-10 16:45:27 +03:00
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2022-06-04 18:54:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('message', (event) => {
|
2023-09-10 16:45:27 +03:00
|
|
|
if (event.data === 'SKIP_WAITING') {
|
|
|
|
self.skipWaiting();
|
|
|
|
}
|
2022-06-04 18:54:06 +03:00
|
|
|
});
|
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
self.addEventListener('fetch', (event) => {
|
|
|
|
event.respondWith(
|
|
|
|
caches.match(event.request).then((response) => {
|
|
|
|
if (response) {
|
|
|
|
return response;
|
|
|
|
}
|
2021-04-10 09:51:04 +03:00
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
return fetch(event.request).then((response) => {
|
|
|
|
const url = event.request.url;
|
2021-03-21 10:55:04 +03:00
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
if (
|
|
|
|
event.request.method !== 'GET' ||
|
|
|
|
!verifyDomain(url) ||
|
|
|
|
isExcluded(url)
|
|
|
|
) {
|
|
|
|
return response;
|
|
|
|
}
|
2021-04-13 08:18:43 +03:00
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
/* see: <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests> */
|
|
|
|
let responseToCache = response.clone();
|
2021-04-13 08:18:43 +03:00
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
caches.open(cacheName).then((cache) => {
|
|
|
|
/* console.log('[sw] Caching new resource: ' + event.request.url); */
|
|
|
|
cache.put(event.request, responseToCache);
|
|
|
|
});
|
2020-05-09 19:28:18 +03:00
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
return response;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2020-05-09 19:28:18 +03:00
|
|
|
});
|