Created
December 10, 2020 17:52
-
-
Save gcangussu/ba064825a9beb350ce7f39a7b8f63a3d to your computer and use it in GitHub Desktop.
Avoid blinking on first visit with service worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* When we have a workbox we must be sure to just render the | |
* app when we assert that we are running the latest version. | |
* Otherwise the user can start interacting with the app and | |
* right after an update arrives and the page is reloaded. | |
*/ | |
if (wb != null) { | |
wb.active | |
.then((serviceWorker) => { | |
/* When the service worker active is not controlling the | |
* current page, we trigger a page reload. So we should | |
* not render the app to avoid the page blinking. */ | |
if (navigator.serviceWorker.controller === serviceWorker) { | |
renderApp(); | |
return; | |
} | |
setTimeout(() => { | |
const error = new Error('Page did not reload when expected'); | |
logException('New service worker must trigger a page reload', error); | |
renderApp(); | |
}, 500); | |
}) | |
.catch((error) => { | |
logException('Error resolving active service worker', error); | |
renderApp(); | |
}); | |
} else { | |
renderApp(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment