From d27b317bd8f1621bd2d268ef912fa6dbf7e1e5c3 Mon Sep 17 00:00:00 2001 From: Babek Naghiyev Date: Sun, 10 Jan 2021 06:13:52 +0400 Subject: [PATCH] Edit offline link --- README.md | 8 ++++---- example/sw.js | 6 +++--- lib/index.js | 2 +- package-lock.json | 2 +- package.json | 2 +- src/index.js | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b47af50..21a5a85 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ No need to hard work as ETag or whatever. As easy as drinking a coffee, even eas ``` -importScripts('https://cdn.jsdelivr.net/gh/nagibaba/easy-service-worker@1.2.2/lib/index.js'); +importScripts('https://cdn.jsdelivr.net/gh/nagibaba/easy-service-worker@1.2.3/lib/index.js'); const sw = new EasySW(); @@ -37,8 +37,8 @@ sw.precacheResources = ['/']; sw.cacheGetRequests = false; // offline if not cached -// @default '/offline.html' -sw.offlinePage = '/offline.html'; +// @default '/offline' +sw.offlinePage = '/offline'; // Returns true if you don't want the URL to be cached @@ -98,7 +98,7 @@ Pull requests are welcome. For major changes, please open an issue first to disc npm run prepublish ``` -## Version 1.2.2 +## Version 1.2.3 ## License diff --git a/example/sw.js b/example/sw.js index be6dd1d..b800fe5 100644 --- a/example/sw.js +++ b/example/sw.js @@ -1,5 +1,5 @@ importScripts( - "https://cdn.jsdelivr.net/gh/nagibaba/easy-service-worker@1.2.2/lib/index.js", + "https://cdn.jsdelivr.net/gh/nagibaba/easy-service-worker@1.2.3/lib/index.js", ); const sw = new EasySW(); @@ -15,8 +15,8 @@ sw.precacheResources = ["/"]; sw.cacheGetRequests = false; // offline if not cached -// @default '/offline.html' -sw.offlinePage = "/offline.html"; +// @default '/offline' +sw.offlinePage = "/offline"; // Returns true if you don't want the URL to be cached // @return boolean diff --git a/lib/index.js b/lib/index.js index f3d2723..afe95d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1 +1 @@ -const _self=self;function EasySW(){const mainClass=this;this.CACHENAME="default-easy-cache";this.precacheResources=["/"];this.cacheGetRequests=false;this.offlinePage="/offline.html";this.exclude=(url=>{return false});const precache=()=>{return caches.open(mainClass.CACHENAME).then(function(cache){return cache.addAll([...mainClass.precacheResources,mainClass.offlinePage])})};const refresh=(response,info=null)=>{return _self.clients.matchAll().then(function(clients){clients.forEach(function(client){var message={type:"refresh",url:response.url,info:info};client.postMessage(JSON.stringify(message))})})};const serverReturnedUncachableResponse=(response,info=null)=>{return _self.clients.matchAll().then(function(clients){clients.forEach(function(client){var message={type:"uncachable-response",response:response,info:info};client.postMessage(JSON.stringify(message))})})};const fromCache=request=>{return caches.open(mainClass.CACHENAME).then(function(cache){return cache.match(request).then(function(matching){return matching||fetch(request).catch(error=>{return caches.match(mainClass.offlinePage)})})})};const update=request=>{return caches.open(mainClass.CACHENAME).then(function(cache){return fetch(request).then(function(response){if(response.status<300&&response.type==="basic"){return Promise.all([response.clone(),response.text()])}serverReturnedUncachableResponse(response);return[false,null]}).then(([response,text])=>{if(text){cache.match(request).then(matching=>matching?matching.text():null).then(function(cachedText){cache.put(request,response.clone()).then(function(){if(cachedText&&!stringsAreSame(text,cachedText)){refresh(response,[text.length,cachedText.length])}})})}}).catch(error=>"Network failed")})};const stringsAreSame=(a,b)=>{const cleanedA=a.replace(/[\s\S]*/,"");const cleanedB=b.replace(/[\s\S]*/,"");if(cleanedA.length!==cleanedB.length){return false}return cleanedA.localeCompare(cleanedB)===0};this.init=(()=>{_self.addEventListener("install",function(evt){console.log("The Easy service worker is being installed.");_self.skipWaiting();evt.waitUntil(precache())});_self.addEventListener("activate",function(event){event.waitUntil(caches.keys().then(function(cacheNames){return Promise.all(cacheNames.filter(function(cacheName){return cacheName!==mainClass.CACHENAME}).map(function(cacheName){return caches.delete(cacheName)}))}))});_self.addEventListener("fetch",function(evt){let nonCachable=!(evt.request.url.indexOf("http")===0)||evt.request.destination!=="document"||evt.request.method=="POST"||evt.request.mode==="cors"||/favicon/.test(evt.request.url);if(!mainClass.cacheGetRequests){nonCachable=nonCachable||/[?]/.test(evt.request.url)}if(nonCachable||mainClass.exclude(evt.request.url)){return evt.respondWith(fetch(evt.request).catch(error=>console.log(error)))}evt.respondWith(fromCache(evt.request));evt.waitUntil(update(evt.request))})});this.init()} \ No newline at end of file +const _self=self;function EasySW(){const mainClass=this;this.CACHENAME="default-easy-cache";this.precacheResources=["/"];this.cacheGetRequests=false;this.offlinePage="/offline";this.exclude=(url=>{return false});const precache=()=>{return caches.open(mainClass.CACHENAME).then(function(cache){return cache.addAll([...mainClass.precacheResources,mainClass.offlinePage])})};const refresh=(response,info=null)=>{return _self.clients.matchAll().then(function(clients){clients.forEach(function(client){var message={type:"refresh",url:response.url,info:info};client.postMessage(JSON.stringify(message))})})};const serverReturnedUncachableResponse=(response,info=null)=>{return _self.clients.matchAll().then(function(clients){clients.forEach(function(client){var message={type:"uncachable-response",response:response,info:info};client.postMessage(JSON.stringify(message))})})};const fromCache=request=>{return caches.open(mainClass.CACHENAME).then(function(cache){return cache.match(request).then(function(matching){return matching||fetch(request).catch(error=>{return caches.match(mainClass.offlinePage)})})})};const update=request=>{return caches.open(mainClass.CACHENAME).then(function(cache){return fetch(request).then(function(response){if(response.status<300&&response.type==="basic"){return Promise.all([response.clone(),response.text()])}serverReturnedUncachableResponse(response);return[false,null]}).then(([response,text])=>{if(text){cache.match(request).then(matching=>matching?matching.text():null).then(function(cachedText){cache.put(request,response.clone()).then(function(){if(cachedText&&!stringsAreSame(text,cachedText)){refresh(response,[text.length,cachedText.length])}})})}}).catch(error=>"Network failed")})};const stringsAreSame=(a,b)=>{const cleanedA=a.replace(/[\s\S]*/,"");const cleanedB=b.replace(/[\s\S]*/,"");if(cleanedA.length!==cleanedB.length){return false}return cleanedA.localeCompare(cleanedB)===0};this.init=(()=>{_self.addEventListener("install",function(evt){console.log("The Easy service worker is being installed.");_self.skipWaiting();evt.waitUntil(precache())});_self.addEventListener("activate",function(event){event.waitUntil(caches.keys().then(function(cacheNames){return Promise.all(cacheNames.filter(function(cacheName){return cacheName!==mainClass.CACHENAME}).map(function(cacheName){return caches.delete(cacheName)}))}))});_self.addEventListener("fetch",function(evt){let nonCachable=!(evt.request.url.indexOf("http")===0)||evt.request.destination!=="document"||evt.request.method=="POST"||evt.request.mode==="cors"||/favicon/.test(evt.request.url);if(!mainClass.cacheGetRequests){nonCachable=nonCachable||/[?]/.test(evt.request.url)}if(nonCachable||mainClass.exclude(evt.request.url)){return evt.respondWith(fetch(evt.request).catch(error=>console.log(error)))}evt.respondWith(fromCache(evt.request));evt.waitUntil(update(evt.request))})});this.init()} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 01b2376..2fb0177 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "easy-service-worker", - "version": "1.2.2", + "version": "1.2.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 38dad83..8862877 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-service-worker", - "version": "1.2.2", + "version": "1.2.3", "description": "This package is for getting best performance with eaziest configuration", "main": "lib/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index cb9c651..0aa16e0 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,7 @@ function EasySW() { this.CACHENAME = "default-easy-cache"; this.precacheResources = ["/"]; this.cacheGetRequests = false; - this.offlinePage = "/offline.html"; + this.offlinePage = "/offline"; this.exclude = (url) => { return false; };