From 00cdab09547f72ec0d7844930a8ff92d042c4a73 Mon Sep 17 00:00:00 2001 From: Piotr Pospiech <55554468+Piotr20@users.noreply.github.com> Date: Fri, 11 Mar 2022 12:13:09 +0100 Subject: [PATCH 1/2] fix_minimal --- lesson-06/service-wookiee/main.js | 26 ++++++++++++++++++++++++++ lesson-06/service-wookiee/sw.js | 12 ++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 lesson-06/service-wookiee/sw.js diff --git a/lesson-06/service-wookiee/main.js b/lesson-06/service-wookiee/main.js index d3c8b4e..e4f0835 100644 --- a/lesson-06/service-wookiee/main.js +++ b/lesson-06/service-wookiee/main.js @@ -1,6 +1,32 @@ const template = document.querySelector("#template").content; const boxes = document.querySelector("#boxes"); +if ("serviceWorker" in navigator) { + window.addEventListener("load", function () { + navigator.serviceWorker.register("/sw.js").then( + function (registration) { + // Registration was successful + console.log( + "ServiceWorker registration successful with scope: ", + registration.scope + ); + }, + function (err) { + // registration failed :( + console.log("ServiceWorker registration failed: ", err); + } + ); + }); +} + +let counts = 0; + +navigator.serviceWorker.addEventListener("message", (e) => { + console.log(e.data); + counts++; + document.querySelector("#request-count").innerHTML = counts; +}); + for (const fileType of ["text/css", "text/html", "application/json"]) { // Clone the template for each box const box = template.cloneNode(true); 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"); + } +}); From 393a53e244333d7eda6a2899b73b168abbf8458b Mon Sep 17 00:00:00 2001 From: Piotr Pospiech <55554468+Piotr20@users.noreply.github.com> Date: Mon, 14 Mar 2022 10:28:10 +0100 Subject: [PATCH 2/2] fix fridat --- lesson-06/service-wookiee/main.js | 1 - lesson-06/service-wookiee/sw.js | 36 +++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lesson-06/service-wookiee/main.js b/lesson-06/service-wookiee/main.js index e4f0835..ef9c810 100644 --- a/lesson-06/service-wookiee/main.js +++ b/lesson-06/service-wookiee/main.js @@ -22,7 +22,6 @@ if ("serviceWorker" in navigator) { let counts = 0; navigator.serviceWorker.addEventListener("message", (e) => { - console.log(e.data); counts++; document.querySelector("#request-count").innerHTML = counts; }); diff --git a/lesson-06/service-wookiee/sw.js b/lesson-06/service-wookiee/sw.js index 98db273..156685a 100644 --- a/lesson-06/service-wookiee/sw.js +++ b/lesson-06/service-wookiee/sw.js @@ -3,10 +3,42 @@ self.addEventListener("install", async (e) => { }); 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"); } }); + +self.addEventListener("fetch", async (e) => { + console.log(e.request.url); + switch (e.request.url.split("/").pop()) { + case "fake.css": + e.respondWith( + new Response("css", { + status: 200, + statusText: `You tried to fetch ${e.request.destination}`, + }) + ); + break; + case "fake.html": + e.respondWith( + new Response("htnl", { + status: 200, + statusText: `You tried to fetch ${e.request.destination}`, + }) + ); + break; + case "fake.json": + e.respondWith( + new Response("json", { + status: 200, + statusText: `You tried to fetch ${e.request.destination}`, + }) + ); + break; + + default: + "Request failed"; + break; + } +});