All files / app/web-ui/scripts service-worker.ts

100% Statements 80/80
100% Branches 1/1
100% Functions 1/1
100% Lines 80/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 801x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x
export const serviceWorker = (basePath: string) => `
/**
 * Service Worker for iCloud Photos Sync Web UI
 * Caches static assets and handles push notifications
 */
self.addEventListener("install", (_event) => {
    console.log("Service_Worker_Installing...")
    self.skipWaiting();
});
 
self.addEventListener("activate", (event) => {
    console.log("Service_Worker_Activated");
    event.waitUntil(self.clients.claim());
});
 
self.addEventListener("fetch", (event) => {
    // only cache requests that don't contain /api/ in the path
    if (event.request.url.includes("/api/")) return;
 
    // Use a "State while revalidate" strategy to cache HTML pages
    event.respondWith(
        caches.open("offline").then((cache) => {
            return cache.match(event.request).then((cachedResponse) => {
                const networkResponse = fetch(${basePath}event.request).then((networkResponse) => {
                    cache.put(${basePath}event.request, networkResponse.clone());
                    return networkResponse;
                });
                return cachedResponse || networkResponse;
            });
        })
    );
});
 
self.addEventListener("push", (event) => {
    console.log("Push notification received:", event);
    const data = event.data?.json() ?? {
        state: "ready",
        prevError: {
            message: "Failed to retrieve push notification from pushed data!"
        }
    };
 
    console.log('Presenting notification: ' + JSON.stringify(data))
 
    let title = "iCloud Photo Sync"
    let body = ""
    let badge = 0
 
    switch(data.state) {
        case 'ready':
            if(data.prevError) {
                title = "iCloud Photo Sync failed"
                body = "Last " + (data.prevTrigger ?? "operation") + " failed: " + data.prevError.message
            } else {
                title = "iCloud Photo Sync succeeded"
                body = "Last " + (data.prevTrigger ?? "operation") + " succeeded!"
            }
            if(data.nextSync) {
                body += " Next sync scheduled for " + new Date(data.nextSync).toLocaleString()
            }
            break;
        case 'mfa_required':
            title = "iCloud Photo Sync requires MFA code"
            body = "In order to continue operation, please provide a valid MFA code"
            badge = 1
            break;
        default:
        case 'syncing':
        case 'authenticating':
            break;
    }
 
    self.registration.showNotification(title, {
        body,
        icon: "/icon.png"
    });
 
    Navigator.setAppBadge(badge);
});
`;