|
3 | 3 | * Handles: |
4 | 4 | * - Offline caching |
5 | 5 | * - Push notifications |
| 6 | + * |
| 7 | + * Notes: |
| 8 | + * - We avoid caching or intercepting dynamic endpoints (/api, /themes) to prevent noisy console logs |
| 9 | + * when responses are 404 (expected in strict checks) or 202 (async endpoints). |
| 10 | + * - We only cache successful GET responses (res.ok). |
6 | 11 | */ |
7 | 12 |
|
| 13 | +const CACHE_NAME = "chamilo-cache-v1" |
| 14 | +const PRECACHE_URLS = [ |
| 15 | + "/", |
| 16 | + "/manifest.json", |
| 17 | + "/img/pwa-icons/icon-192.png", |
| 18 | + "/img/pwa-icons/icon-512.png", |
| 19 | +] |
| 20 | + |
8 | 21 | // PWA: Cache basic files on install |
9 | | -self.addEventListener('install', (event) => { |
10 | | - console.log('[Service Worker] Install event'); |
| 22 | +self.addEventListener("install", (event) => { |
| 23 | + console.log("[Service Worker] Install event") |
11 | 24 |
|
12 | 25 | event.waitUntil( |
13 | | - caches.open('chamilo-cache-v1').then((cache) => { |
14 | | - return cache.addAll([ |
15 | | - '/', |
16 | | - '/manifest.json', |
17 | | - '/img/pwa-icons/icon-192.png', |
18 | | - '/img/pwa-icons/icon-512.png', |
19 | | - ]); |
| 26 | + caches.open(CACHE_NAME).then((cache) => { |
| 27 | + return cache.addAll(PRECACHE_URLS) |
20 | 28 | }) |
21 | | - ); |
22 | | -}); |
23 | | - |
24 | | -//PWA: Serve from cache if available |
25 | | -self.addEventListener('fetch', (event) => { |
26 | | - const url = new URL(event.request.url); |
27 | | - if (url.pathname.startsWith('/r/')) { |
28 | | - return; |
| 29 | + ) |
| 30 | + |
| 31 | + // Activate updated SW ASAP |
| 32 | + self.skipWaiting() |
| 33 | +}) |
| 34 | + |
| 35 | +self.addEventListener("activate", (event) => { |
| 36 | + console.log("[Service Worker] Activate event") |
| 37 | + |
| 38 | + event.waitUntil( |
| 39 | + (async () => { |
| 40 | + // Cleanup old caches if cache name changes in the future |
| 41 | + const keys = await caches.keys() |
| 42 | + await Promise.all( |
| 43 | + keys.map((key) => { |
| 44 | + if (key !== CACHE_NAME) { |
| 45 | + return caches.delete(key) |
| 46 | + } |
| 47 | + return Promise.resolve() |
| 48 | + }) |
| 49 | + ) |
| 50 | + |
| 51 | + await self.clients.claim() |
| 52 | + })() |
| 53 | + ) |
| 54 | +}) |
| 55 | + |
| 56 | +/** |
| 57 | + * Decide whether we should bypass SW for this request. |
| 58 | + * Keep logic explicit and conservative. |
| 59 | + */ |
| 60 | +function shouldBypass(request) { |
| 61 | + const url = new URL(request.url) |
| 62 | + |
| 63 | + // Ignore internal redirect helper routes |
| 64 | + if (url.pathname.startsWith("/r/")) return true |
| 65 | + |
| 66 | + // Avoid intercepting theme assets (prevents console noise for strict 404 checks) |
| 67 | + if (url.pathname.startsWith("/themes/")) return true |
| 68 | + |
| 69 | + // Avoid intercepting API calls (can return 202 or other statuses that shouldn't be cached) |
| 70 | + if (url.pathname.startsWith("/api/")) return true |
| 71 | + |
| 72 | + // Avoid strict probing calls anywhere (expected 404 should not produce SW noise) |
| 73 | + if (url.searchParams.has("strict")) return true |
| 74 | + |
| 75 | + // Do not handle non-GET requests |
| 76 | + if (request.method !== "GET") return true |
| 77 | + |
| 78 | + return false |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Cache-first for static navigations/assets, network fallback. |
| 83 | + * - Only caches successful responses (res.ok). |
| 84 | + * - If network fails, returns cache if available. |
| 85 | + */ |
| 86 | +self.addEventListener("fetch", (event) => { |
| 87 | + const request = event.request |
| 88 | + |
| 89 | + if (shouldBypass(request)) { |
| 90 | + // Let the browser handle it directly |
| 91 | + return |
29 | 92 | } |
30 | 93 |
|
31 | 94 | event.respondWith( |
32 | | - caches.match(event.request).then((cachedResponse) => { |
33 | | - return cachedResponse || fetch(event.request); |
34 | | - }) |
35 | | - ); |
36 | | -}); |
| 95 | + (async () => { |
| 96 | + try { |
| 97 | + const cachedResponse = await caches.match(request) |
| 98 | + if (cachedResponse) { |
| 99 | + return cachedResponse |
| 100 | + } |
| 101 | + |
| 102 | + const response = await fetch(request) |
| 103 | + |
| 104 | + // Cache only successful responses |
| 105 | + if (response && response.ok) { |
| 106 | + const cache = await caches.open(CACHE_NAME) |
| 107 | + await cache.put(request, response.clone()) |
| 108 | + } |
| 109 | + |
| 110 | + return response |
| 111 | + } catch (err) { |
| 112 | + // Network failure: try cache |
| 113 | + const cachedResponse = await caches.match(request) |
| 114 | + if (cachedResponse) { |
| 115 | + return cachedResponse |
| 116 | + } |
| 117 | + |
| 118 | + // As last resort, provide a friendly offline response for navigations |
| 119 | + const accept = request.headers.get("accept") || "" |
| 120 | + if (accept.includes("text/html")) { |
| 121 | + return new Response( |
| 122 | + "<h1>Offline</h1><p>The application is currently offline.</p>", |
| 123 | + { headers: { "Content-Type": "text/html; charset=utf-8" }, status: 503 } |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + // For other assets, just fail gracefully |
| 128 | + return new Response("Offline", { status: 503, statusText: "Service Unavailable" }) |
| 129 | + } |
| 130 | + })() |
| 131 | + ) |
| 132 | +}) |
37 | 133 |
|
38 | 134 | // PUSH NOTIFICATIONS |
39 | | -self.addEventListener('push', function (event) { |
40 | | - console.log('[Service Worker] Push received.'); |
| 135 | +self.addEventListener("push", function (event) { |
| 136 | + console.log("[Service Worker] Push received.") |
41 | 137 |
|
42 | | - let data = {}; |
| 138 | + let data = {} |
43 | 139 | if (event.data) { |
44 | | - data = event.data.json(); |
| 140 | + data = event.data.json() |
45 | 141 | } |
46 | 142 |
|
47 | 143 | const options = { |
48 | | - body: data.message || 'No message payload', |
49 | | - icon: '/img/pwa-icons/icon-192.png', |
| 144 | + body: data.message || "No message payload", |
| 145 | + icon: "/img/pwa-icons/icon-192.png", |
50 | 146 | data: { |
51 | | - url: data.url || '/', |
| 147 | + url: data.url || "/", |
52 | 148 | }, |
53 | | - }; |
| 149 | + } |
54 | 150 |
|
55 | 151 | event.waitUntil( |
56 | | - self.registration.showNotification(data.title || 'Chamilo', options) |
57 | | - ); |
58 | | -}); |
| 152 | + self.registration.showNotification(data.title || "Chamilo", options) |
| 153 | + ) |
| 154 | +}) |
59 | 155 |
|
60 | | -self.addEventListener('notificationclick', function (event) { |
61 | | - event.notification.close(); |
62 | | - const url = event.notification.data.url || '/'; |
63 | | - event.waitUntil( |
64 | | - clients.openWindow(url) |
65 | | - ); |
66 | | -}); |
| 156 | +self.addEventListener("notificationclick", function (event) { |
| 157 | + event.notification.close() |
| 158 | + const url = (event.notification && event.notification.data && event.notification.data.url) || "/" |
| 159 | + |
| 160 | + event.waitUntil(clients.openWindow(url)) |
| 161 | +}) |
0 commit comments