MCPcopy Create free account
hub / github.com/expo/examples / registerValidSW

Function registerValidSW

with-workbox/src/serviceWorkerRegistration.js:58–100  ·  view source on GitHub ↗
(swUrl, config)

Source from the content-addressed store, hash-verified

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