-
Notifications
You must be signed in to change notification settings - Fork 0
/
presw.js
1 lines (1 loc) · 2.35 KB
/
presw.js
1
function mainjsLoaded(){for(var e=document.body.getElementsByTagName("script"),r=0;r<e.length;r++)if(-1!=e[r].src.lastIndexOf("vendors~main"))return!0;return!1}function addBundleJS(){if(!mainjsLoaded()&&hashes){var e=document.createElement("link"),r=document.createElement("script"),n=document.createElement("script");e.rel="stylesheet",e.href="./"+hashes.main.css,r.src="./"+hashes["vendors~main"].js,n.src="./"+hashes.main.js,document.head.appendChild(e),document.body.appendChild(r),document.body.appendChild(n)}}function check_sw_rdy(r,e){("number"!=typeof e||isNaN(e))&&(e=0),window.setTimeout(function(){r.ready&&"function"==typeof r.ready.then?r.ready.then(function(e){"activated"===e.active.state?addBundleJS():sw_retry_count<100?(err_fn({error:{message:"Waiting for oServiceWorkerRegistration.active.state to === 'activated'..."}}),sw_retry_count+=1,check_sw_rdy(r,500)):err_fn({error:{message:"Service worker took too long to get ready. Either we lost internet connection or there is a code issue that needs to be fixed."}})}):(err_fn({error:{message:"serviceWorker.ready is null or undefined. Trying again.."}}),sw_retry_count+=1,check_sw_rdy(r,500))},e)}function err_fn(e){var r=console_div.childNodes.length,n=document.createElement("div");return n.textContent=e.error.message,n.style.backgroundColor=r%2==0?"rgb(226,239,228)":"rgb(255,255,255)",console_div.appendChild(n),!1}var sw_retry_count=0,app_div=document.getElementById("app"),console_div=document.getElementById("console");console_div&&(console_div.textContent="",window.addEventListener("error",err_fn)),"serviceWorker"in navigator?"string"==typeof window.location.href&&-1!==window.location.href.indexOf("zzzz")?navigator.serviceWorker.getRegistrations().then(function(e){for(var r=0,n=0;n<e.length;n++)e[n].active&&-1!==e[n].active.scriptURL.indexOf("sw.js")&&(r++,e[n].unregister());err_fn({error:{message:"Found and unregistered "+r+" Service worker(s)."}}),err_fn({error:{message:"Service worker off. You might need to refresh again for network-first assets."}}),window.setTimeout(addBundleJS,1e3)}):(navigator.serviceWorker.register("./sw.js").then(function(e){console.log("%cRegistration succeeded. Scope is "+e.scope,"background-color: lightgreen;")}).catch(function(e){console.log("Registration failed with "+e)}),check_sw_rdy(navigator.serviceWorker)):err_fn({error:{message:"navigator.serviceWorker not available."}});