Created
July 31, 2019 18:13
-
-
Save FezVrasta/aacc2fc36b5094fdf17feba00d0d4a0d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function registerValidSW(swUrl, config) { | |
navigator.serviceWorker | |
.register(swUrl) | |
.then(registration => { | |
registration.onupdatefound = () => { | |
const installingWorker = registration.installing; | |
if (installingWorker == null) { | |
return; | |
} | |
installingWorker.onstatechange = () => { | |
if (installingWorker.state === 'installed') { | |
if (navigator.serviceWorker.controller) { | |
// At this point, the updated precached content has been fetched, | |
// but the previous service worker will still serve the older | |
// content until all client tabs are closed. | |
console.log( | |
'New content is available and will be used when all ' + | |
'tabs for this page are closed. See https://bit.ly/CRA-PWA.' | |
); | |
// Execute callback | |
if (config && config.onUpdate) { | |
config.onUpdate(registration); | |
} | |
} else { | |
// At this point, everything has been precached. | |
// It's the perfect time to display a | |
// "Content is cached for offline use." message. | |
console.log('Content is cached for offline use.'); | |
// Execute callback | |
if (config && config.onSuccess) { | |
config.onSuccess(registration); | |
} | |
} | |
} | |
// OUR EDIT BEGINS HERE | |
else if (installingWorker.state === 'activated') { | |
// eslint-disable-next-line no-console | |
console.log('A service worker has been activated; please refresh.'); | |
if (config && config.onActivated) { | |
config.onActivated(registration); | |
} | |
} | |
// OUR EDIT ENDS HERE | |
}; | |
}; | |
}) | |
.catch(error => { | |
console.error('Error during service worker registration:', error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment