75 lines
2.6 KiB
JavaScript
75 lines
2.6 KiB
JavaScript
// Network-first service worker: always try GitHub Pages for the latest
|
|
// version, fall back to the cached copy when offline.
|
|
var CACHE = 'brainfeed-v25';
|
|
var ASSETS = [
|
|
'./',
|
|
'./index.html',
|
|
'./supabase.js',
|
|
'./manifest.json',
|
|
'./icon-192.png',
|
|
'./icon-512.png',
|
|
'./apple-touch-icon.png',
|
|
'./favicon-32.png'
|
|
];
|
|
|
|
self.addEventListener('install', function(e){
|
|
e.waitUntil(
|
|
caches.open(CACHE)
|
|
.then(function(c){ return c.addAll(ASSETS); })
|
|
.then(function(){ return self.skipWaiting(); })
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', function(e){
|
|
e.waitUntil(
|
|
caches.keys().then(function(keys){
|
|
// Only this app's caches: every app on kburchtree.cloud shares one cache store.
|
|
return Promise.all(keys.filter(function(k){ return k.indexOf('brainfeed-') === 0 && k !== CACHE; })
|
|
.map(function(k){ return caches.delete(k); }));
|
|
}).then(function(){ return self.clients.claim(); })
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', function(e){
|
|
if(e.request.method !== 'GET') return;
|
|
// Supabase API/storage requests pass through untouched.
|
|
if(new URL(e.request.url).origin !== self.location.origin) return;
|
|
e.respondWith(
|
|
fetch(e.request).then(function(resp){
|
|
var copy = resp.clone();
|
|
caches.open(CACHE).then(function(c){ c.put(e.request, copy); });
|
|
return resp;
|
|
}).catch(function(){
|
|
return caches.match(e.request, { ignoreSearch: true }).then(function(hit){
|
|
return hit || caches.match('./index.html');
|
|
});
|
|
})
|
|
);
|
|
});
|
|
|
|
// Push notifications (reminders from the brainfeed-push function). The payload is
|
|
// {title, body, tag, url}; tapping opens or focuses BrainFeed at that URL.
|
|
self.addEventListener('push', function(e){
|
|
var d = {};
|
|
try { d = e.data ? e.data.json() : {}; } catch (_) { d = { body: e.data ? e.data.text() : '' }; }
|
|
e.waitUntil(self.registration.showNotification(d.title || 'BrainFeed', {
|
|
body: d.body || '', tag: d.tag || undefined, icon: 'icon-192.png', badge: 'favicon-32.png',
|
|
data: { url: d.url || './' }
|
|
}));
|
|
});
|
|
|
|
self.addEventListener('notificationclick', function(e){
|
|
e.notification.close();
|
|
var url = new URL((e.notification.data && e.notification.data.url) || './', self.registration.scope).href;
|
|
e.waitUntil(self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function(list){
|
|
for (var i = 0; i < list.length; i++) {
|
|
var c = list[i];
|
|
if (c.url.indexOf(self.registration.scope) === 0 && 'focus' in c) {
|
|
c.postMessage({ type: 'open', url: url });
|
|
return c.focus();
|
|
}
|
|
}
|
|
return self.clients.openWindow(url);
|
|
}));
|
|
});
|