(swUrl, config)
| 75 | } |
| 76 | |
| 77 | function registerValidSW (swUrl, config) { |
| 78 | navigator.serviceWorker |
| 79 | .register(swUrl) |
| 80 | .then(registration => { |
| 81 | registration.onupdatefound = () => { |
| 82 | const installingWorker = registration.installing |
| 83 | if (installingWorker == null) { |
| 84 | return |
| 85 | } |
| 86 | installingWorker.onstatechange = () => { |
| 87 | if (installingWorker.state === 'installed') { |
| 88 | if (navigator.serviceWorker.controller != null) { |
| 89 | // At this point, the updated precached content has been fetched, |
| 90 | // but the previous service worker will still serve the older |
| 91 | // content until all client tabs are closed. |
| 92 | console.log( |
| 93 | 'New content is available and will be used when all ' + |
| 94 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' |
| 95 | ) |
| 96 | |
| 97 | // Execute callback |
| 98 | if (config && config.onUpdate) { |
| 99 | config.onUpdate(registration) |
| 100 | } |
| 101 | } else { |
| 102 | // At this point, everything has been precached. |
| 103 | // It's the perfect time to display a |
| 104 | // "Content is cached for offline use." message. |
| 105 | console.log('Content is cached for offline use.') |
| 106 | |
| 107 | // Execute callback |
| 108 | if (config && config.onSuccess) { |
| 109 | config.onSuccess(registration) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | }) |
| 116 | .catch(error => { |
| 117 | console.error('Error during service worker registration:', error) |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | function checkValidServiceWorker (swUrl, config) { |
| 122 | // Check if the service worker can be found. If it can't reload the page. |
no test coverage detected