(swUrl, config)
| 19 | ) |
| 20 | |
| 21 | function registerValidSW(swUrl, config) { |
| 22 | navigator.serviceWorker |
| 23 | .register(swUrl) |
| 24 | .then((registration) => { |
| 25 | registration.onupdatefound = () => { |
| 26 | const installingWorker = registration.installing |
| 27 | if (installingWorker == null) { |
| 28 | return |
| 29 | } |
| 30 | installingWorker.onstatechange = () => { |
| 31 | if (installingWorker.state === 'installed') { |
| 32 | if (navigator.serviceWorker.controller) { |
| 33 | // At this point, the updated precached content has been fetched, |
| 34 | // but the previous service worker will still serve the older |
| 35 | // content until all client tabs are closed. |
| 36 | console.info( |
| 37 | 'New content is available and will be used when all tabs for this page are closed. See https://bit.ly/CRA-PWA.' |
| 38 | ) |
| 39 | |
| 40 | // Execute callback |
| 41 | if (config && config.onUpdate) { |
| 42 | config.onUpdate(registration) |
| 43 | } |
| 44 | } else { |
| 45 | // At this point, everything has been precached. |
| 46 | // It's the perfect time to display a |
| 47 | // "Content is cached for offline use." message. |
| 48 | console.info('Content is cached for offline use.') |
| 49 | |
| 50 | // Execute callback |
| 51 | if (config && config.onSuccess) { |
| 52 | config.onSuccess(registration) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | }) |
| 59 | .catch((error) => { |
| 60 | console.error('Error during service worker registration:', error) |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | function checkValidServiceWorker(swUrl, config) { |
| 65 | // Check if the service worker can be found. If it can't reload the page. |
no test coverage detected