Skip to content

Instantly share code, notes, and snippets.

@baorv
Created July 3, 2018 02:21
Show Gist options
  • Select an option

  • Save baorv/d3079294187a76f7e5ea38ec5f8461b2 to your computer and use it in GitHub Desktop.

Select an option

Save baorv/d3079294187a76f7e5ea38ec5f8461b2 to your computer and use it in GitHub Desktop.
export default function register() {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
window.addEventListener('load', () => {
const swUrl = 'service-worker.js';
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
console.log('New content is available; please refresh.');
} else {
console.log('Content is cached for offline use.');
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
});
}
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.unregister();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment