forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvirtual-server-loader.js
More file actions
1 lines (1 loc) · 2.62 KB
/
virtual-server-loader.js
File metadata and controls
1 lines (1 loc) · 2.62 KB
1
import{Workbox}from"https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-window.prod.mjs";function _getBaseURL(){let baseURL=window.location.href;return location.href.indexOf("?")>-1&&(baseURL=location.href.substring(0,location.href.indexOf("?"))),location.href.indexOf("#")>-1&&(baseURL=baseURL.substring(0,baseURL.indexOf("#"))),location.href.indexOf("/")>-1&&(baseURL=baseURL.substring(0,baseURL.lastIndexOf("/"))),baseURL.endsWith("/")||(baseURL+="/"),baseURL}function getRoute(){return"phoenix/vfs"}function _isServiceWorkerLoaderPage(){const indexUrl=`${location.origin}/index.html`,baseUrl=`${location.origin}/`,devURL="http://localhost:8000/src/",distTestURL="http://localhost:8000/dist-test/src/",playwrightDevURL="http://localhost:5000/src/",playwrightDistTestURL="http://localhost:5000/dist-test/src/",currentURL=_getBaseURL();return console.log("currentURL",currentURL,indexUrl,baseUrl,devURL),currentURL===baseUrl||currentURL===indexUrl||currentURL===devURL||currentURL===distTestURL||currentURL===playwrightDevURL&&window.Phoenix.browser.desktop.isChromeBased||currentURL===playwrightDistTestURL&&window.Phoenix.browser.desktop.isChromeBased}async function shouldUpdate(){return!0}if(window.__IS_NATIVE_SHELL__||(window.fsServerUrl=_getBaseURL()+getRoute()+"/"),!window.__IS_NATIVE_SHELL__&&_isServiceWorkerLoaderPage()&&"serviceWorker"in navigator){logger.leaveTrail("Service worker loader: Loading from page..."+window.location.href);const wb=new Workbox(`virtual-server-main.js?debug=false&route=${getRoute()}`,{updateViaCache:"none"});function serverReady(){console.log("Service worker loader: Server ready."),wb.messageSW({type:"INIT_PHOENIX_CONFIG",debugMode:"true"===window.logger.logToConsolePref,logLivePreview:window.logger.loggingOptions.logLivePreview}).then(config=>{logger.leaveTrail(`Service worker loader: Server ready! Service worker inited at base url: ${config.baseURL}`)}).catch(err=>{console.error("Service worker loader: Error while init of service worker",err)})}function serverInstall(){logger.leaveTrail("Service worker loader: Web server Worker installed.")}window.Phoenix.cache={},window.messageSW=function(params){return wb.messageSW(params)};const showSkipWaitingPrompt=async event=>{const updateAccepted=await shouldUpdate();updateAccepted&&wb.messageSkipWaiting()};wb.addEventListener("waiting",event=>{logger.leaveTrail("Service worker loader: A new service worker is pending load. Trying to update worker now."),window.Phoenix.cache.updatePendingReloadReason="skipWait",showSkipWaitingPrompt(event)}),wb.controlling.then(serverReady),wb.addEventListener("installed",event=>{event.isUpdate||serverInstall()}),wb.register()}