From 13ec8b0cad41743e171718d5f64feb7381671866 Mon Sep 17 00:00:00 2001 From: Arenukvern Date: Sun, 3 Mar 2024 01:54:50 +0200 Subject: [PATCH] fix: service worker --- release/web/flutter_service_worker.js | 93 ++++++++++++--------------- release/web/index.html | 15 +++-- 2 files changed, 50 insertions(+), 58 deletions(-) diff --git a/release/web/flutter_service_worker.js b/release/web/flutter_service_worker.js index 915727ff..b563fd34 100644 --- a/release/web/flutter_service_worker.js +++ b/release/web/flutter_service_worker.js @@ -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") { diff --git a/release/web/index.html b/release/web/index.html index a4a4c163..b4f32b70 100644 --- a/release/web/index.html +++ b/release/web/index.html @@ -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"); @@ -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); }, });