From 8fb8103948b24d8688c9a90774d3598563442206 Mon Sep 17 00:00:00 2001 From: Thierry Bela Date: Wed, 14 Oct 2020 21:55:59 -0400 Subject: [PATCH] do not build the service worker if the file is empty #104 --- build.sh | 30 ++++-- worker/dist/browser.administrator.js | 48 +++++---- worker/dist/browser.js | 151 ++++++++++++++++----------- worker/dist/browser.sync.js | 72 +++++++------ worker/dist/browser.sync.min.js | 1 - worker/dist/browser.uninstall.js | 48 +++++---- worker/dist/serviceworker.js | 4 +- worker/dist/serviceworker.min.js | 1 + worker_version | 2 +- 9 files changed, 207 insertions(+), 150 deletions(-) diff --git a/build.sh b/build.sh index 48a99fd..e700a35 100755 --- a/build.sh +++ b/build.sh @@ -8,22 +8,32 @@ VER=$(git rev-parse --short HEAD) cd $DIR # # +fail() { + + echo -e '\e[31mbuild failed\e[0m' + exit 1 +} +# +# +# build config files +# process es6+ deps node rollup.config.js +# minify +node terser.config.js # -cat worker/dist/serviceworker.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/serviceworker.js +sed -i "s/build-date/$DATE/g" worker/dist/serviceworker.js && sed -i "s/build-id/$VER/g" worker/dist/serviceworker.js || fail # -./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\ - -- worker/src/browser.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.js +[ -s worker/dist/serviceworker.js ] || fail # -./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\ - -- worker/src/browser.administrator.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.administrator.js +# shellcheck disable=SC2002 +sed "s/build-date/$DATE/g" worker/src/browser.js | sed "s/build-id/$VER/g" > worker/dist/browser.js || fail # -./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\ - -- worker/src/browser.sync.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.sync.js +sed "s/build-date/$DATE/g" worker/src/browser.administrator.js | sed "s/build-id/$VER/g" > worker/dist/browser.administrator.js || fail # -./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\ - -- worker/src/browser.uninstall.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.uninstall.js +sed "s/build-date/$DATE/g" worker/src/browser.sync.js | sed "s/build-id/$VER/g" > worker/dist/browser.sync.js || fail # -node terser.config.js +sed "s/build-date/$DATE/g" worker/src/browser.uninstall.js | sed "s/build-id/$VER/g" > worker/dist/browser.uninstall.js || fail +# +# node terser.config.js # sha1sum worker/dist/serviceworker.js | awk '{print $1;}' | tee ./worker_version \ No newline at end of file diff --git a/worker/dist/browser.administrator.js b/worker/dist/browser.administrator.js index 02576d7..518aceb 100644 --- a/worker/dist/browser.administrator.js +++ b/worker/dist/browser.administrator.js @@ -1,21 +1,27 @@ -/* do not edit! */ -/** - * Service worker browser client - * @package GZip Plugin - * @copyright Copyright (C) 2005 - 2018 Thierry Bela. - * - * dual licensed - * - * @license LGPL v3 - * @license MIT License - */ -// @ts-check -// build d108aa3 2020-10-12 20:38:26-04:00 -if ("serviceWorker" in navigator) { - navigator.serviceWorker.register("{scope}worker{debug}.js", { - scope: "{scope}" - }).catch((function(error) { - // console.log(error); - console.error("😭", error); - })); -} +/** + * Service worker browser client + * @package GZip Plugin + * @copyright Copyright (C) 2005 - 2018 Thierry Bela. + * + * dual licensed + * + * @license LGPL v3 + * @license MIT License + */ + +// @ts-check + +// build 77ad4d7 2020-10-14 21:54:44-04:00 + +if ("serviceWorker" in navigator) { + navigator.serviceWorker. + register("{scope}worker{debug}.js", {scope: "{scope}"}). + // .then(function(registration) { + + // console.log("🍻"); + // }) + catch(function(error) { + // console.log(error); + console.error("😭", error); + }); +} diff --git a/worker/dist/browser.js b/worker/dist/browser.js index a844b1d..7ab0b92 100644 --- a/worker/dist/browser.js +++ b/worker/dist/browser.js @@ -1,61 +1,90 @@ -/* do not edit! */ -/** - * Service worker browser client - * @package GZip Plugin - * @copyright Copyright (C) 2005 - 2018 Thierry Bela. - * - * dual licensed - * - * @license LGPL v3 - * @license MIT License - */ -// @ts-check -// build d108aa3 2020-10-12 20:38:26-04:00 -if ("serviceWorker" in navigator) { - navigator.serviceWorker.register("{scope}worker{debug}.js", { - scope: "{scope}" - }).catch((function(error) { - // console.log(error); - console.error("😭", error); - })); - if ("onbeforeinstallprompt" in window) { - let deferredPrompt; - let button; - const buttonHTML = '
' + "" + "Click here to make this site available offline."; - const clickHandler = function(e) { - e.preventDefault(); - e.stopPropagation(); - deferredPrompt.prompt(); - button.removeEventListener("click", clickHandler, false); - button = null; - // log the platforms provided as options in an install prompt - // console.log(deferredPrompt.platforms); // e.g., ["web", "android", "windows"] - deferredPrompt.userChoice.then((function(outcome) { - console.info(outcome); - // either "installed", "dismissed", etc. - }), (function(error) { - console.error("😭", error); - })); - // e.target.closest('[data-action=install-pwa-app]').removeEventListener('click', clickHandler, false) - }; - const createButton = function() { - document.body.insertAdjacentHTML("beforeend", buttonHTML); - button = document.querySelector("a[data-action=install-pwa-app]"); - button.addEventListener("click", clickHandler, false); - }; - window.addEventListener("beforeinstallprompt", (function(e) { - // console.log("beforeinstallprompt", e); - deferredPrompt = e; - e.preventDefault(); - // if ("getInstalledRelatedApps" in navigator) { - // navigator.getInstalledRelatedApps().then(function(relatedApps) { - // if (relatedApps.length == 0) { - // createButton(); - // } - // }); - // } else { - createButton(); - // } - })); - } -} +/** + * Service worker browser client + * @package GZip Plugin + * @copyright Copyright (C) 2005 - 2018 Thierry Bela. + * + * dual licensed + * + * @license LGPL v3 + * @license MIT License + */ + +// @ts-check + +// build 77ad4d7 2020-10-14 21:54:44-04:00 + +if ("serviceWorker" in navigator) { + navigator.serviceWorker. + register("{scope}worker{debug}.js", { + scope: "{scope}" + }). + // .then(function(registration) { + + // console.log("🍻"); + // }) + catch(function (error) { + // console.log(error); + console.error("😭", error); + }); + + if ("onbeforeinstallprompt" in window) { + let deferredPrompt; + let button; + + const buttonHTML = + '
' + + "" + + "Click here to make this site available offline."; + + const clickHandler = function (e) { + e.preventDefault(); + e.stopPropagation(); + + deferredPrompt.prompt(); + + button.removeEventListener("click", clickHandler, false); + button = null; + + // log the platforms provided as options in an install prompt + // console.log(deferredPrompt.platforms); // e.g., ["web", "android", "windows"] + + deferredPrompt.userChoice.then( + function (outcome) { + console.info(outcome); // either "installed", "dismissed", etc. + }, + function (error) { + console.error("😭", error); + } + ); + + // e.target.closest('[data-action=install-pwa-app]').removeEventListener('click', clickHandler, false) + }; + + const createButton = function () { + document.body.insertAdjacentHTML("beforeend", buttonHTML); + button = document.querySelector("a[data-action=install-pwa-app]"); + + button.addEventListener("click", clickHandler, false); + }; + + window.addEventListener("beforeinstallprompt", function (e) { + // console.log("beforeinstallprompt", e); + + deferredPrompt = e; + + e.preventDefault(); + + // if ("getInstalledRelatedApps" in navigator) { + // navigator.getInstalledRelatedApps().then(function(relatedApps) { + // if (relatedApps.length == 0) { + // createButton(); + // } + // }); + // } else { + createButton(); + // } + }); + } +} \ No newline at end of file diff --git a/worker/dist/browser.sync.js b/worker/dist/browser.sync.js index 0c68253..47be4d1 100644 --- a/worker/dist/browser.sync.js +++ b/worker/dist/browser.sync.js @@ -1,32 +1,40 @@ -/* do not edit! */ -/** - * Service worker browser client - * @package GZip Plugin - * @copyright Copyright (C) 2005 - 2018 Thierry Bela. - * - * dual licensed - * - * @license LGPL v3 - * @license MIT License - */ -// @ts-check -if ("serviceWorker" in navigator) { - if ("SyncManager" in window) { - navigator.serviceWorker.ready.then((function(reg) { - return reg.sync.getTags().then((function(tags) { - if (!tags.includes("{SYNC_API_TAG}")) { - reg.sync.register("{SYNC_API_TAG}"); - } - })); - })).catch((function(error) { - // system was unable to register for a sync, - // this could be an OS-level restriction - console.error("cannot setup native sync api, using fallback 😭", error); - new Worker("{scope}sync-fallback{debug}.js"); - })); - } else { - // serviceworker/sync not supported, use a worker instead - console.info("background sync api not supported, using fallback 😭"); - new Worker("{scope}sync-fallback{debug}.js"); - } -} +/** + * Service worker browser client + * @package GZip Plugin + * @copyright Copyright (C) 2005 - 2018 Thierry Bela. + * + * dual licensed + * + * @license LGPL v3 + * @license MIT License + */ + +// @ts-check + +if ('serviceWorker' in navigator) { + + if ('SyncManager' in window) { + + navigator.serviceWorker.ready.then(function (reg) { + + return reg.sync.getTags().then(function (tags) { + + if (!tags.includes("{SYNC_API_TAG}")) { + reg.sync.register("{SYNC_API_TAG}"); + } + }) + }). + catch(function (error) { + // system was unable to register for a sync, + // this could be an OS-level restriction + console.error('cannot setup native sync api, using fallback 😭', error); + new Worker("{scope}sync-fallback{debug}.js"); + }); + + } else { + + // serviceworker/sync not supported, use a worker instead + console.info('background sync api not supported, using fallback 😭'); + new Worker("{scope}sync-fallback{debug}.js"); + } +} \ No newline at end of file diff --git a/worker/dist/browser.sync.min.js b/worker/dist/browser.sync.min.js index 4b17786..e69de29 100644 --- a/worker/dist/browser.sync.min.js +++ b/worker/dist/browser.sync.min.js @@ -1 +0,0 @@ -"serviceWorker"in navigator&&("SyncManager"in window?navigator.serviceWorker.ready.then((function(n){return n.sync.getTags().then((function(e){e.includes("{SYNC_API_TAG}")||n.sync.register("{SYNC_API_TAG}")}))})).catch((function(n){console.error("cannot setup native sync api, using fallback 😭",n),new Worker("{scope}sync-fallback{debug}.js")})):new Worker("{scope}sync-fallback{debug}.js")); \ No newline at end of file diff --git a/worker/dist/browser.uninstall.js b/worker/dist/browser.uninstall.js index 504a370..abd6ae8 100644 --- a/worker/dist/browser.uninstall.js +++ b/worker/dist/browser.uninstall.js @@ -1,22 +1,26 @@ -/* do not edit! */ -/** - * Service worker browser client uninstall - * @package GZip Plugin - * @copyright Copyright (C) 2005 - 2018 Thierry Bela. - * - * dual licensed - * - * @license LGPL v3 - * @license MIT License - */ -// @ts-check -// build d108aa3 2020-10-12 20:38:26-04:00 -if ("serviceWorker" in navigator && navigator.serviceWorker.controller) { - navigator.serviceWorker.getRegistration().then((function(registration) { - registration.unregister().then((function(result) { - if (result) { - console.info("The service worker has been successfully removed 😭"); - } - })); - })); -} +/** + * Service worker browser client uninstall + * @package GZip Plugin + * @copyright Copyright (C) 2005 - 2018 Thierry Bela. + * + * dual licensed + * + * @license LGPL v3 + * @license MIT License + */ +// @ts-check + +// build 77ad4d7 2020-10-14 21:54:44-04:00 + +if ("serviceWorker" in navigator && navigator.serviceWorker.controller) { + navigator.serviceWorker.getRegistration().then(function (registration) { + + registration.unregister().then(function (result) { + + if (result) { + + console.info('The service worker has been successfully removed 😭'); + } + }); + }); +} \ No newline at end of file diff --git a/worker/dist/serviceworker.js b/worker/dist/serviceworker.js index b918ad7..0e84ec6 100644 --- a/worker/dist/serviceworker.js +++ b/worker/dist/serviceworker.js @@ -881,14 +881,14 @@ * service worker build id */ buildid: { - value: "build-id", + value: "77ad4d7", enumerable: true }, /** * service worker buid date */ builddate: { - value: "build-date", + value: "2020-10-14 21:54:44-04:00", enumerable: true }, /** diff --git a/worker/dist/serviceworker.min.js b/worker/dist/serviceworker.min.js index e69de29..2b1801e 100644 --- a/worker/dist/serviceworker.min.js +++ b/worker/dist/serviceworker.min.js @@ -0,0 +1 @@ +!function(){"use strict";async function DB(e,t="id",r=[]){return new Promise((n,s)=>{const a=indexedDB.open(e,1),o=`${e}_store`;let i;const _query=(e,t,r=null,n=null)=>new Promise((s,a)=>{const c=t?"readonly":"readwrite";if(i.objectStoreNames.contains(o)){const t=i.transaction(o,c),l=t.objectStore(o);let u;"put"===e&&r&&void 0!==r.length?(u=t,r.forEach(e=>{l.put(e)})):(n&&l.index(n),u=l[e](r)),u.oncomplete=e=>{s(e.target.result)},u.onsuccess=e=>{s(e.target.result)},u.onerror=e=>{a(e)}}else a(new Error("Store not found"))}),c={count:async()=>{const e=i.transaction(o,"readonly").objectStore(o).count();return new Promise((function(t,r){e.onsuccess=function(e){t(e.target.result)},e.onerror=function(e){r(e)}}))},get:(e,t)=>_query("get",!0,e,t),getAll:(e,t)=>_query("getAll",!0,e,t),put:e=>_query("put",!1,e),delete:e=>_query("delete",!1,e),clear:()=>_query("clear",!1),deleteDatabase:()=>new Promise((function(e,t){const r=indexedDB.deleteDatabase;r.onerror=t,r.onsuccess=e}))};a.onupgradeneeded=(()=>{i=a.result;const e=i.createObjectStore(o,{keyPath:t});let n;for(n of r)e.createIndex(n.name,n.key,n.options)}).bind(this),a.onsuccess=(()=>{i=a.result,n(c)}).bind(this),a.onerror=(e=>{s(new Error(e.originalTarget&&e.originalTarget.error||e))}).bind(this)})}const e={implement(e){const t=e.prototype,r=[].slice.call(arguments,1);let n,s,a;function makefunc(e,t,r){return function(){const n=this,s="previous"in n,a="parent"in n,o=n.previous,i=n.parent;n.previous=t,n.parent=r;const c=e.apply(n,arguments);return s&&(n.previous=o),a&&(n.parent=i),c}}for(n=0;nfunction(t){if("object"==typeof t){const r=[].slice.call(arguments,1);let n;for(n in t)e.apply(this,[n,t[n]].concat(r))}else e.apply(this,arguments);return this}};function merge(e){const t=[].slice.call(arguments,1);let r,n,s,a,o="boolean"==typeof e;for(!0===o&&(o=e,e=t.shift()),r=0;r1?[].slice.call(arguments,1):[];return Promise.all((t.$events[e]||[]).concat().map(e=>new Promise(n=>{n(e.cb.apply(t,r))})))},addPseudo(e,t){return this.$pseudo[e]=t,this}};function normalize(e="GET"){return Array.isArray(e)||(e=[e]),e.forEach(e=>e==i||"HEAD"==e?"GET":e.toUpperCase()),e}r.addPseudo("once",(function(e){return e.cb=function(){const t=this,r=e.fn.apply(t,arguments);return t.off(e.name,e.fn),r},this}));class n{constructor(){this.routers=Object.create(i),this.defaultRouter=Object.create(i)}getRouter(e){const t=e!=i&&e.request.method||"GET",r=this.routers[t]||[],n=r.length;let s,a=0;for(;a{t in this.routers||(this.routers[t]=[]),this.routers[t].push(e)}),this}unregisterRoute(e,t){return normalize(t).forEach(t=>{const r=this.routers[t]||[],n=r.indexOf(e);-1!=n&&r.splice(n,1)}),this}setDefaultRouter(e,t){normalize(t).forEach(t=>this.defaultRouter[t]=e)}getDefaultRouter(){for(let e in this.defaultRouter)return this.defaultRouter[e];return i}}e.merge(!0,n.prototype,r);class s{constructor(e,t,r=null){const n=this;n.plugins=[],n.options=Object.assign(Object.create(i),{mime:[]},r||{}),n.path=e,n.strategy=t.name,n.handler={handle:async e=>{let r,s;for(r of n.plugins)try{if(s=await r.precheck(e),s instanceof Response)return s}catch(e){console.error({error:e})}if(s=await t.handle(e),s instanceof Response)for(r of n.plugins)try{await r.postcheck(e,s)}catch(e){console.error(e.message)}return s}}}addPlugin(e){return this.plugins.includes(e)||this.plugins.push(e),this}match(e,t){return!0}}e.merge(!0,s.prototype,r);class a extends s{match(e,t){const r=e.request.url;return/^https?:/.test(r)&&this.path.test(r)||this.options.mime.includes(e.request.headers.get("Content-Type"))||t!=i&&this.options.mime.includes(t.headers.get("Content-Type"))}}class o extends s{constructor(e,t,r=null){super(e,t,r),this.url=new URL(e,self.origin)}match(e,t){const r=e.request.url,n=new URL(r);return/^https?:/.test(r)&&n.origin==this.url.origin&&0==n.pathname.indexOf(this.url.pathname)||this.options.mime.includes(e.request.headers.get("Content-Type"))||t!=i&&this.options.mime.includes(t.headers.get("Content-Type"))}}const i=null,c=Object.create(i);function hashCode(e){var t,r=0;if(0===e.length)return r;for(t=0;ta+=t+":"+getObjectHash(e)+","),","==a[a.length-1]&&(a=a.substr(0,a.length-2)),a+="}";else{for(n of(a+="[",r))a+=getObjectHash(n)+",";","==a[a.length-1]&&(a=a.substr(0,a.length-2)),a+="]"}else a+="{"+getObjectHash(r)+"}";else a+=""+r;else a+=JSON.stringify(r);a+=","}","==a[a.length-1]&&(a=a.substr(0,a.length-2)),Array.isArray(e)?a="["+a+"]":"object"==typeof e&&(a="{"+a+"}")}return a}function num2FileSize(e,t){if(0==e)return 0;const r=Math.floor(Math.log(e)/Math.log(1024));return(e/Math.pow(1024,Math.floor(r))).toFixed(2)+" "+(t&&t[r]?t[r]:["b","Kb","Mb","Gb","Tb","Pb"][r])}function sprintf(e){let t,r=-1;const n=[].slice.apply(arguments).slice(1);return e.replace(/%([s%])/g,(function(e,s){if("%"==s)return s;switch(t=n[++r],s){case"s":return null==t?"":t}}))}function ellipsis(e,t=30,r=15,n="..."){return e.length>t?e.slice(0,t-r-n.length+1)+n+e.slice(e.length-r+1):e}let l;e.merge(!0,c,r),Object.defineProperties(c,{app:{value:Object.create(i)},routes:{value:new n}}),Object.defineProperties(c.app,{name:{value:"gzip",enumerable:!0},scope:{value:"{scope}",enumerable:!0},route:{value:"{ROUTE}",enumerable:!0},cacheName:{value:"{CACHE_NAME}",enumerable:!0},codeName:{value:"Joomla Website Optimizer Plugin",enumerable:!0},build:{value:"{VERSION}",enumerable:!0},buildid:{value:"build-id",enumerable:!0},builddate:{value:"build-date",enumerable:!0},urls:{value:"{CDN_HOSTS}",enumerable:!0},backgroundSync:{value:"{BACKGROUND_SYNC}",enumerable:!0},offline:{value:"{pwa_offline_page}"},network:{value:"{pwa_cache_settings}"},customNetwork:{value:"{pwa_custom_cache_settings}"},precache:{value:"{preloaded_urls}".map(e=>new URL(e,self.origin).href),enumerable:!0},homepage:{value:"https://github.com/tbela99/gzip",enumerable:!0}}),caches.open(c.app.cacheName).then(e=>l=e);class u{constructor(e){this.setOptions(e)}getRouteTag(e){const t=c.app.route;let r;for(r of c.app.urls)if(new RegExp("^https?://"+r+c.app.scope+t+"/").test(e))return t;return i}async setOptions(e){const t=new Date,r=+t;this.maxAge=0,this.limit=+e.limit||0,this.maxFileSize=+e.maxFileSize||0;const n=e.maxAge.match(/([+-]?\d+)(.*)$/);if(null!=n)switch(n[2]){case"months":case"minutes":case"hours":let e=(s=n[2])[0].toUpperCase()+s.slice(1);"Months"==e&&(e="Month"),t["set"+e](+n[1]+t["get"+e]()),this.maxAge=t-r}var s;this.db=await DB(e.cacheName!=i?e.cacheName:"gzip_sw_worker_expiration_cache_private","url",[{name:"url",key:"url"},{name:"version",key:"version"},{name:"route",key:"route"}])}async precheck(e){try{if(this.db==i||0==this.maxAge)return!0;const t=hashCode(getObjectHash(e.request)),r=await this.db.get(e.request.url,"url");return r!=i&&(r.version!=t||r.timestamp0&&t.body!=i){const e=await t.clone().blob();if(e.size>this.maxFileSize)throw sprintf("cache limit exceeded. Deleting item [%s]",ellipsis(t.url)),await this.db.delete(t.url),await l.delete(t),new Error(sprintf("[%s][cache failed] cache size limit exceeded %s of %s",ellipsis(t.url,42),num2FileSize(e.size),num2FileSize(this.maxFileSize)))}try{const t=e.request.url,r=await this.db.get(t,"url"),n=hashCode(getObjectHash(e.request));return r==i||r.version!=n||r.timestampt instanceof Response&&("cors"==t.type||new URL(e.url,self.origin).origin==self.origin)&&"GET"==e.method&&t.ok&&["default","cors","basic","navigate"].includes(t.type)&&!t.bodyUsed,h=new Map,f={add:(e,t,r)=>h.set(e,{key:e,name:r==i?e:r,handle:async e=>await t(e)}),keys:()=>h.keys(),values:()=>h.values(),entries:()=>h.entries(),get:e=>h.get(e),has:e=>h.has(e),delete:e=>h.delete(e),isCacheableRequest:isCacheableRequest};f[Symbol.iterator]=()=>h[Symbol.iterator](),Object.defineProperty(f,"size",{get:()=>h.size});const p=c.app.cacheName,d=c.app.cacheName,m=c.app.cacheName,g=c.app.cacheName;f.add("cf",(async function(e){let t=await caches.match(e.request,{cacheName:p});if(null!=t)return t;if(t=await fetch(e.request),f.isCacheableRequest(e.request,t)){const r=t.clone();caches.open(p).then(t=>t.put(e.request,r))}return t}),"Cache fallback to Network"),f.add("cn",(async function(e){const t=await caches.match(e.request,{cacheName:d}),r=fetch(e.request).then(t=>{if(f.isCacheableRequest(e.request,t)){const r=t.clone();caches.open(d).then(t=>t.put(e.request,r))}return t}).catch(()=>{});return t||r}),"Cache and Network Update"),f.add("co",(async function(e){return await caches.match(e.request,{cacheName:m})}),"Cache Only"),f.add("nf",(async function(e){try{const t=await fetch(e.request);if(null==t)throw new Error("Network error");if(f.isCacheableRequest(e.request,t)){const r=t.clone();caches.open(g).then(t=>t.put(e.request,r))}return t}catch(e){}return caches.match(e.request,{cacheName:g})}),"Network fallback to Cache"),f.add("no",(async function(e){return fetch(e.request)}),"Network Only"),self.addEventListener("activate",e=>{e.waitUntil((async()=>{try{await c.resolve("activate",e)}catch(e){console.error("😭",e)}return self.clients.claim()})())}),self.addEventListener("fetch",e=>{e.respondWith(async function(){if(!e.url||"only-if-cached"===e.request.cache&&"same-origin"!==e.request.mode)return fetch(e.request);let t;const r=c.routes.getRouter(e);if(r!=i)try{if(t=await r.handler.handle(e),t instanceof Response)return t;for(t of await c.routes.resolve("fail",e,t))if(t instanceof Response)return t}catch(e){console.error("😭",e)}return fetch(e.request)}())}),self.addEventListener("install",e=>{e.waitUntil((async()=>{try{await c.resolve("install",e)}catch(e){console.error("😭",e)}return self.skipWaiting()})())});const y=["method","referrer","referrerPolicy","mode","credentials","cache","redirect","integrity","keepalive"];class w{async push(e){const t=await this.cloneRequestData(e),r=await this.getDB();return await r.put({id:hashCode(e.url+y.map(async e=>{let r=t[e];return r==i?"":"headers"==e?r instanceof Headers?[...r.values()].filter(e=>e!=i).join(""):Object.values(r).map(r=>t[e][r]!=i?t[e][r]:"").join(""):"body"==e?await r.text():r}).join("")),lastRetry:Date.now()+864e5,url:e.url,request:t}),this}async cloneRequestData(e){const t={headers:{}};"GET"!==e.method&&(t.body=await e.clone().arrayBuffer());for(const[r,n]of e.headers.entries())t.headers[r]=n;for(const r of y)void 0!==e[r]&&(t[r]=e[r]);return"navigate"===t.mode&&(t.mode="same-origin"),t}async getDB(){return this.db==i&&(this.db=await DB("gzip_sw_worker_sync_requests","id")),this.db}async replay(e){if("{SYNC_API_TAG}"!=e)return;const t=await this.getDB(),r=await t.getAll();r.length;const n=await caches.open("{CACHE_NAME}");for(const e of r){let r=!1;try{e.request.method,e.url;const t=new Request(e.url,e.request);let s=await n.match(t);r=s!=i,r||(s=await fetch(t.clone()),r=s!=i&&s.ok,r&&isCacheableRequest(t,s)&&await n.put(t,s))}catch(e){}(r||e.lastRetry<=Date.now())&&await t.delete(e.id)}}}if(c.app.backgroundSync.enabled){const e=new w;c.on({async install(){for(const e of["gzip_sw_worker_sync_requests","gzip_sw_worker_sync_tasks"]){const t=await DB(e,"id");null!=t&&await t.clear()}}}),c.routes.on({fail(t,r){const n=c.app.backgroundSync;if(0==n.length||-1!=n.method.indexOf(t.method)){const r=t.url.replace(self.origin,"");if(0==n.pattern.length||n.pattern.some(e=>0==r.indexOf(e)))return e.push(t)}}}),self.addEventListener("sync",(function(t){t.tag,t.waitUntil(e.replay(t.tag).catch(e=>console.error({error:e})))}))}c.app.network.limit>0&&self.addEventListener("sync",async e=>{const t=await async function(){let e=await caches.open("{CACHE_NAME}");const t="{preloaded_urls}".map(e=>new URL(e,self.origin).href),r="{pwa_cache_max_file_count}",n=await DB("gzip_sw_worker_expiration_cache_private","url",[{name:"url",key:"url"},{name:"version",key:"version"},{name:"route",key:"route"}]);return async function(){let s=await n.count();if(s>r){sprintf("cleaning up [%s] items present. [%s] items allowed",s,r);for(let a of await n.getAll())if(t.includes(a.url))sprintf("skipped preloaded resource [%s]",a.url);else if(sprintf("removing [%s]",a.url),await e.delete(a.url),await n.delete(a.url),--s<=r)break;sprintf("cleaned up [%s] items present. [%s] items allowed",s,r)}}}();e.waitUntil(t())}),c.app.offline.enabled&&c.routes.on("fail",async e=>{if("navigate"==e.request.mode&&c.app.offline.methods.includes(e.request.method)){if("response"==c.app.offline.type)return new Response(c.app.offline.body,{headers:new Headers({"Content-Type":'text/html; charset="{offline_charset}"'})});if(""!=c.app.offline.url)return caches.match(c.app.offline.url)}});const b=c.routes,v=c.app.scope,k=c.app.network,O=k.caching;let _,R,x,q,j,A,E;({limit:j,maxAge:x,cacheName:A,strategy:E,maxFileSize:q}=k);const N={limit:j,maxAge:x,strategy:E,maxFileSize:q};for(_ of"{exclude_urls}")b.registerRoute(new a(new RegExp(_),f.get("no")));for(_ of k.settings)R=new a(new RegExp("("+_.ext.join(")|(")+")","i"),f.get(_.strategy),_),O&&(({limit:j,maxAge:x,maxFileSize:q}=_),R.addPlugin(new u({limit:j,maxAge:x,maxFileSize:q}))),b.registerRoute(R);for(_ of f)R=new o(v+"{ROUTE}/media/z/"+_[0]+"/",_[1]),O&&R.addPlugin(new u(N)),b.registerRoute(R),c.app.customNetwork.forEach(e=>{let t=v+"{ROUTE}/"+_[0]+"/"+e.prefix+"/";delete e.prefix;let r=new o(t,e);r.addPlugin(new u(e)),b.registerRoute(r)});R=new o(v,f.get(k.strategy)),O&&R.addPlugin(new u(N)),b.setDefaultRouter(R),A=c.app.cacheName,c.on({error(e,t){console.error({error:e,event:t})},async install(){await caches.open(A).then(async e=>await e.addAll(c.app.precache))},async activate(){const e=await DB("gzip_sw_worker_config_cache_private","name"),t=await e.get("gzip");if(t!=i&&"{ROUTE}"!=t.route){let e,t;for(e of"{STORES}")t=await DB(e,"url",[{name:"url",key:"url"},{name:"version",key:"version"},{name:"route",key:"route"}]),t!=i&&t.clear()}await e.put(c.app);const r=await caches.keys(),n=A.split(/_/,2),s=2==n.length&&n[0]+"_";0!=s&&await Promise.all(r.map(e=>0==e.indexOf(s)&&e!=A&&caches.delete(e)))}})}(); \ No newline at end of file diff --git a/worker_version b/worker_version index be0e2cd..76e19fa 100644 --- a/worker_version +++ b/worker_version @@ -1 +1 @@ -da39a3ee5e6b4b0d3255bfef95601890afd80709 +c22fadaa841cf95cf62c214f8edb67521ea00d0e