Skip to content

Commit

Permalink
fix: service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Arenukvern committed Mar 2, 2024
1 parent af884e1 commit 13ec8b0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 58 deletions.
93 changes: 42 additions & 51 deletions release/web/flutter_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,60 +244,51 @@ self.addEventListener("activate", function (event) {
// The fetch handler redirects requests for RESOURCE files to the service
// worker cache.
self.addEventListener("fetch", (event) => {
try {
console.log({ "dTADA fetch": event.request.method });
if (event.request.method !== "GET") {
return;
}
var origin = self.location.origin;
var key = event.request.url.substring(origin.length + 1);
// Redirect URLs to the index.html
if (key.indexOf("?v=") != -1) {
key = key.split("?v=")[0];
}
if (
event.request.url == origin ||
event.request.url.startsWith(origin + "/#") ||
event.request.url.startsWith(origin + "/#/home") ||
key == ""
) {
key = "/";
}
console.log({ "TADA: resource ": !RESOURCES[key] });
// If the URL is not the RESOURCE list then return to signal that the
// browser should take over.
if (!RESOURCES[key]) {
return;
}
// If the URL is the index.html, perform an online-first request.
if (key == "/") {
console.log("key / ");
return onlineFirst(event);
}
event.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(event.request).then((response) => {
console.log({ "cache resopnse / ": response != null });
// Either respond with the cached resource, or perform a fetch and
// lazily populate the cache only if the resource was successfully fetched.
return (
response ||
fetch(event.request).then((response) => {
if (response && Boolean(response.ok)) {
cache.put(event.request, response.clone());
}
return response;
})
);
});
})
);
} catch (error) {
console.error("OOOPES", error);
if (event.request.method !== "GET") {
return;
}
var origin = self.location.origin;
var key = event.request.url.substring(origin.length + 1);
// Redirect URLs to the index.html
if (key.indexOf("?v=") != -1) {
key = key.split("?v=")[0];
}
if (
event.request.url == origin ||
event.request.url.startsWith(origin + "/#") ||
key == ""
) {
key = "/";
}
// If the URL is not the RESOURCE list then return to signal that the
// browser should take over.
if (!RESOURCES[key]) {
return;
}
// If the URL is the index.html, perform an online-first request.
if (key == "/") {
return onlineFirst(event);
}
event.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(event.request).then((response) => {
console.log({ "cache resopnse / ": response != null });
// Either respond with the cached resource, or perform a fetch and
// lazily populate the cache only if the resource was successfully fetched.
return (
response ||
fetch(event.request).then((response) => {
if (response && Boolean(response.ok)) {
cache.put(event.request, response.clone());
}
return response;
})
);
});
})
);
});
self.addEventListener("message", (event) => {
console.log({ "TADA: message": event.data });
// SkipWaiting can be used to immediately activate a waiting service worker.
// This will also require a page refresh triggered by the main worker.
if (event.data === "skipWaiting") {
Expand Down
15 changes: 8 additions & 7 deletions release/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
navigator.userAgent
);
}
if (navigator.serviceWorker) {
console.log("SW found");
navigator.serviceWorker.ready.then((registration) => {
console.log("requesting offline resources");
registration.active.postMessage("downloadOffline");
});
}
var loading = document.querySelector("#loading");
var loadingContainer = document.querySelector("#loading_container");
var hostElement = document.querySelector("#flutter_app");
Expand All @@ -93,18 +100,12 @@
hostElement: hostElement,
});

console.log("appRunner");
console.log("engine initialized");
loading.classList.add("init_done");
await appRunner.runApp();
console.log("runApp - done");
window.setTimeout(function () {
console.log("removing");
loadingContainer.remove();
if (navigator.serviceWorker) {
navigator.serviceWorker.ready.then((registration) => {
registration.active.postMessage("downloadOffline");
});
}
}, 200);
},
});
Expand Down

0 comments on commit 13ec8b0

Please sign in to comment.