"; html += ""; html += ""; let response = new Response(html, { headers: { "Content-Type": "text/html" }, }); return response; })(), ); } else { e.respondWith( caches.match(e.request).then((response) => { return ( response || fetch(e.request).catch(() => { return caches.match("/offline.html"); }) ); }), ); } }); async function aget_api(url) { //console.log("...try to get data"); try { const response = await fetch(url); if (!response.ok) { throw new Error(`Response status: ${response.status}`); } const json = await response.json(); //console.log("...got data"); return json; } catch (error) { //console.error(error.message); return error; } } self.addEventListener("sync", (e) => { if (e.tag === "sync-posts") { e.waitUntil( // Code to send queued posts to the server send_contact(), ); } }); function send_contact() { console.log("...send_contact"); // Implementation for sending queued posts to the server /* return fetch('/api/send-queued-posts', { method: 'POST', body: JSON.stringify({ posts: queuedPosts }), headers: { 'Content-Type': 'application/json', }, }); */ } self.addEventListener("push", (event) => { const data = event.data.json(); const options = { body: data.body, icon: "/images/icon.png", badge: "/images/badge.png", }; event.waitUntil(self.registration.showNotification(data.title, options)); }); self.addEventListener("notificationclick", (event) => { event.notification.close(); event.waitUntil(clients.openWindow(event.notification.data.url)); }); addEventListener("message", (event) => { message(event); }); async function message(event) { console.log("...got message from App "); console.log(event.data); const client = await self.clients.get(event.source.id); client.postMessage({ msg: "Hello from SW!" }); } ```
https://developer.mozilla.org/en-US/docs/Web/API/Client/postMessage
Anonymous