-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated sw.js sw.min.js pwabuilder-sw.fixed.js as of meanbee/magento-…
- Loading branch information
1 parent
1a00385
commit 07767f9
Showing
3 changed files
with
173 additions
and
109 deletions.
There are no files selected for viewing
131 changes: 83 additions & 48 deletions
131
cdn/pwabuilder-serviceworkers/1.1.1/serviceWorker2/src/pwabuilder-sw.fixed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
})); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "mytushino-muicss-offline-v1550083710"; //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 = "mytushino-muicss-offline-v1550083710"; | ||
|
||
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; | ||
}); | ||
})); | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.