(swUrl: string, config?: Config)
| 58 | } |
| 59 | |
| 60 | function registerValidSW(swUrl: string, config?: Config) { |
| 61 | navigator.serviceWorker |
| 62 | .register(swUrl) |
| 63 | .then((registration) => { |
| 64 | registration.onupdatefound = () => { |
| 65 | const installingWorker = registration.installing; |
| 66 | if (installingWorker == null) { |
| 67 | return; |
| 68 | } |
| 69 | installingWorker.onstatechange = () => { |
| 70 | if (installingWorker.state === 'installed') { |
| 71 | if (navigator.serviceWorker.controller) { |
| 72 | // At this point, the updated precached content has been fetched, |
| 73 | // but the previous service worker will still serve the older |
| 74 | // content until all client tabs are closed. |
| 75 | console.log( |
| 76 | 'New content is available and will be used when all ' + |
| 77 | 'tabs for this page are closed. See https://cra.link/PWA.' |
| 78 | ); |
| 79 | |
| 80 | // Execute callback |
| 81 | if (config && config.onUpdate) { |
| 82 | config.onUpdate(registration); |
| 83 | } |
| 84 | } else { |
| 85 | // At this point, everything has been precached. |
| 86 | // It's the perfect time to display a |
| 87 | // "Content is cached for offline use." message. |
| 88 | console.log('Content is cached for offline use.'); |
| 89 | |
| 90 | // Execute callback |
| 91 | if (config && config.onSuccess) { |
| 92 | config.onSuccess(registration); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | }; |
| 98 | }) |
| 99 | .catch((error) => { |
| 100 | console.error('Error during service worker registration:', error); |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | function checkValidServiceWorker(swUrl: string, config?: Config) { |
| 105 | // Check if the service worker can be found. If it can't reload the page. |
no outgoing calls
no test coverage detected