2019-09-10 06:59:44 +12:00
|
|
|
|
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) {
|
|
|
|
|
for (let item of blacklist) {
|
2019-09-10 06:59:44 +12:00
|
|
|
if (url.indexOf(item) >= 0){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.addEventListener('activate', function(event) {
|
|
|
|
|
event.waitUntil(
|
|
|
|
|
caches.keys().then(cacheNames => {
|
|
|
|
|
return Promise.all(
|
|
|
|
|
cacheNames.filter(cacheName => {
|
|
|
|
|
// Return true if you want to remove this cache,
|
|
|
|
|
// but remember that caches are shared across
|
|
|
|
|
// the whole origin
|
|
|
|
|
return true;
|
|
|
|
|
}).map(cacheName => {
|
|
|
|
|
return caches.delete(cacheName);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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'.
|
|
|
|
|
caches.open('iris').then(cache => {
|
2019-09-11 11:38:45 +12:00
|
|
|
return cache.match(request)
|
2019-09-10 06:59:44 +12: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)
|
|
|
|
|
.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
|
2019-10-16 16:42:38 +13:00
|
|
|
if (!isBlacklisted &&
|
2019-09-11 11:38:45 +12:00
|
|
|
request.method === 'GET' &&
|
|
|
|
|
liveResponse.status >= 200 &&
|
|
|
|
|
liveResponse.status < 400
|
|
|
|
|
) {
|
|
|
|
|
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()
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
throw error;
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
});
|