From 47b601f6727348dba7d563f5a2ba987ddaf826b5 Mon Sep 17 00:00:00 2001 From: englishextra Date: Wed, 30 Sep 2020 14:45:26 +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 | 131 +++++++++------ sw.js | 149 +++++++++++------- sw.min.js | 2 +- 3 files changed, 173 insertions(+), 109 deletions(-) diff --git a/cdn/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js b/cdn/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js index e6aab06..e93a35c 100644 --- a/cdn/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js +++ b/cdn/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js @@ -1,66 +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)); - - /*! - * https://github.com/meanbee/magento-meanbee-pwa/issues/20#issuecomment-497626347 - */ + var request = event.request; - if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") { + if (request.method !== "GET") { + if (!navigator.onLine && isHtmlRequest(request)) { + return event.respondWith(caches.match(indexPage)); + } return; } - event.respondWith( - fetch(event.request).catch (function (error) { - console.log("[sw.js] Network request Failed. Serving content from cache: " + error); + if (isHtmlRequest(request)) { - //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; + 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; + }); + })); + } +}); diff --git a/sw.js b/sw.js index d75d625..14266eb 100644 --- a/sw.js +++ b/sw.js @@ -1,72 +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 = "lovespy-muicss-offline-v1550084156"; //Install stage sets up the index page (home page) in the cache and opens a new cache -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); - }); - }) - ); -}); //If any fetch fails, it will look for the request in the cache and serve it from there first +var cacheName = "lovespy-muicss-offline-v1550084156"; + +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); + })); + }); +} -self.addEventListener("fetch", function(event) { - var updateCache = function updateCache(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); - }); - }); - }; +function isHtmlRequest(request) { + return request.headers.get("Accept").indexOf("text/html") !== -1; +} - event.waitUntil(updateCache(event.request)); - - /*! - * https://github.com/meanbee/magento-meanbee-pwa/issues/20#issuecomment-497626347 - */ +function isCachableResponse(response) { + return (response && response.ok); +} + +self.addEventListener("install", function (event) { + event.waitUntil(updateStaticCache().then(function () { + return self.skipWaiting(); + })); +}); + +self.addEventListener("activate", function (event) { + event.waitUntil(clearOldCaches().then(function () { + return self.clients.claim(); + })); +}); - if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") { +self.addEventListener("fetch", function (event) { + + var request = event.request; + + if (request.method !== "GET") { + if (!navigator.onLine && isHtmlRequest(request)) { + return event.respondWith(caches.match(indexPage)); + } return; } - - 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; + 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; + }); + })); + } }); diff --git a/sw.min.js b/sw.min.js index 444f517..3d416c0 100644 --- a/sw.min.js +++ b/sw.min.js @@ -1 +1 @@ -var cacheName="lovespy-muicss-offline-v1550084156";self.addEventListener("install",function(e){var t=new Request("index.html");e.waitUntil(fetch(t).then(function(n){return caches.open(cacheName).then(function(e){return console.log("[sw.js] Cached index page during install: "+n.url),e.put(t,n)})}))}),self.addEventListener("fetch",function(n){var t;n.waitUntil((t=n.request,caches.open(cacheName).then(function(n){return fetch(t,{credentials:"include",redirect:"follow"}).then(function(e){return console.log("[sw.js] Add page to offline: "+e.url),n.put(t,e)})}))),"only-if-cached"===n.request.cache&&"same-origin"!==n.request.mode||n.respondWith(fetch(n.request).catch(function(e){return console.log("[sw.js] Network request Failed. Serving content from cache: "+e),caches.open(cacheName).then(function(e){return e.match(n.request).then(function(e){return e&&404!=e.status?e:Promise.reject("no-match")})})}))}); \ No newline at end of file +var cacheName="lovespy-muicss-offline-v1550084156",indexPage="index.html",offlinePages=[indexPage];function updateStaticCache(){return caches.open(cacheName).then(function(e){return console.log("[sw.js] Added to offline during install: "+offlinePages),e.addAll(offlinePages)})}function clearOldCaches(){return caches.keys().then(function(e){return Promise.all(e.filter(function(e){return 0!==e.indexOf(cacheName)}).map(function(e){return caches.delete(e)}))})}function isHtmlRequest(e){return-1!==e.headers.get("Accept").indexOf("text/html")}function isCachableResponse(e){return e&&e.ok}self.addEventListener("install",function(e){e.waitUntil(updateStaticCache().then(function(){return self.skipWaiting()}))}),self.addEventListener("activate",function(e){e.waitUntil(clearOldCaches().then(function(){return self.clients.claim()}))}),self.addEventListener("fetch",function(e){var t=e.request;if("GET"!==t.method)return!navigator.onLine&&isHtmlRequest(t)?e.respondWith(caches.match(indexPage)):void 0;if(isHtmlRequest(t))e.respondWith(fetch(t).then(function(e){var n;return isCachableResponse(e)&&(n=e.clone(),caches.open(cacheName).then(function(e){return console.log("[sw.js] Added to offline: "+n.url),e.put(t,n)})),e}).catch(function(){return caches.match(t).then(function(e){return e||"navigate"!=t.mode?e:caches.match(indexPage)})}));else{if("only-if-cached"===e.request.cache&&"same-origin"!==e.request.mode)return;e.respondWith(caches.match(t).then(function(e){return e||fetch(t,{credentials:"include",redirect:"follow"}).then(function(e){var n;return isCachableResponse(e)&&(n=e.clone(),caches.open(cacheName).then(function(e){return console.log("[sw.js] Added to offline: "+n.url),e.put(t,n)})),e})}))}}); \ No newline at end of file