serviceWorkerRegistration.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // This optional code is used to register a service worker.
  2. // register() is not called by default.
  3. // This lets the app load faster on subsequent visits in production, and gives
  4. // it offline capabilities. However, it also means that developers (and users)
  5. // will only see deployed updates on subsequent visits to a page, after all the
  6. // existing tabs open on the page have been closed, since previously cached
  7. // resources are updated in the background.
  8. // To learn more about the benefits of this model and instructions on how to
  9. // opt-in, read https://cra.link/PWA
  10. const isLocalhost = true
  11. // Boolean(
  12. // window.location.hostname === 'localhost' ||
  13. // window.location.hostname === '192.168.0.109' ||
  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 ('serviceWorker' in navigator) {
  21. console.log('registering..')
  22. // The URL constructor is available in all browsers that support SW.
  23. const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
  24. console.log(publicUrl)
  25. if (publicUrl.origin !== window.location.origin) {
  26. // Our service worker won't work if PUBLIC_URL is on a different origin
  27. // from what our page is served on. This might happen if a CDN is used to
  28. // serve assets; see https://github.com/facebook/create-react-app/issues/2374
  29. return;
  30. }
  31. window.addEventListener('load', () => {
  32. const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  33. console.log(swUrl, config)
  34. registerValidSW(swUrl, config);
  35. });
  36. }
  37. }
  38. function registerValidSW(swUrl, config) {
  39. navigator.serviceWorker
  40. .register(swUrl)
  41. .then((registration) => {
  42. console.log('registration...')
  43. registration.onupdatefound = () => {
  44. const installingWorker = registration.installing;
  45. if (installingWorker == null) {
  46. return;
  47. }
  48. installingWorker.onstatechange = () => {
  49. if (installingWorker.state === 'installed') {
  50. if (navigator.serviceWorker.controller) {
  51. // At this point, the updated precached content has been fetched,
  52. // but the previous service worker will still serve the older
  53. // content until all client tabs are closed.
  54. console.log(
  55. 'New content is available and will be used when all ' +
  56. 'tabs for this page are closed. See https://cra.link/PWA.'
  57. );
  58. // 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.');
  67. // Execute callback
  68. if (config && config.onSuccess) {
  69. config.onSuccess(registration);
  70. }
  71. }
  72. }
  73. };
  74. };
  75. })
  76. .catch((error) => {
  77. console.error('Error during service worker registration:', error);
  78. });
  79. }
  80. function checkValidServiceWorker(swUrl, config) {
  81. // Check if the service worker can be found. If it can't reload the page.
  82. fetch(swUrl, {
  83. headers: { 'Service-Worker': 'script' },
  84. })
  85. .then((response) => {
  86. // Ensure service worker exists, and that we really are getting a JS file.
  87. const contentType = response.headers.get('content-type');
  88. if (
  89. response.status === 404 ||
  90. (contentType != null && contentType.indexOf('javascript') === -1)
  91. ) {
  92. // No service worker found. Probably a different app. Reload the page.
  93. navigator.serviceWorker.ready.then((registration) => {
  94. registration.unregister().then(() => {
  95. window.location.reload();
  96. });
  97. });
  98. } else {
  99. // Service worker found. Proceed as normal.
  100. registerValidSW(swUrl, config);
  101. }
  102. })
  103. .catch(() => {
  104. console.log('No internet connection found. App is running in offline mode.');
  105. });
  106. }
  107. // export function unregister() {
  108. // if ('serviceWorker' in navigator) {
  109. // navigator.serviceWorker.ready
  110. // .then((registration) => {
  111. // registration.unregister();
  112. // })
  113. // .catch((error) => {
  114. // console.error(error.message);
  115. // });
  116. // }
  117. // }