Simple monitor for displaying MIDI status for digital pianos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

150 lines
5.2 KiB

  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://bit.ly/CRA-PWA
  10. const isLocalhost = Boolean(
  11. window.location.hostname === 'localhost' ||
  12. // [::1] is the IPv6 localhost address.
  13. window.location.hostname === '[::1]' ||
  14. // 127.0.0.0/8 are considered localhost for IPv4.
  15. window.location.hostname.match(
  16. /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
  17. )
  18. );
  19. type Config = {
  20. onSuccess?: (registration: ServiceWorkerRegistration) => void;
  21. onUpdate?: (registration: ServiceWorkerRegistration) => void;
  22. };
  23. export function register(config?: Config) {
  24. if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
  25. // The URL constructor is available in all browsers that support SW.
  26. const publicUrl = new URL(
  27. process.env.PUBLIC_URL,
  28. window.location.href
  29. );
  30. if (publicUrl.origin !== window.location.origin) {
  31. // Our service worker won't work if PUBLIC_URL is on a different origin
  32. // from what our page is served on. This might happen if a CDN is used to
  33. // serve assets; see https://github.com/facebook/create-react-app/issues/2374
  34. return;
  35. }
  36. window.addEventListener('load', () => {
  37. const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  38. if (isLocalhost) {
  39. // This is running on localhost. Let's check if a service worker still exists or not.
  40. checkValidServiceWorker(swUrl, config);
  41. // Add some additional logging to localhost, pointing developers to the
  42. // service worker/PWA documentation.
  43. navigator.serviceWorker.ready.then(() => {
  44. console.log(
  45. 'This web app is being served cache-first by a service ' +
  46. 'worker. To learn more, visit https://bit.ly/CRA-PWA'
  47. );
  48. });
  49. } else {
  50. // Is not localhost. Just register service worker
  51. registerValidSW(swUrl, config);
  52. }
  53. });
  54. }
  55. }
  56. function registerValidSW(swUrl: string, config?: Config) {
  57. navigator.serviceWorker
  58. .register(swUrl)
  59. .then(registration => {
  60. registration.onupdatefound = () => {
  61. const installingWorker = registration.installing;
  62. if (installingWorker == null) {
  63. return;
  64. }
  65. installingWorker.onstatechange = () => {
  66. if (installingWorker.state === 'installed') {
  67. if (navigator.serviceWorker.controller) {
  68. // At this point, the updated precached content has been fetched,
  69. // but the previous service worker will still serve the older
  70. // content until all client tabs are closed.
  71. console.log(
  72. 'New content is available and will be used when all ' +
  73. 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
  74. );
  75. // Execute callback
  76. if (config && config.onUpdate) {
  77. config.onUpdate(registration);
  78. }
  79. } else {
  80. // At this point, everything has been precached.
  81. // It's the perfect time to display a
  82. // "Content is cached for offline use." message.
  83. console.log('Content is cached for offline use.');
  84. // Execute callback
  85. if (config && config.onSuccess) {
  86. config.onSuccess(registration);
  87. }
  88. }
  89. }
  90. };
  91. };
  92. })
  93. .catch(error => {
  94. console.error('Error during service worker registration:', error);
  95. });
  96. }
  97. function checkValidServiceWorker(swUrl: string, config?: Config) {
  98. // Check if the service worker can be found. If it can't reload the page.
  99. fetch(swUrl, {
  100. headers: { 'Service-Worker': 'script' }
  101. })
  102. .then(response => {
  103. // Ensure service worker exists, and that we really are getting a JS file.
  104. const contentType = response.headers.get('content-type');
  105. if (
  106. response.status === 404 ||
  107. (contentType != null && contentType.indexOf('javascript') === -1)
  108. ) {
  109. // No service worker found. Probably a different app. Reload the page.
  110. navigator.serviceWorker.ready.then(registration => {
  111. registration.unregister().then(() => {
  112. window.location.reload();
  113. });
  114. });
  115. } else {
  116. // Service worker found. Proceed as normal.
  117. registerValidSW(swUrl, config);
  118. }
  119. })
  120. .catch(() => {
  121. console.log(
  122. 'No internet connection found. App is running in offline mode.'
  123. );
  124. });
  125. }
  126. export function unregister() {
  127. if ('serviceWorker' in navigator) {
  128. navigator.serviceWorker.ready
  129. .then(registration => {
  130. registration.unregister();
  131. })
  132. .catch(error => {
  133. console.error(error.message);
  134. });
  135. }
  136. }