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. console.log('start sw...')
  20. export function register(config) {
  21. if ('serviceWorker' in navigator) {
  22. console.log('registering..')
  23. // The URL constructor is available in all browsers that support SW.
  24. const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
  25. console.log(publicUrl)
  26. if (publicUrl.origin !== window.location.origin) {
  27. // Our service worker won't work if PUBLIC_URL is on a different origin
  28. // from what our page is served on. This might happen if a CDN is used to
  29. // serve assets; see https://github.com/facebook/create-react-app/issues/2374
  30. return;
  31. }
  32. window.addEventListener('load', () => {
  33. const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  34. console.log(swUrl, config)
  35. registerValidSW(swUrl, config);
  36. });
  37. }
  38. }
  39. function registerValidSW(swUrl, config) {
  40. navigator.serviceWorker
  41. .register(swUrl)
  42. .then((registration) => {
  43. console.log('registration...')
  44. registration.onupdatefound = () => {
  45. const installingWorker = registration.installing;
  46. if (installingWorker == null) {
  47. return;
  48. }
  49. installingWorker.onstatechange = () => {
  50. if (installingWorker.state === 'installed') {
  51. if (navigator.serviceWorker.controller) {
  52. // At this point, the updated precached content has been fetched,
  53. // but the previous service worker will still serve the older
  54. // content until all client tabs are closed.
  55. console.log(
  56. 'New content is available and will be used when all ' +
  57. 'tabs for this page are closed. See https://cra.link/PWA.'
  58. );
  59. // Execute callback
  60. if (config && config.onUpdate) {
  61. config.onUpdate(registration);
  62. }
  63. } else {
  64. // At this point, everything has been precached.
  65. // It's the perfect time to display a
  66. // "Content is cached for offline use." message.
  67. console.log('Content is cached for offline use.');
  68. // Execute callback
  69. if (config && config.onSuccess) {
  70. config.onSuccess(registration);
  71. }
  72. }
  73. }
  74. };
  75. };
  76. })
  77. .catch((error) => {
  78. console.error('Error during service worker registration:', error);
  79. });
  80. }
  81. function checkValidServiceWorker(swUrl, config) {
  82. // Check if the service worker can be found. If it can't reload the page.
  83. fetch(swUrl, {
  84. headers: { 'Service-Worker': 'script' },
  85. })
  86. .then((response) => {
  87. // Ensure service worker exists, and that we really are getting a JS file.
  88. const contentType = response.headers.get('content-type');
  89. if (
  90. response.status === 404 ||
  91. (contentType != null && contentType.indexOf('javascript') === -1)
  92. ) {
  93. // No service worker found. Probably a different app. Reload the page.
  94. navigator.serviceWorker.ready.then((registration) => {
  95. registration.unregister().then(() => {
  96. window.location.reload();
  97. });
  98. });
  99. } else {
  100. // Service worker found. Proceed as normal.
  101. registerValidSW(swUrl, config);
  102. }
  103. })
  104. .catch(() => {
  105. console.log('No internet connection found. App is running in offline mode.');
  106. });
  107. }
  108. // export function unregister() {
  109. // if ('serviceWorker' in navigator) {
  110. // navigator.serviceWorker.ready
  111. // .then((registration) => {
  112. // registration.unregister();
  113. // })
  114. // .catch((error) => {
  115. // console.error(error.message);
  116. // });
  117. // }
  118. // }