Skip to content

Instantly share code, notes, and snippets.

@gcangussu
Created December 10, 2020 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcangussu/ba064825a9beb350ce7f39a7b8f63a3d to your computer and use it in GitHub Desktop.
Save gcangussu/ba064825a9beb350ce7f39a7b8f63a3d to your computer and use it in GitHub Desktop.
Avoid blinking on first visit with service worker
/*
* 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