Advertisement
Guest User

Untitled

a guest
Oct 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const isLocalhost = Boolean(
  2.   window.location.hostname === 'localhost' ||
  3.     // [::1] is the IPv6 localhost address.
  4.     window.location.hostname === '[::1]' ||
  5.     // 127.0.0.1/8 is considered localhost for IPv4.
  6.     window.location.hostname.match(
  7.       /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
  8.     )
  9. );
  10.  
  11. export default function register() {
  12.   if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
  13.     // The URL constructor is available in all browsers that support SW.
  14.     const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
  15.     if (publicUrl.origin !== window.location.origin) {
  16.       // Our service worker won't work if PUBLIC_URL is on a different origin
  17.       // from what our page is served on. This might happen if a CDN is used to
  18.       // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
  19.       return;
  20.     }
  21.  
  22.     window.addEventListener('load', () => {
  23.       const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  24.  
  25.       if (!isLocalhost) {
  26.         // Is not local host. Just register service worker
  27.         registerValidSW(swUrl);
  28.       } else {
  29.         // This is running on localhost. Lets check if a service worker still exists or not.
  30.         checkValidServiceWorker(swUrl);
  31.       }
  32.     });
  33.   }
  34. }
  35.  
  36. function registerValidSW(swUrl) {
  37.   navigator.serviceWorker
  38.     .register(swUrl)
  39.     .then(registration => {
  40.       registration.onupdatefound = () => {
  41.         const installingWorker = registration.installing;
  42.         installingWorker.onstatechange = () => {
  43.           if (installingWorker.state === 'installed') {
  44.             if (navigator.serviceWorker.controller) {
  45.               // At this point, the old content will have been purged and
  46.               // the fresh content will have been added to the cache.
  47.               // It's the perfect time to display a "New content is
  48.               // available; please refresh." message in your web app.
  49.               console.log('New content is available; please refresh.');
  50.             } else {
  51.               // At this point, everything has been precached.
  52.               // It's the perfect time to display a
  53.               // "Content is cached for offline use." message.
  54.               console.log('Content is cached for offline use.');
  55.             }
  56.           }
  57.         };
  58.       };
  59.     })
  60.     .catch(error => {
  61.       console.error('Error during service worker registration:', error);
  62.     });
  63. }
  64.  
  65. function checkValidServiceWorker(swUrl) {
  66.   // Check if the service worker can be found. If it can't reload the page.
  67.   fetch(swUrl)
  68.     .then(response => {
  69.       // Ensure service worker exists, and that we really are getting a JS file.
  70.       if (
  71.         response.status === 404 ||
  72.         response.headers.get('content-type').indexOf('javascript') === -1
  73.       ) {
  74.         // No service worker found. Probably a different app. Reload the page.
  75.         navigator.serviceWorker.ready.then(registration => {
  76.           registration.unregister().then(() => {
  77.             window.location.reload();
  78.           });
  79.         });
  80.       } else {
  81.         // Service worker found. Proceed as normal.
  82.         registerValidSW(swUrl);
  83.       }
  84.     })
  85.     .catch(() => {
  86.       console.log(
  87.         'No internet connection found. App is running in offline mode.'
  88.       );
  89.     });
  90. }
  91.  
  92. export function unregister() {
  93.   if ('serviceWorker' in navigator) {
  94.     navigator.serviceWorker.ready.then(registration => {
  95.       registration.unregister();
  96.     });
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement