From 1ffbea299da2acc4c2c586b7f675a70e3bd64321 Mon Sep 17 00:00:00 2001 From: NeOMakinG <14963751+NeOMakinG@users.noreply.github.com> Date: Fri, 28 Feb 2025 15:08:34 +0100 Subject: [PATCH] fix: remove that useless service worker --- src/index.tsx | 3 - src/service-worker.ts | 33 ---------- src/serviceWorkerRegistration.ts | 107 ------------------------------- 3 files changed, 143 deletions(-) delete mode 100644 src/service-worker.ts delete mode 100644 src/serviceWorkerRegistration.ts diff --git a/src/index.tsx b/src/index.tsx index 821cfeb4e78..23e1babf579 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -20,7 +20,6 @@ import { AppProviders } from './AppProviders' import { getConfig } from './config' import { renderConsoleArt } from './lib/consoleArt' import { reportWebVitals } from './lib/reportWebVitals' -import * as serviceWorkerRegistration from './serviceWorkerRegistration' import { httpClientIntegration } from './utils/sentry/httpclient' // Remove this condition to test sentry locally @@ -135,8 +134,6 @@ root.render( , ) -serviceWorkerRegistration.register() - // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals diff --git a/src/service-worker.ts b/src/service-worker.ts deleted file mode 100644 index f32e288f4b9..00000000000 --- a/src/service-worker.ts +++ /dev/null @@ -1,33 +0,0 @@ -/// - -declare const self: ServiceWorkerGlobalScope & - typeof globalThis & { __WB_MANIFEST: { revision: string | null; url: string } } - -export {} - -// create-react-app calls workbox-webpack-plugin.InjectManifest by default -// and will throw an error if __WEB_MANIFEST isn't referenced in the service worker file -// @ts-ignore: declaring manifest breaks the noUnusedLocals TS rule, but is required in this file as per the comments above -const manifest = self.__WB_MANIFEST /* eslint-disable-line @typescript-eslint/no-unused-vars */ - -self.addEventListener('activate', () => self.clients.claim()) - -self.addEventListener('install', event => { - event.waitUntil( - (async () => { - try { - await self.skipWaiting() - } catch (e) { - console.error('ServiceWorker:install:Error', e) - } - })(), - ) -}) - -self.addEventListener('message', event => { - if (event.data && event.data.type === 'SKIP_WAITING') { - self.skipWaiting() - } -}) - -self.addEventListener('fetch', _ => {}) diff --git a/src/serviceWorkerRegistration.ts b/src/serviceWorkerRegistration.ts deleted file mode 100644 index 1eb7928e73f..00000000000 --- a/src/serviceWorkerRegistration.ts +++ /dev/null @@ -1,107 +0,0 @@ -// The service-worker doesn't load when use `yarn dev`. You need to do a -// `yarn build && http-server build` and serve up the compiled output - -type ServiceWorkerCallbacks = { - onSuccess?: (registration: ServiceWorkerRegistration) => void - onUpdate?: (registration: ServiceWorkerRegistration) => void -} - -const isLocalhost = Boolean( - window.location.hostname === 'localhost' || - // [::1] is the IPv6 localhost address. - window.location.hostname === '[::1]' || - // 127.0.0.0/8 are considered localhost for IPv4. - window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/), -) - -// --- Internal Functions --- - -async function registerValidSW(swUrl: string, callbacks?: ServiceWorkerCallbacks) { - try { - const registration = await navigator.serviceWorker.register(swUrl) - registration.onupdatefound = () => { - const installingWorker = registration.installing - if (!installingWorker) return - - installingWorker.onstatechange = () => { - if (installingWorker.state === 'installed') { - if (navigator.serviceWorker.controller) { - // At this point, the updated precached content has been fetched, - // but the previous service worker will still serve the older - // content until all client tabs are closed. - callbacks?.onUpdate?.(registration) - } else { - // At this point, everything has been precached. - callbacks?.onSuccess?.(registration) - } - } - } - } - } catch (e) { - console.error('ServiceWorker:registerValidSW:Error', e) - } -} - -async function checkValidServiceWorker(swUrl: string, callbacks?: ServiceWorkerCallbacks) { - // Check if the service worker can be found. If it can't reload the page. - try { - const response = await fetch(swUrl, { headers: { 'Service-Worker': 'script' } }) - // Ensure service worker exists, and that we really are getting a JS file. - const contentType = response.headers.get('content-type') - if (response.status === 404 || contentType?.indexOf('javascript') === -1) { - // No service worker found. Probably a different app. Reload the page. - const reg = await navigator.serviceWorker.ready - await reg.unregister() - window.location.reload() - } else { - // Service worker found. Proceed as normal. - await registerValidSW(swUrl, callbacks) - } - } catch (e) { - console.warn('ServiceWorker:checkValidServiceWorker - No internet connection found.') - } -} - -// --- Exported Functions --- - -/** - * Unregister the service worker - */ -export async function unregister() { - if ('serviceWorker' in navigator) { - try { - const reg = await navigator.serviceWorker.ready - await reg.unregister() - } catch (e) { - console.error('ServiceWorker:unregister:Error', e) - } - } -} - -/** - * Register the service worker - */ -export function register(callbacks?: ServiceWorkerCallbacks) { - // @TODO: import.meta.env.NODE_ENV === 'production' - if ('serviceWorker' in navigator) { - const publicUrl = new URL(import.meta.env.PUBLIC_URL, window.location.href) - - if (publicUrl.origin !== window.location.origin) { - // Our service worker won't work if PUBLIC_URL is on a different origin - // from what our page is served on. This might happen if a CDN is used to - // serve assets; see https://github.com/facebook/create-react-app/issues/2374 - return - } - - window.addEventListener('load', () => { - const swUrl = `${import.meta.env.PUBLIC_URL}/service-worker.js` - if (isLocalhost) { - // This is running on localhost. Let's check if a service worker still exists or not. - void checkValidServiceWorker(swUrl, callbacks) - } else { - // Is not localhost. Just register service worker - void registerValidSW(swUrl, callbacks) - } - }) - } -}