2023-01-21 09:54:08 -08:00
|
|
|
const OFFLINE_CACHE_NAME = "offline";
|
|
|
|
// Bumping this version will force an update of the service worker
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
const OFFLINE_PAGE_VERSION = 1;
|
|
|
|
const OFFLINE_CACHE_PATHS = [
|
|
|
|
"/pwa_offline.html",
|
|
|
|
"/images/errors/small/404.png",
|
|
|
|
"/images/errors/medium/404.png",
|
|
|
|
"/images/errors/original/404.png"
|
|
|
|
];
|
|
|
|
|
2022-09-11 11:42:40 -07:00
|
|
|
self.addEventListener('push', function (event) {
|
|
|
|
if (event.data) {
|
|
|
|
const notification = event.data.json();
|
|
|
|
|
|
|
|
event.waitUntil(self.registration.showNotification(notification.title, {
|
2022-09-11 14:55:06 -07:00
|
|
|
body: notification.body,
|
2022-12-24 12:11:30 -08:00
|
|
|
tag: notification.type,
|
2022-12-25 16:19:52 -08:00
|
|
|
icon: notification.icon,
|
2022-09-11 11:42:40 -07:00
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
console.error("Push event received, but it didn't contain any data.", event);
|
|
|
|
}
|
|
|
|
});
|
2022-09-11 14:55:06 -07:00
|
|
|
|
|
|
|
self.addEventListener('notificationclick', async event => {
|
|
|
|
if (event.notification.tag === 'inbox') {
|
|
|
|
event.preventDefault();
|
2022-12-26 03:09:25 -08:00
|
|
|
return clients.openWindow("/inbox", "_blank").then(result => {
|
|
|
|
event.notification.close();
|
|
|
|
return result;
|
|
|
|
});
|
2022-09-11 14:55:06 -07:00
|
|
|
} else {
|
|
|
|
console.warn(`Unhandled notification tag: ${event.notification.tag}`);
|
|
|
|
}
|
|
|
|
});
|
2023-01-21 09:54:08 -08:00
|
|
|
|
|
|
|
self.addEventListener('install', function (event) {
|
|
|
|
event.waitUntil(
|
|
|
|
(async () => {
|
|
|
|
const cache = await caches.open(OFFLINE_CACHE_NAME);
|
|
|
|
await cache.addAll(OFFLINE_CACHE_PATHS);
|
|
|
|
})()
|
|
|
|
);
|
|
|
|
// Immediately activate new versions of the service worker instead of waiting
|
|
|
|
self.skipWaiting();
|
|
|
|
});
|
|
|
|
|
|
|
|
self.addEventListener('fetch', function (event) {
|
|
|
|
event.respondWith(
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
// Try to load the resource
|
|
|
|
return await fetch(event.request);
|
|
|
|
} catch (error) {
|
|
|
|
// Show an error page if offline
|
|
|
|
console.log("Fetch failed; returning offline page instead.", error);
|
|
|
|
|
|
|
|
const cache = await caches.open(OFFLINE_CACHE_NAME);
|
|
|
|
return await cache.match(OFFLINE_CACHE_PATHS[0]);
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
);
|
|
|
|
});
|