registerServiceWorker.js 4.0 KB

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