+ <span id="DefaultAppWorkerJS">DefaultAppWorkerJS</span> = "// -----------------------------------------------------------------------------\n// PWA\n// -----------------------------------------------------------------------------\nconst cacheName = \"app-\" + \"{{.Version}}\";\nconst resourcesToCache = {{.ResourcesToCache}};\n\nself.addEventListener(\"install\", async (event) => {\n try {\n console.log(\"installing app worker {{.Version}}\");\n await installWorker();\n await self.skipWaiting();\n } catch (error) {\n console.error(\"error during installation:\", error);\n }\n});\n\nasync function installWorker() {\n const cache = await caches.open(cacheName);\n await cache.addAll(resourcesToCache);\n}\n\nself.addEventListener(\"activate\", async (event) => {\n try {\n await deletePreviousCaches(); // Await cache cleanup\n await self.clients.claim(); // Ensure the service worker takes control of the clients\n console.log(\"app worker {{.Version}} is activated\");\n } catch (error) {\n console.error(\"error during activation:\", error);\n }\n});\n\nasync function deletePreviousCaches() {\n const keys = await caches.keys();\n await Promise.all(\n keys.map(async (key) => {\n if (key !== cacheName) {\n try {\n console.log(\"deleting\", key, \"cache\");\n await caches.delete(key);\n } catch (err) {\n console.error(\"deleting\", key, \"cache failed:\", err);\n }\n }\n })\n );\n}\n\nself.addEventListener(\"fetch\", (event) => {\n event.respondWith(fetchWithCache(event.request));\n});\n\nasync function fetchWithCache(request) {\n const cachedResponse = await caches.match(request);\n if (cachedResponse) {\n return cachedResponse;\n }\n return await fetch(request);\n}\n\n// -----------------------------------------------------------------------------\n// Push Notifications\n// -----------------------------------------------------------------------------\nself.addEventListener(\"push\", (event) => {\n event.waitUntil((async () => {\n let notification;\n\n try {\n notification = event.data ? event.data.json() : null;\n } catch {\n notification = null;\n }\n\n if (!notification) {\n return;\n }\n\n await showNotification(self.registration, notification);\n })());\n});\n\nself.addEventListener(\"message\", (event) => {\n const msg = event.data;\n if (!msg || msg.type !== \"goapp:notify\") {\n return;\n }\n\n event.waitUntil(\n showNotification(self.registration, msg.options)\n );\n});\n\nasync function showNotification(registration, notification) {\n const title = notification.title || \"Notification\";\n\n let actions = [];\n for (let i in notification.actions) {\n const action = notification.actions[i];\n actions.push({\n action: action.action,\n path: action.path,\n });\n delete action.path;\n }\n\n await registration.showNotification(title, {\n body: notification.body,\n icon: notification.icon,\n badge: notification.badge,\n actions: notification.actions,\n data: {\n goapp: {\n path: notification.path,\n actions: actions\n }\n }\n });\n}\n\nself.addEventListener(\"notificationclick\", (event) => {\n event.notification.close();\n\n const notification = event.notification;\n let path = notification.data.goapp.path;\n\n for (let i in notification.data.goapp.actions) {\n const action = notification.data.goapp.actions[i];\n if (action.action === event.action) {\n path = action.path;\n break;\n }\n }\n\n event.waitUntil(\n clients\n .matchAll({\n type: \"window\",\n })\n .then((clientList) => {\n for (var i = 0; i < clientList.length; i++) {\n let client = clientList[i];\n if (\"focus\" in client) {\n client.focus();\n client.postMessage({\n goapp: {\n type: \"notification\",\n path: path,\n },\n });\n return;\n }\n }\n\n if (clients.openWindow) {\n return clients.openWindow(path);\n }\n })\n );\n});"
0 commit comments