-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
1 lines (1 loc) · 3.71 KB
/
sw.js
1
(()=>{const t="zxmaCache",e="https://id.v3/",n=n=>caches.open(t).then((t=>t.put(e,new Response(JSON.stringify(n))))),s=()=>caches.match(e).then((t=>t?.json()));self.addEventListener("install",(()=>{self.skipWaiting()})),self.addEventListener("activate",(t=>t.waitUntil(clients.claim())));const a=t=>{if(t.startsWith("https://npm.elemecdn.com"))return{timeout:3e3,list:[t,`https://cdn.cbd.int/${new URL(t).pathname}`],list:[t,`https://fastly.jsdelivr.net/npm${new URL(t).pathname}`]}},r={resource:{clean:!0,match:t=>"www.zxma.top"===t.host&&t.pathname.match(/\.(woff2|woff|ttf|cur|js|css|json)$/)},resource2:{clean:!0,match:t=>"www.zxma.top"===t.host&&t.pathname.match(/^\/(posts|tags|archives|categories|index\.html)/)},static:{clean:!0,match:t=>"cdn.staticfile.org"===t.host||t.pathname.match(/\.(woff2|woff|ttf|cur|json)$/)},zxma:{clean:!0,match:t=>"cdn.zxma.top"===t.host||t.pathname.match(/\.(woff2|woff|ttf|cur)$/)}},c=(t,e,n=null)=>{const s={cache:e?"no-store":"default",mode:"cors",credentials:"same-origin"};if(n||(n=a(t.url)),!n)return fetch(t,s);const r=n.list,c=[];let i=0;return new Promise(((e,a)=>{const o=()=>{const l=c.length;if(l===r.length)return;const u=()=>{++i===r.length?a(`请求 ${t.url} 失败`):l+1===c.length&&(clearTimeout(c[l].id),o())};c.push({ctrl:new AbortController,id:setTimeout(o,n.timeout)}),fetch(new Request(r[l],t),s).then((t=>{if(h(t)){for(let t in c)t!==l&&c[t].ctrl.abort(),clearTimeout(c[t].id);e(t)}else u()})).catch(u)};o()}))},h=t=>t.ok||[301,302,307,308].includes(t.status);function i(t){if("localhost"!==t.hostname)for(let e in r){const n=r[e];if(n.match(t))return n}}function o(){const t=[];this.push=e=>{t.push(e)},this.clean=e=>{t.length=0,e||this.push(e)},this.match=e=>{if(this.refresh)return!0;for(let n of t)if(n.match(e))return!0;return!1}}function l(t){const e=t=>{const e=i(new URL(t));return!e||e.clean},n=e=>{const n=t.value;if(Array.isArray(n)){for(let t of n)if(e(t))return!0;return!1}return e(n)};switch(t.flag){case"all":this.match=e;break;case"html":this.match=t=>t.match(/(\/|\.html)$/);break;case"page":this.match=t=>n((e=>t.match(new RegExp(`\\/${e}(\\/|\\.html)$`))));break;case"file":this.match=t=>n((e=>t.endsWith(e)));break;case"str":this.match=t=>n((e=>t.includes(e)));break;case"reg":this.match=t=>n((e=>t.match(new RegExp(e,"i"))));break;default:throw`未知表达式:${JSON.stringify(t)}`}}self.addEventListener("fetch",(e=>{let n=e.request;if("GET"!==n.method||!n.url.startsWith("http"))return;const s=new URL(n.url),r=i(s);if(r){let a=`https://${s.host}${s.pathname}`;a.endsWith("/index.html")&&(a=a.substring(0,a.length-10)),r.search&&(a+=s.search),e.respondWith(caches.match(a).then((e=>e??c(n,!0).then((e=>{if(h(e)){const n=e.clone();caches.open(t).then((t=>t.put(a,n)))}return e})))))}else{const t=a(n.url);t&&e.respondWith(c(n,!1,t))}})),self.addEventListener("message",(a=>{"update"===a.data&&function(){const a=(t,e,n)=>{for(let s of e){const{version:e,change:a}=s;if(e===n)return!1;if(a)for(let e of a)t.push(new l(e))}return!0},r=t=>s().then((e=>{const{info:s,global:r}=t,c={global:r,local:s[0].version,escape:e?.escape??0};if(!e)return n(c),c;let h=new o,i=a(h,s,e.local);return n(c),i&&(r!==e.global?h.refresh=!0:h.clean(new l({flag:"all"}))),{list:h,version:c}}));return c(new Request("/update.json"),!1).then((n=>{if(h(n))return n.json().then((n=>r(n).then((n=>{return n.list?(s=n.list,caches.open(t).then((t=>t.keys().then((n=>Promise.all(n.map((async n=>{const a=n.url;return a!==e&&s.match(a)?(t.delete(n),a):null}))))).then((t=>t.filter((t=>t))))))).then((t=>0===t.length?null:t)).then((t=>({list:t,version:n.version}))):{version:n};var s}))));throw`加载 update.json 时遇到异常,状态码:${n.status}`}))}().then((t=>a.source.postMessage({type:"update",update:t.list,version:t.version})))}))})();