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

Function checkValidServiceWorker

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

Source from the content-addressed store, hash-verified

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

Callers 1

registerFunction · 0.85

Calls 1

registerValidSWFunction · 0.85

Tested by

no test coverage detected