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

Function checkValidServiceWorker

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

Source from the content-addressed store, hash-verified

100}
101
102function checkValidServiceWorker(swUrl, config) {
103 // Check if the service worker can be found. If it can't reload the page.
104 fetch(swUrl, {
105 headers: { "Service-Worker": "script" },
106 })
107 .then((response) => {
108 // Ensure service worker exists, and that we really are getting a JS file.
109 const contentType = response.headers.get("content-type");
110 if (
111 response.status === 404 ||
112 (contentType != null && contentType.indexOf("javascript") === -1)
113 ) {
114 // No service worker found. Probably a different app. Reload the page.
115 navigator.serviceWorker.ready.then((registration) => {
116 registration.unregister().then(() => {
117 window.location.reload();
118 });
119 });
120 } else {
121 // Service worker found. Proceed as normal.
122 registerValidSW(swUrl, config);
123 }
124 })
125 .catch(() => {
126 console.log(
127 "No internet connection found. App is running in offline mode."
128 );
129 });
130}
131
132export function unregister() {
133 if ("serviceWorker" in navigator) {

Callers 1

registerFunction · 0.85

Calls 1

registerValidSWFunction · 0.85

Tested by

no test coverage detected