Skip to content

Commit

Permalink
updated sw.js sw.min.js pwabuilder-sw.fixed.js as of meanbee/magento-…
Browse files Browse the repository at this point in the history
  • Loading branch information
englishextra committed Sep 30, 2020
1 parent dcf5057 commit 47b601f
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -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;
});
}));
}
});
149 changes: 89 additions & 60 deletions sw.js
Original file line number Diff line number Diff line change
@@ -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;
});
}));
}
});
2 changes: 1 addition & 1 deletion sw.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 47b601f

Please sign in to comment.