Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created August 28, 2019 06:10
Show Gist options
  • Save tkssharma/2083f34bf73b153d372cfa523eeab083 to your computer and use it in GitHub Desktop.
Save tkssharma/2083f34bf73b153d372cfa523eeab083 to your computer and use it in GitHub Desktop.
export default function register() {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/custom-sw.js`;
if (isLocalhost) {
// This is running on localhost. Lets check if a service worker still exists or not.
checkValidServiceWorker(swUrl);
// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://goo.gl/SC7cgQ',
);
});
} else {
// Is not local host. Just register service worker
registerValidSW(swUrl);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment