From f4263e49b5ca1d7e26a29ea21cf6e4b0bacf33dc Mon Sep 17 00:00:00 2001 From: GermanBluefox Date: Wed, 29 Jan 2025 18:08:51 +0000 Subject: [PATCH] Fixing build --- package.json | 2 + .../{serviceWorker.js => serviceWorker.tsx} | 104 ++++++++++-------- 2 files changed, 60 insertions(+), 46 deletions(-) rename src-admin/src/{serviceWorker.js => serviceWorker.tsx} (76%) diff --git a/package.json b/package.json index d0cc8f1..757acfd 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,9 @@ "@types/jsonwebtoken": "^9.0.8", "jsonwebtoken": "^9.0.2", "chai": "^4.5.0", + "colorette": "^2.0.20", "mocha": "^11.1.0", + "puppeteer": "^24.1.1", "typescript": "~5.7.3" }, "bugs": { diff --git a/src-admin/src/serviceWorker.js b/src-admin/src/serviceWorker.tsx similarity index 76% rename from src-admin/src/serviceWorker.js rename to src-admin/src/serviceWorker.tsx index d9f7777..aa824a5 100644 --- a/src-admin/src/serviceWorker.js +++ b/src-admin/src/serviceWorker.tsx @@ -1,3 +1,4 @@ +/* eslint-disable */ // This optional code is used to register a service worker. // register() is not called by default. @@ -12,13 +13,53 @@ const isLocalhost = Boolean( window.location.hostname === 'localhost' || - // [::1] is the IPv6 localhost address. - window.location.hostname === '[::1]' || - // 127.0.0.1/8 is considered localhost for IPv4. - window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/), + // [::1] is the IPv6 localhost address. + window.location.hostname === '[::1]' || + // 127.0.0.1/8 is considered localhost for IPv4. + window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/), ); -function registerValidSW(swUrl, config) { +export function register(config?: { + onUpdate: (r: ServiceWorkerRegistration) => void; + onSuccess: (r: ServiceWorkerRegistration) => void; +}): void { + if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { + // The URL constructor is available in all browsers that support SW. + const publicUrl = new 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 = `./service-worker.js`; + + if (isLocalhost) { + // This is running on localhost. Let's check if a service worker still exists or not. + checkValidServiceWorker(swUrl, config); + + // Add some additional logging to localhost, pointing developers to the + // service worker/PWA documentation. + navigator.serviceWorker.ready.then(() => { + console.log( + 'This web app is being served cache-first by a service ' + + 'worker. To learn more, visit http://bit.ly/CRA-PWA', + ); + }); + } else { + // Is not localhost. Just register service worker + registerValidSW(swUrl, config); + } + }); + } +} + +function registerValidSW( + swUrl: string | URL, + config?: { onUpdate: (r: ServiceWorkerRegistration) => void; onSuccess: (r: ServiceWorkerRegistration) => void }, +): void { navigator.serviceWorker .register(swUrl) .then(registration => { @@ -35,11 +76,11 @@ function registerValidSW(swUrl, config) { // content until all client tabs are closed. console.log( 'New content is available and will be used when all ' + - 'tabs for this page are closed. See http://bit.ly/CRA-PWA.', + 'tabs for this page are closed. See http://bit.ly/CRA-PWA.', ); // Execute callback - if (config && config.onUpdate) { + if (config?.onUpdate) { config.onUpdate(registration); } } else { @@ -49,7 +90,7 @@ function registerValidSW(swUrl, config) { console.log('Content is cached for offline use.'); // Execute callback - if (config && config.onSuccess) { + if (config?.onSuccess) { config.onSuccess(registration); } } @@ -62,13 +103,16 @@ function registerValidSW(swUrl, config) { }); } -function checkValidServiceWorker(swUrl, config) { +function checkValidServiceWorker( + swUrl: string | URL, + config?: { onUpdate: (r: ServiceWorkerRegistration) => void; onSuccess: (r: ServiceWorkerRegistration) => void }, +): void { // Check if the service worker can be found. If it can't reload the page. fetch(swUrl) .then(response => { // 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 !== null && !contentType.includes('javascript'))) { + if (response.status === 404 || (contentType !== null && contentType.indexOf('javascript') === -1)) { // No service worker found. Probably a different app. Reload the page. navigator.serviceWorker.ready.then(registration => { registration.unregister().then(() => { @@ -80,47 +124,15 @@ function checkValidServiceWorker(swUrl, config) { registerValidSW(swUrl, config); } }) - .catch(() => console.log('No internet connection found. App is running in offline mode.')); -} - -export function register(config) { - if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { - // The URL constructor is available in all browsers that support SW. - const publicUrl = new URL(process.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 = `${process.env.PUBLIC_URL}/service-worker.js`; - - if (isLocalhost) { - // This is running on localhost. Let's check if a service worker still exists or not. - checkValidServiceWorker(swUrl, config); - - // Add some additional logging to localhost, pointing developers to the - // service worker/PWA documentation. - navigator.serviceWorker.ready.then(() => { - console.log( - 'This web app is being served cache-first by a service ' + - 'worker. To learn more, visit http://bit.ly/CRA-PWA', - ); - }); - } else { - // Is not localhost. Just register service worker - registerValidSW(swUrl, config); - } + .catch(() => { + console.log('No internet connection found. App is running in offline mode.'); }); - } } -export function unregister() { +export function unregister(): void { if ('serviceWorker' in navigator) { navigator.serviceWorker.ready.then(registration => { - registration.unregister(); + return registration.unregister(); }); } }