From 4c93cf1ab47a973377d50972bb277de1c2c56a78 Mon Sep 17 00:00:00 2001 From: englishextra Date: Wed, 30 Sep 2020 14:47:24 +0300 Subject: [PATCH] updated sw.js sw.min.js pwabuilder-sw.fixed.js as of https://github.com/meanbee/magento-meanbee-pwa/issues/20 --- .../serviceWorker2/src/pwabuilder-sw.fixed.js | 135 ++++++++++++------ 1 file changed, 89 insertions(+), 46 deletions(-) diff --git a/libs/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js b/libs/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js index f039fff5..e93a35cf 100644 --- a/libs/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js +++ b/libs/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js @@ -1,58 +1,101 @@ /*global caches, self, Promise, */ /*! - * @see {@link https://github.com/pwa-builder/pwabuilder-serviceworkers/blob/master/serviceWorker2/pwabuilder-sw.js} - * @see {@link https://github.com/pwa-builder/serviceworkers/pull/28} + * https://github.com/meanbee/magento-meanbee-pwa/issues/20#issuecomment-497626347 + * This is a reworked code of the "Offline copy of pages" service worker */ -//This is the "Offline copy of pages" service worker var cacheName = "pwabuilder-offline"; -//Install stage sets up the index page (home page) in the cache and opens a new cache +var indexPage = "index.html"; +var offlinePages = [indexPage]; + +function updateStaticCache() { + return caches.open(cacheName).then(function (cache) { + console.log("[sw.js] Added to offline during install: " + offlinePages); + return cache.addAll(offlinePages); + }); +} + +function clearOldCaches() { + return caches.keys().then(function (keys) { + return Promise.all(keys.filter(function (key) { + return key.indexOf(cacheName) !== 0; + }).map(function (key) { + return caches.delete (key); + })); + }); +} + +function isHtmlRequest(request) { + return request.headers.get("Accept").indexOf("text/html") !== -1; +} + +function isCachableResponse(response) { + return (response && response.ok); +} + self.addEventListener("install", function (event) { - var indexPage = new Request("index.html"); - event.waitUntil( - fetch(indexPage).then(function (response) { - return caches.open(cacheName).then(function (cache) { - console.log("[sw.js] Cached index page during install: " + response.url); - return cache.put(indexPage, response); - }); + event.waitUntil(updateStaticCache().then(function () { + return self.skipWaiting(); + })); +}); + +self.addEventListener("activate", function (event) { + event.waitUntil(clearOldCaches().then(function () { + return self.clients.claim(); })); }); -//If any fetch fails, it will look for the request in the cache and serve it from there first self.addEventListener("fetch", function (event) { - var updateCache = function (request) { - return caches.open(cacheName).then(function (cache) { - /*! - * @see {@link https://github.com/pwa-builder/serviceworkers/issues/16#issuecomment-388215410} - * @see {@link https://github.com/icarito/serviceworkers/commit/f17f892ba9f2be057aeffa133f01f243604ddc0c} - */ - //return fetch(request).then(function(response) { - return fetch(request, { - credentials: "include", - redirect: "follow" - }).then(function (response) { - console.log("[sw.js] Add page to offline: " + response.url); - return cache.put(request, response); - }); - }); - }; - - event.waitUntil(updateCache(event.request)); - - event.respondWith( - fetch(event.request).catch (function (error) { - console.log("[sw.js] Network request Failed. Serving content from cache: " + error); - - //Check to see if you have it in the cache - //Return response - //If not in the cache, then return error page - return caches.open(cacheName).then(function (cache) { - return cache.match(event.request).then(function (matching) { - var report = !matching || matching.status == 404 ? Promise.reject("no-match") : matching; - return report; + + var request = event.request; + + if (request.method !== "GET") { + if (!navigator.onLine && isHtmlRequest(request)) { + return event.respondWith(caches.match(indexPage)); + } + return; + } + + if (isHtmlRequest(request)) { + + event.respondWith(fetch(request).then(function (response) { + if (isCachableResponse(response)) { + var clonedResponse = response.clone(); + caches.open(cacheName).then(function (cache) { + console.log("[sw.js] Added to offline: " + clonedResponse.url); + return cache.put(request, clonedResponse); + }); + } + return response; + }).catch (function () { + return caches.match(request).then(function (response) { + if (!response && request.mode == "navigate") { + return caches.match(indexPage); + } + return response; }); - }); - }) - ); - }); + })); + } else { + + if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") { + return; + } + + event.respondWith(caches.match(request).then(function (response) { + return response || fetch(request, { + credentials: "include", + redirect: "follow" + }).then(function (response) { + if (isCachableResponse(response)) { + var clonedResponse = response.clone(); + caches.open(cacheName).then(function (cache) { + console.log("[sw.js] Added to offline: " + clonedResponse.url); + return cache.put(request, clonedResponse); + }); + } + return response; + }); + })); + } +});