2019-09-11 11:38:45 +12:00
|
|
|
const blacklist = [
|
|
|
|
|
'jamesbarnsley.co.nz',
|
2019-09-15 21:11:57 +12:00
|
|
|
'following/contains',
|
|
|
|
|
'followers/contains',
|
2019-09-26 10:22:43 +12:00
|
|
|
'me/tracks',
|
|
|
|
|
'me/albums',
|
2019-10-16 16:42:38 +13:00
|
|
|
'me/following',
|
2019-09-15 21:11:57 +12:00
|
|
|
'refresh_spotify_token'
|
2019-09-10 06:59:44 +12:00
|
|
|
];
|
2019-09-11 11:38:45 +12:00
|
|
|
function inBlacklist(url) {
|
2021-03-23 20:29:31 +13:00
|
|
|
for (const item of blacklist) {
|
|
|
|
|
if (url.indexOf(item) >= 0) {
|
2019-09-10 06:59:44 +12:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-23 20:29:31 +13:00
|
|
|
return false;
|
2019-09-10 06:59:44 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.addEventListener('activate', function(event) {
|
|
|
|
|
event.waitUntil(
|
2021-03-23 20:29:31 +13:00
|
|
|
caches.keys().then((cacheNames) => {
|
|
|
|
|
return Promise.all(cacheNames.map((cacheName) => caches.delete(cacheName)));
|
2019-09-10 06:59:44 +12:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-03-23 20:29:31 +13:00
|
|
|
self.addEventListener('fetch', (event) => {
|
2019-09-11 11:38:45 +12:00
|
|
|
const { request } = event;
|
|
|
|
|
|
2019-09-10 06:59:44 +12:00
|
|
|
event.respondWith(
|
2019-10-16 16:42:38 +13:00
|
|
|
|
2019-09-10 06:59:44 +12:00
|
|
|
// Opens Cache objects that start with 'font'.
|
2021-03-23 20:29:31 +13:00
|
|
|
caches.open('iris').then((cache) => {
|
2019-09-11 11:38:45 +12:00
|
|
|
return cache.match(request)
|
2021-03-23 20:29:31 +13:00
|
|
|
.then((response) => {
|
2019-09-11 11:38:45 +12:00
|
|
|
if (response) {
|
2019-09-10 06:59:44 +12:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not cached, so we make the request, return that and also save response in cache
|
2019-09-11 11:38:45 +12:00
|
|
|
return fetch(request)
|
2021-03-23 20:29:31 +13:00
|
|
|
.then((liveResponse) => {
|
2019-10-16 16:42:38 +13:00
|
|
|
const isBlacklisted = inBlacklist(request.url);
|
|
|
|
|
|
2019-09-11 11:38:45 +12:00
|
|
|
// Only cache successful GET requests
|
2021-03-23 20:29:31 +13:00
|
|
|
if (
|
|
|
|
|
!isBlacklisted
|
|
|
|
|
&& request.method === 'GET'
|
|
|
|
|
&& liveResponse.status >= 200
|
|
|
|
|
&& liveResponse.status < 400
|
|
|
|
|
) {
|
|
|
|
|
// Fixes Edge browser "'chrome-extension' is unsupported" issue
|
|
|
|
|
// See https://stackoverflow.com/questions/49157622/service-worker-typeerror-when-opening-chrome-extension
|
|
|
|
|
if (!/^https?:$/i.test(new URL(request.url).protocol)) return;
|
|
|
|
|
|
2019-09-11 11:38:45 +12:00
|
|
|
cache.put(request, liveResponse.clone());
|
2019-10-16 16:42:38 +13:00
|
|
|
} else {
|
|
|
|
|
console.info(`Not caching ${isBlacklisted ? '(blacklisted) ' : ''}${request.method} ${request.url}`);
|
2019-09-10 06:59:44 +12:00
|
|
|
}
|
2019-09-11 11:38:45 +12:00
|
|
|
|
|
|
|
|
return liveResponse;
|
2019-09-10 06:59:44 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Exceptions from match() or fetch()
|
2021-03-23 20:29:31 +13:00
|
|
|
}).catch((error) => {
|
2019-09-10 06:59:44 +12:00
|
|
|
throw error;
|
|
|
|
|
});
|
2021-03-23 20:29:31 +13:00
|
|
|
}),
|
2019-09-10 06:59:44 +12:00
|
|
|
);
|
2021-03-23 20:29:31 +13:00
|
|
|
});
|