diff --git a/lesson-06/service-wookiee/main.js b/lesson-06/service-wookiee/main.js index d3c8b4e..5213327 100644 --- a/lesson-06/service-wookiee/main.js +++ b/lesson-06/service-wookiee/main.js @@ -1,5 +1,21 @@ const template = document.querySelector("#template").content; const boxes = document.querySelector("#boxes"); +const count = document.querySelector("#request-count"); +let counts = 0; + +if ("serviceWorker" in navigator) { + navigator.serviceWorker.register("sw.js"); +} + +navigator.serviceWorker.addEventListener("message", (e) => { + console.log(e.data); + counter(); +}); + +function counter() { + counts++; + count.innerHTML = counts; +} for (const fileType of ["text/css", "text/html", "application/json"]) { // Clone the template for each box diff --git a/lesson-06/service-wookiee/sw.js b/lesson-06/service-wookiee/sw.js new file mode 100644 index 0000000..98db273 --- /dev/null +++ b/lesson-06/service-wookiee/sw.js @@ -0,0 +1,12 @@ +self.addEventListener("install", async (e) => { + console.log("installed"); +}); + +self.addEventListener("fetch", async (e) => { + console.log("fetched " + e.clientId); + console.log(await self.clients.get(e.clientId)); + let client = await clients.get(e.clientId); + if (client) { + client.postMessage("test"); + } +}); diff --git a/lesson-06/worker-example/index.html b/lesson-06/worker-example/index.html index 3da08e9..7e57b82 100644 --- a/lesson-06/worker-example/index.html +++ b/lesson-06/worker-example/index.html @@ -1,37 +1,20 @@ - -
- - - - + + + + +