Skip to content

Commit

Permalink
restore all intercepting conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesoroka committed Oct 8, 2024
1 parent 2b03aad commit f8699d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
71 changes: 33 additions & 38 deletions public/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let accessToken = "";

self.addEventListener("install", (event) => {
self.skipWaiting();
console.log("Service Worker is installed");
Expand All @@ -11,46 +13,39 @@ self.addEventListener("activate", (event) => {
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "TOKEN") {
accessToken = event.data.token;
console.log("token received by Service Worker:", accessToken);
console.log(
"Message Event: token received by Service Worker:",
accessToken
);
}
});

// self.addEventListener('fetch', event => {
// console.log('Handling fetch event for', event.request.url);
// event.respondWith(fetch(event.request));
// });

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

// 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");
// }
// });
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");
}
});
8 changes: 2 additions & 6 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ const Main = () => {
const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register(base_url, {
scope: "/search/",
});
const registration = await navigator.serviceWorker.register(base_url);
console.log("scope", registration.scope);

await registration.active.postMessage({
Expand All @@ -49,9 +47,7 @@ const Main = () => {
}
};

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

// if ("serviceWorker" in navigator) {
// window.addEventListener("load", () => {
Expand Down

0 comments on commit f8699d9

Please sign in to comment.