serviceWorker.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.register = register;
  6. exports.unregister = unregister;
  7. // This optional code is used to register a service worker.
  8. // register() is not called by default.
  9. // This lets the app load faster on subsequent visits in production, and gives
  10. // it offline capabilities. However, it also means that developers (and users)
  11. // will only see deployed updates on subsequent visits to a page, after all the
  12. // existing tabs open on the page have been closed, since previously cached
  13. // resources are updated in the background.
  14. // To learn more about the benefits of this model and instructions on how to
  15. // opt-in, read http://bit.ly/CRA-PWA
  16. var isLocalhost = Boolean(window.location.hostname === 'localhost' || // [::1] is the IPv6 localhost address.
  17. window.location.hostname === '[::1]' || // 127.0.0.1/8 is considered localhost for IPv4.
  18. window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
  19. function register(config) {
  20. if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
  21. // The URL constructor is available in all browsers that support SW.
  22. var publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
  23. if (publicUrl.origin !== window.location.origin) {
  24. // Our service worker won't work if PUBLIC_URL is on a different origin
  25. // from what our page is served on. This might happen if a CDN is used to
  26. // serve assets; see https://github.com/facebook/create-react-app/issues/2374
  27. return;
  28. }
  29. window.addEventListener('load', function () {
  30. var swUrl = "".concat(process.env.PUBLIC_URL, "/service-worker.js");
  31. if (isLocalhost) {
  32. // This is running on localhost. Let's check if a service worker still exists or not.
  33. checkValidServiceWorker(swUrl, config); // Add some additional logging to localhost, pointing developers to the
  34. // service worker/PWA documentation.
  35. navigator.serviceWorker.ready.then(function () {
  36. console.log('This web app is being served cache-first by a service ' + 'worker. To learn more, visit http://bit.ly/CRA-PWA');
  37. });
  38. } else {
  39. // Is not localhost. Just register service worker
  40. registerValidSW(swUrl, config);
  41. }
  42. });
  43. }
  44. }
  45. function registerValidSW(swUrl, config) {
  46. navigator.serviceWorker.register(swUrl).then(function (registration) {
  47. registration.onupdatefound = function () {
  48. var installingWorker = registration.installing;
  49. if (installingWorker == null) {
  50. return;
  51. }
  52. installingWorker.onstatechange = function () {
  53. if (installingWorker.state === 'installed') {
  54. if (navigator.serviceWorker.controller) {
  55. // At this point, the updated precached content has been fetched,
  56. // but the previous service worker will still serve the older
  57. // content until all client tabs are closed.
  58. console.log('New content is available and will be used when all ' + 'tabs for this page are closed. See http://bit.ly/CRA-PWA.'); // Execute callback
  59. if (config && config.onUpdate) {
  60. config.onUpdate(registration);
  61. }
  62. } else {
  63. // At this point, everything has been precached.
  64. // It's the perfect time to display a
  65. // "Content is cached for offline use." message.
  66. console.log('Content is cached for offline use.'); // Execute callback
  67. if (config && config.onSuccess) {
  68. config.onSuccess(registration);
  69. }
  70. }
  71. }
  72. };
  73. };
  74. }).catch(function (error) {
  75. console.error('Error during service worker registration:', error);
  76. });
  77. }
  78. function checkValidServiceWorker(swUrl, config) {
  79. // Check if the service worker can be found. If it can't reload the page.
  80. fetch(swUrl).then(function (response) {
  81. // Ensure service worker exists, and that we really are getting a JS file.
  82. var contentType = response.headers.get('content-type');
  83. if (response.status === 404 || contentType != null && contentType.indexOf('javascript') === -1) {
  84. // No service worker found. Probably a different app. Reload the page.
  85. navigator.serviceWorker.ready.then(function (registration) {
  86. registration.unregister().then(function () {
  87. window.location.reload();
  88. });
  89. });
  90. } else {
  91. // Service worker found. Proceed as normal.
  92. registerValidSW(swUrl, config);
  93. }
  94. }).catch(function () {
  95. console.log('No internet connection found. App is running in offline mode.');
  96. });
  97. }
  98. function unregister() {
  99. if ('serviceWorker' in navigator) {
  100. navigator.serviceWorker.ready.then(function (registration) {
  101. registration.unregister();
  102. });
  103. }
  104. }