Gracefully handle errors when a crosspost can't be retrieved.
[lw2-viewer.git] / www / service-worker.js
blob86a2217d257a178ef3aae96755ac4b237d2b5460
1 let cache = null;
3 self.addEventListener('fetch', function(event) {
4         if(event.request.url.match(/^\/([a-z]*\.(js|css)|css\/|assets\/)/)) {
5                 let responder = cache => {
6                         return cache.match(event.request).then(match => {
7                                 if(match) {
8                                         return match;
9                                 } else {
10                                         return cache.match(event.request, {ignoreSearch: true}).then(match => {
11                                                 if(match) cache.delete(match);
12                                                 
13                                                 return fetch(event.request).then(response => {
14                                                         cache.put(event.request, response.clone());
15                                                         return response;
16                                                 });
17                                         });
18                                 }
19                         });
20                 };
22                 if(cache) {
23                         event.respondWith(responder(cache));
24                 } else {
25                         event.respondWith(
26                                 caches.open("v1").then(openedCache => {
27                                         cache = openedCache;
28                                         return responder(cache);
29                                 })
30                         );
31                 }
32         }
35 let lastNotification = null;
37 self.addEventListener('push', function(event) {
38         event.waitUntil(
39                 fetch("/check-notifications?format=push")
40                         .then(res => res.json())
41                         .then(notifications => {
42                                 if(!notifications) return;
43                                 for(var i=0; i < notifications.length; i++) {
44                                         if(lastNotification === notifications[i]._id) {
45                                                 lastNotification = notifications[i]._id;
46                                                 return;
47                                         }
48                                         self.registration.showNotification(notifications[i].message);
49                                 }
50                         })
51         );
52 });
54 self.addEventListener('notificationclick', function(event) {
55         event.waitUntil(clients.openWindow("/push/go-inbox"));
56 });
58 self.addEventListener('install', (event) => {
59         event.waitUntil(
60                 caches.open("v1").then(openedCache => { cache = openedCache; }).then(self.skipWaiting())
61         );
62 });
64 self.addEventListener('activate', (event) => {
65         event.waitUntil(clients.claim());
66 });