@ -1,4 +1,3 @@
|
||||
|
||||
const blacklist = [
|
||||
'jamesbarnsley.co.nz',
|
||||
'following/contains',
|
||||
@ -9,55 +8,51 @@ const blacklist = [
|
||||
'refresh_spotify_token'
|
||||
];
|
||||
function inBlacklist(url) {
|
||||
for (let item of blacklist) {
|
||||
if (url.indexOf(item) >= 0){
|
||||
for (const item of blacklist) {
|
||||
if (url.indexOf(item) >= 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
})
|
||||
);
|
||||
caches.keys().then((cacheNames) => {
|
||||
return Promise.all(cacheNames.map((cacheName) => caches.delete(cacheName)));
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', event => {
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const { request } = event;
|
||||
|
||||
event.respondWith(
|
||||
|
||||
// Opens Cache objects that start with 'font'.
|
||||
caches.open('iris').then(cache => {
|
||||
caches.open('iris').then((cache) => {
|
||||
return cache.match(request)
|
||||
.then(response => {
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
|
||||
// Not cached, so we make the request, return that and also save response in cache
|
||||
return fetch(request)
|
||||
.then(liveResponse => {
|
||||
|
||||
.then((liveResponse) => {
|
||||
const isBlacklisted = inBlacklist(request.url);
|
||||
|
||||
// Only cache successful GET requests
|
||||
if (!isBlacklisted &&
|
||||
request.method === 'GET' &&
|
||||
liveResponse.status >= 200 &&
|
||||
liveResponse.status < 400
|
||||
) {
|
||||
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;
|
||||
|
||||
cache.put(request, liveResponse.clone());
|
||||
} else {
|
||||
console.info(`Not caching ${isBlacklisted ? '(blacklisted) ' : ''}${request.method} ${request.url}`);
|
||||
@ -67,9 +62,9 @@ self.addEventListener('fetch', event => {
|
||||
});
|
||||
|
||||
// Exceptions from match() or fetch()
|
||||
}).catch(error => {
|
||||
}).catch((error) => {
|
||||
throw error;
|
||||
});
|
||||
})
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user