serviceWorker.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* eslint-disable */
  2. /* @ts-nocheck */
  3. // This optional code is used to register a service worker.
  4. // register() is not called by default.
  5. // This lets the app load faster on subsequent visits in production, and gives
  6. // it offline capabilities. However, it also means that developers (and users)
  7. // will only see deployed updates on subsequent visits to a page, after all the
  8. // existing tabs open on the page have been closed, since previously cached
  9. // resources are updated in the background.
  10. // To learn more about the benefits of this model and instructions on how to
  11. // opt-in, read https://bit.ly/CRA-PWA
  12. const isLocalhost = Boolean(
  13. window.location.hostname === 'localhost' ||
  14. // [::1] is the IPv6 localhost address.
  15. window.location.hostname === '[::1]' ||
  16. // 127.0.0.0/8 are considered localhost for IPv4.
  17. window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/),
  18. );
  19. export 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. const 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', () => {
  30. const swUrl = `${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);
  34. // Add some additional logging to localhost, pointing developers to the
  35. // service worker/PWA documentation.
  36. navigator.serviceWorker.ready.then(() => {
  37. console.log(
  38. 'This web app is being served cache-first by a service ' +
  39. 'worker. To learn more, visit https://bit.ly/CRA-PWA',
  40. );
  41. });
  42. } else {
  43. // Is not localhost. Just register service worker
  44. registerValidSW(swUrl, config);
  45. }
  46. });
  47. }
  48. }
  49. function registerValidSW(swUrl, config) {
  50. navigator.serviceWorker
  51. .register(swUrl)
  52. .then((registration) => {
  53. registration.onupdatefound = () => {
  54. const installingWorker = registration.installing;
  55. if (installingWorker == null) {
  56. return;
  57. }
  58. installingWorker.onstatechange = () => {
  59. if (installingWorker.state === 'installed') {
  60. if (navigator.serviceWorker.controller) {
  61. // At this point, the updated precached content has been fetched,
  62. // but the previous service worker will still serve the older
  63. // content until all client tabs are closed.
  64. console.log(
  65. 'New content is available and will be used when all ' +
  66. 'tabs for this page are closed. See https://bit.ly/CRA-PWA.',
  67. );
  68. // Execute callback
  69. if (config && config.onUpdate) {
  70. config.onUpdate(registration);
  71. }
  72. } else {
  73. // At this point, everything has been precached.
  74. // It's the perfect time to display a
  75. // "Content is cached for offline use." message.
  76. console.log('Content is cached for offline use.');
  77. // Execute callback
  78. if (config && config.onSuccess) {
  79. config.onSuccess(registration);
  80. }
  81. }
  82. }
  83. };
  84. };
  85. })
  86. .catch((error) => {
  87. console.error('Error during service worker registration:', error);
  88. });
  89. }
  90. function checkValidServiceWorker(swUrl, config) {
  91. // Check if the service worker can be found. If it can't reload the page.
  92. fetch(swUrl, {
  93. headers: { 'Service-Worker': 'script' },
  94. })
  95. .then((response) => {
  96. // Ensure service worker exists, and that we really are getting a JS file.
  97. const contentType = response.headers.get('content-type');
  98. if (
  99. response.status === 404 ||
  100. (contentType != null && contentType.indexOf('javascript') === -1)
  101. ) {
  102. // No service worker found. Probably a different app. Reload the page.
  103. navigator.serviceWorker.ready.then((registration) => {
  104. registration.unregister().then(() => {
  105. window.location.reload();
  106. });
  107. });
  108. } else {
  109. // Service worker found. Proceed as normal.
  110. registerValidSW(swUrl, config);
  111. }
  112. })
  113. .catch(() => {
  114. console.log('No internet connection found. App is running in offline mode.');
  115. });
  116. }
  117. export function unregister() {
  118. if ('serviceWorker' in navigator) {
  119. navigator.serviceWorker.ready
  120. .then((registration) => {
  121. registration.unregister();
  122. })
  123. .catch((error) => {
  124. console.error(error.message);
  125. });
  126. }
  127. }