|
@@ -25,47 +25,10 @@ type Config = {
|
|
onUpdate?: (registration: ServiceWorkerRegistration) => void;
|
|
onUpdate?: (registration: ServiceWorkerRegistration) => void;
|
|
};
|
|
};
|
|
|
|
|
|
-export function register(config?: 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 https://bit.ly/CRA-PWA'
|
|
|
|
- );
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- // Is not localhost. Just register service worker
|
|
|
|
- registerValidSW(swUrl, config);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
function registerValidSW(swUrl: string, config?: Config) {
|
|
function registerValidSW(swUrl: string, config?: Config) {
|
|
navigator.serviceWorker
|
|
navigator.serviceWorker
|
|
.register(swUrl)
|
|
.register(swUrl)
|
|
- .then(registration => {
|
|
|
|
|
|
+ .then((registration) => {
|
|
registration.onupdatefound = () => {
|
|
registration.onupdatefound = () => {
|
|
const installingWorker = registration.installing;
|
|
const installingWorker = registration.installing;
|
|
if (installingWorker == null) {
|
|
if (installingWorker == null) {
|
|
@@ -101,7 +64,7 @@ function registerValidSW(swUrl: string, config?: Config) {
|
|
};
|
|
};
|
|
};
|
|
};
|
|
})
|
|
})
|
|
- .catch(error => {
|
|
|
|
|
|
+ .catch((error) => {
|
|
console.error('Error during service worker registration:', error);
|
|
console.error('Error during service worker registration:', error);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -109,9 +72,9 @@ function registerValidSW(swUrl: string, config?: Config) {
|
|
function checkValidServiceWorker(swUrl: string, config?: Config) {
|
|
function checkValidServiceWorker(swUrl: string, config?: Config) {
|
|
// Check if the service worker can be found. If it can't reload the page.
|
|
// Check if the service worker can be found. If it can't reload the page.
|
|
fetch(swUrl, {
|
|
fetch(swUrl, {
|
|
- headers: { 'Service-Worker': 'script' }
|
|
|
|
|
|
+ headers: { 'Service-Worker': 'script' },
|
|
})
|
|
})
|
|
- .then(response => {
|
|
|
|
|
|
+ .then((response) => {
|
|
// Ensure service worker exists, and that we really are getting a JS file.
|
|
// Ensure service worker exists, and that we really are getting a JS file.
|
|
const contentType = response.headers.get('content-type');
|
|
const contentType = response.headers.get('content-type');
|
|
if (
|
|
if (
|
|
@@ -119,7 +82,7 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
|
|
(contentType != null && contentType.indexOf('javascript') === -1)
|
|
(contentType != null && contentType.indexOf('javascript') === -1)
|
|
) {
|
|
) {
|
|
// No service worker found. Probably a different app. Reload the page.
|
|
// No service worker found. Probably a different app. Reload the page.
|
|
- navigator.serviceWorker.ready.then(registration => {
|
|
|
|
|
|
+ navigator.serviceWorker.ready.then((registration) => {
|
|
registration.unregister().then(() => {
|
|
registration.unregister().then(() => {
|
|
window.location.reload();
|
|
window.location.reload();
|
|
});
|
|
});
|
|
@@ -136,9 +99,44 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+export function register(config?: Config) {
|
|
|
|
+ if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
|
|
|
+ const url = process.env.PUBLIC_URL || 'localhost';
|
|
|
|
+ // The URL constructor is available in all browsers that support SW.
|
|
|
|
+ const publicUrl = new URL(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 https://bit.ly/CRA-PWA'
|
|
|
|
+ );
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ // Is not localhost. Just register service worker
|
|
|
|
+ registerValidSW(swUrl, config);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
export function unregister() {
|
|
export function unregister() {
|
|
if ('serviceWorker' in navigator) {
|
|
if ('serviceWorker' in navigator) {
|
|
- navigator.serviceWorker.ready.then(registration => {
|
|
|
|
|
|
+ navigator.serviceWorker.ready.then((registration) => {
|
|
registration.unregister();
|
|
registration.unregister();
|
|
});
|
|
});
|
|
}
|
|
}
|