Skip to content

Commit

Permalink
Chech if fetch event occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesoroka committed Oct 7, 2024
1 parent 69b3ceb commit 447bf53
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 37 deletions.
41 changes: 41 additions & 0 deletions public/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
self.addEventListener("activate", (event) => {
event.waitUntil(self.clients.claim());
console.log("ServiceWorker.js activated");
});

self.addEventListener("message", (event) => {
if (event.data && event.data.type === "TOKEN") {
accessToken = event.data.token;
console.log("token received by Service Worker:", accessToken);
}
});

self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(event.request.url);
console.log("fetch", url);

if (
accessToken &&
url.origin.startsWith("https://") &&
url.origin.endsWith(".ideaconsult.net") &&
request.method === "GET" &&
url.origin !== "https://iam.ideaconsult.net" &&
url.origin !== "https://idp.ideaconsult.net" &&
request.destination === "image" &&
event.request.headers["Authorization"] == undefined
) {
const authRequest = new Request(request, {
headers: new Headers({
...request.headers,
Authorization: `Bearer ${accessToken}`,
}),
mode: "cors",
});
event.respondWith(fetch(authRequest));
console.log("with token");
} else {
event.respondWith(fetch(request));
console.log("without token");
}
});
30 changes: 16 additions & 14 deletions public/worker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
let accessToken = null;

self.addEventListener("install", (event) => {
event.waitUntil(self.skipWaiting());
console.log("Installed worked");

// self.skipWaiting();
});

self.addEventListener("activate", (event) => {
event.waitUntil(clients.claim());
event.waitUntil(self.clients.claim());
console.log("Service worker activated");
});

Expand All @@ -24,18 +26,18 @@ self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(event.request.url);
console.log("fetch", url);
console.log("fetch. Token:", accessToken);
console.log(
"conditions:",
url.origin.startsWith("https://"),
url.origin.endsWith(".ideaconsult.net"),
request.method === "GET",
isGeneratedImage(url),
url.origin !== "https://iam.ideaconsult.net",
url.origin !== "https://idp.ideaconsult.net",
// request.destination === "image",
event.request.headers["Authorization"] == undefined
);
// console.log("fetch. Token:", accessToken);
// console.log(
// "conditions:",
// url.origin.startsWith("https://"),
// url.origin.endsWith(".ideaconsult.net"),
// request.method === "GET",
// isGeneratedImage(url),
// url.origin !== "https://iam.ideaconsult.net",
// url.origin !== "https://idp.ideaconsult.net",
// // request.destination === "image",
// event.request.headers["Authorization"] == undefined
// );

if (
accessToken &&
Expand Down
77 changes: 54 additions & 23 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,63 @@ const Main = () => {
? "https://spectra-dev.adma.ai/search/"
: "http://localhost:5173/search/";

if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register(base_url, { scope: scope_url })
.then((registration) => {
console.log(
"Service Worker registered with scope: ",
registration.scope
);
})
.catch((error) => {
console.log("Service Worker registration failed: ", error);
const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register(
"/serviceWorker.js",
{ scope: scope_url }
);
console.log("scope", registration.scope);

await registration.active.postMessage({
type: "TOKEN",
token: token,
});
});
}
} catch (error) {
console.log(`Registration failed with ${error}`);
}
}
};

useEffect(() => {
registerServiceWorker();
}, [token]);

// if ("serviceWorker" in navigator) {
// window.addEventListener("load", () => {
// navigator.serviceWorker
// .register(base_url)
// .then((registration) => {
// console.log(
// "Service Worker registered with scope: ",
// registration.scope
// );
// if (token && registration.active) {
// console.log("registration.active");

// registration.active.postMessage({
// type: "SET_TOKEN",
// token: token,
// });
// }
// })
// .catch((error) => {
// console.log("ServiceWorker registration failed: ", error);
// });
// });
// }

if (navigator.serviceWorker.controller) {
console.log("post message", navigator);
// if (navigator.serviceWorker.controller) {
// console.log("post message", navigator);

navigator.serviceWorker.controller.postMessage({
type: "SET_TOKEN",
token: token,
});
} else {
console.log("no message", navigator.serviceWorker);
}
// navigator.serviceWorker.controller.postMessage({
// type: "SET_TOKEN",
// token: token,
// });
// } else {
// console.log("no message", navigator.serviceWorker);
// }

return <></>;
};
Expand Down

0 comments on commit 447bf53

Please sign in to comment.