Skip to content

Commit

Permalink
check if fetch event is triggered
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesoroka committed Oct 8, 2024
1 parent d172715 commit 2b03aad
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions public/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
self.addEventListener("install", (event) => {
self.skipWaiting();
console.log("Service Worker is installed");
});

self.addEventListener("activate", (event) => {
Expand All @@ -14,32 +15,42 @@ self.addEventListener("message", (event) => {
}
});

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

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");
}
self.addEventListener("fetch", (event) => {
console.log("Fetch event for", event.request.url);
event.respondWith(fetch(event.request));
});

// 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");
// }
// });

0 comments on commit 2b03aad

Please sign in to comment.