MCPcopy
hub / github.com/realdennis/md2pdf / registerValidSW

Function registerValidSW

src/serviceWorkerRegistration.js:55–97  ·  view source on GitHub ↗
(swUrl, config)

Source from the content-addressed store, hash-verified

53}
54
55function registerValidSW(swUrl, config) {
56 navigator.serviceWorker
57 .register(swUrl)
58 .then((registration) => {
59 registration.onupdatefound = () => {
60 const installingWorker = registration.installing;
61 if (installingWorker == null) {
62 return;
63 }
64 installingWorker.onstatechange = () => {
65 if (installingWorker.state === 'installed') {
66 if (navigator.serviceWorker.controller) {
67 // At this point, the updated precached content has been fetched,
68 // but the previous service worker will still serve the older
69 // content until all client tabs are closed.
70 console.log(
71 'New content is available and will be used when all ' +
72 'tabs for this page are closed. See https://cra.link/PWA.'
73 );
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
85 // Execute callback
86 if (config && config.onSuccess) {
87 config.onSuccess(registration);
88 }
89 }
90 }
91 };
92 };
93 })
94 .catch((error) => {
95 console.error('Error during service worker registration:', error);
96 });
97}
98
99function checkValidServiceWorker(swUrl, config) {
100 // Check if the service worker can be found. If it can't reload the page.

Callers 2

registerFunction · 0.85
checkValidServiceWorkerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected