-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sw.js
18 lines (18 loc) · 7.27 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'use strict';var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.arrayIteratorImpl=function(a){var b=0;return function(){return b<a.length?{done:!1,value:a[b++]}:{done:!0}}};$jscomp.arrayIterator=function(a){return{next:$jscomp.arrayIteratorImpl(a)}};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.ISOLATE_POLYFILLS=!1;
$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,c){if(a==Array.prototype||a==Object.prototype)return a;a[b]=c.value;return a};$jscomp.getGlobal=function(a){a=["object"==typeof globalThis&&globalThis,a,"object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global];for(var b=0;b<a.length;++b){var c=a[b];if(c&&c.Math==Math)return c}throw Error("Cannot find global object");};$jscomp.global=$jscomp.getGlobal(this);
$jscomp.IS_SYMBOL_NATIVE="function"===typeof Symbol&&"symbol"===typeof Symbol("x");$jscomp.TRUST_ES6_POLYFILLS=!$jscomp.ISOLATE_POLYFILLS||$jscomp.IS_SYMBOL_NATIVE;$jscomp.polyfills={};$jscomp.propertyToPolyfillSymbol={};$jscomp.POLYFILL_PREFIX="$jscp$";var $jscomp$lookupPolyfilledValue=function(a,b){var c=$jscomp.propertyToPolyfillSymbol[b];if(null==c)return a[b];c=a[c];return void 0!==c?c:a[b]};
$jscomp.polyfill=function(a,b,c,d){b&&($jscomp.ISOLATE_POLYFILLS?$jscomp.polyfillIsolated(a,b,c,d):$jscomp.polyfillUnisolated(a,b,c,d))};$jscomp.polyfillUnisolated=function(a,b,c,d){c=$jscomp.global;a=a.split(".");for(d=0;d<a.length-1;d++){var e=a[d];if(!(e in c))return;c=c[e]}a=a[a.length-1];d=c[a];b=b(d);b!=d&&null!=b&&$jscomp.defineProperty(c,a,{configurable:!0,writable:!0,value:b})};
$jscomp.polyfillIsolated=function(a,b,c,d){var e=a.split(".");a=1===e.length;d=e[0];d=!a&&d in $jscomp.polyfills?$jscomp.polyfills:$jscomp.global;for(var f=0;f<e.length-1;f++){var g=e[f];if(!(g in d))return;d=d[g]}e=e[e.length-1];c=$jscomp.IS_SYMBOL_NATIVE&&"es6"===c?d[e]:null;b=b(c);null!=b&&(a?$jscomp.defineProperty($jscomp.polyfills,e,{configurable:!0,writable:!0,value:b}):b!==c&&($jscomp.propertyToPolyfillSymbol[e]=$jscomp.IS_SYMBOL_NATIVE?$jscomp.global.Symbol(e):$jscomp.POLYFILL_PREFIX+e,e=
$jscomp.propertyToPolyfillSymbol[e],$jscomp.defineProperty(d,e,{configurable:!0,writable:!0,value:b})))};$jscomp.initSymbol=function(){};$jscomp.initSymbolIterator=function(){};$jscomp.initSymbolAsyncIterator=function(){};$jscomp.iteratorPrototype=function(a){a={next:a};a[Symbol.iterator]=function(){return this};return a};
$jscomp.polyfill("String.prototype.matchAll",function(a){return a?a:function(a){if(a instanceof RegExp&&!a.global)throw new TypeError("RegExp passed into String.prototype.matchAll() must have global tag.");var c=new RegExp(a,a instanceof RegExp?void 0:"g"),b=this,e=!1,f={next:function(){var a={},d=c.lastIndex;if(e)return{value:void 0,done:!0};var f=c.exec(b);if(!f)return e=!0,{value:void 0,done:!0};c.lastIndex===d&&(c.lastIndex+=1);a.value=f;a.done=!1;return a}};f[Symbol.iterator]=function(){return f};
return f}},"es_2020","es3");const OFFLINE_DATA_FILE="offline.json",CACHE_NAME_PREFIX="c3offline",BROADCASTCHANNEL_NAME="offline",CONSOLE_PREFIX="[SW] ",LAZYLOAD_KEYNAME="",broadcastChannel="undefined"===typeof BroadcastChannel?null:new BroadcastChannel(BROADCASTCHANNEL_NAME);function PostBroadcastMessage(a){broadcastChannel&&setTimeout(()=>broadcastChannel.postMessage(a),3E3)}function Broadcast(a){PostBroadcastMessage({type:a})}
function BroadcastDownloadingUpdate(a){PostBroadcastMessage({type:"downloading-update",version:a})}function BroadcastUpdateReady(a){PostBroadcastMessage({type:"update-ready",version:a})}function IsUrlInLazyLoadList(a,b){if(!b)return!1;try{for(const c of b)if((new RegExp(c)).test(a))return!0}catch(c){console.error(CONSOLE_PREFIX+"Error matching in lazy-load list: ",c)}return!1}
function WriteLazyLoadListToStorage(a){return"undefined"===typeof localforage?Promise.resolve():localforage.setItem(LAZYLOAD_KEYNAME,a)}function ReadLazyLoadListFromStorage(){return"undefined"===typeof localforage?Promise.resolve([]):localforage.getItem(LAZYLOAD_KEYNAME)}function GetCacheBaseName(){return CACHE_NAME_PREFIX+"-"+self.registration.scope}function GetCacheVersionName(a){return GetCacheBaseName()+"-v"+a}
async function GetAvailableCacheNames(){const a=await caches.keys(),b=GetCacheBaseName();return a.filter(a=>a.startsWith(b))}async function IsUpdatePending(){return 2<=(await GetAvailableCacheNames()).length}async function GetMainPageUrl(){var a=await clients.matchAll({includeUncontrolled:!0,type:"window"});for(const b of a)if(a=b.url,a.startsWith(self.registration.scope)&&(a=a.substring(self.registration.scope.length)),a&&"/"!==a)return a.startsWith("?")&&(a="/"+a),a;return""}
function fetchWithBypass(a,b){"string"===typeof a&&(a=new Request(a));return b?fetch(a.url,{headers:a.headers,mode:a.mode,credentials:a.credentials,redirect:a.redirect,cache:"no-store"}):fetch(a)}
async function CreateCacheFromFileList(a,b,c){const d=await Promise.all(b.map(a=>fetchWithBypass(a,c)));let e=!0;for(const a of d)a.ok||(e=!1,console.error(CONSOLE_PREFIX+"Error fetching '"+a.url+"' ("+a.status+" "+a.statusText+")"));if(!e)throw Error("not all resources were fetched successfully");const f=await caches.open(a);try{return await Promise.all(d.map((a,c)=>f.put(b[c],a)))}catch(g){throw console.error(CONSOLE_PREFIX+"Error writing cache entries: ",g),caches.delete(a),g;}}
async function UpdateCheck(a){try{const c=await fetchWithBypass(OFFLINE_DATA_FILE,!0);if(!c.ok)throw Error(OFFLINE_DATA_FILE+" responded with "+c.status+" "+c.statusText);const d=await c.json(),e=d.version,f=d.fileList,g=d.lazyLoad,h=GetCacheVersionName(e);if(await caches.has(h))await IsUpdatePending()?(console.log(CONSOLE_PREFIX+"Update pending"),Broadcast("update-pending")):(console.log(CONSOLE_PREFIX+"Up to date"),Broadcast("up-to-date"));else{var b=await GetMainPageUrl();f.unshift("./");b&&-1===
f.indexOf(b)&&f.unshift(b);console.log(CONSOLE_PREFIX+"Caching "+f.length+" files for offline use");a?Broadcast("downloading"):BroadcastDownloadingUpdate(e);g&&await WriteLazyLoadListToStorage(g);await CreateCacheFromFileList(h,f,!a);await IsUpdatePending()?(console.log(CONSOLE_PREFIX+"All resources saved, update ready"),BroadcastUpdateReady(e)):(console.log(CONSOLE_PREFIX+"All resources saved, offline support ready"),Broadcast("offline-ready"))}}catch(c){console.warn(CONSOLE_PREFIX+"Update check failed: ",
c)}}self.addEventListener("install",a=>{a.waitUntil(UpdateCheck(!0).catch(()=>null))});async function GetCacheNameToUse(a,b){if(1===a.length||!b||1<(await clients.matchAll()).length)return a[0];b=a[a.length-1];console.log(CONSOLE_PREFIX+"Updating to new version");await Promise.all(a.slice(0,-1).map(a=>caches.delete(a)));return b}
async function HandleFetch(a,b){var c=await GetAvailableCacheNames();if(!c.length)return fetch(a.request);b=await GetCacheNameToUse(c,b);b=await caches.open(b);if(c=await b.match(a.request))return c;c=await Promise.all([fetch(a.request),ReadLazyLoadListFromStorage()]);const d=c[0];if(IsUrlInLazyLoadList(a.request.url,c[1]))try{await b.put(a.request,d.clone())}catch(e){console.warn(CONSOLE_PREFIX+"Error caching '"+a.request.url+"': ",e)}return d}
self.addEventListener("fetch",a=>{if((new URL(a.request.url)).origin===location.origin){var b="navigate"===a.request.mode,c=HandleFetch(a,b);b&&a.waitUntil(c.then(()=>UpdateCheck(!1)));a.respondWith(c)}});