From 87b5ab6bb4044b95345a97b646a6594e1050afae Mon Sep 17 00:00:00 2001 From: Valiantsin Date: Sun, 15 Dec 2019 16:50:56 +0300 Subject: [PATCH] feat: force update service worker --- apps/js-machine-admin/src/main.tsx | 10 +++++++++- apps/js-machine-admin/src/serviceWorker.ts | 16 +++++++++------- apps/js-machine-front/src/main.tsx | 10 +++++++++- apps/js-machine-front/src/serviceWorker.ts | 16 +++++++++------- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/apps/js-machine-admin/src/main.tsx b/apps/js-machine-admin/src/main.tsx index 7e1536e..55c10f8 100644 --- a/apps/js-machine-admin/src/main.tsx +++ b/apps/js-machine-admin/src/main.tsx @@ -9,4 +9,12 @@ ReactDOM.render(, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: https://bit.ly/CRA-PWA -serviceWorker.register(); +serviceWorker.register({ + onUpdate: registration => { + if (registration.waiting) { + // update service worker + // user have to reload page to use new version + registration.waiting.postMessage({ type: 'SKIP_WAITING' }); + } + }, +}); diff --git a/apps/js-machine-admin/src/serviceWorker.ts b/apps/js-machine-admin/src/serviceWorker.ts index c18aa1b..4fa1d93 100644 --- a/apps/js-machine-admin/src/serviceWorker.ts +++ b/apps/js-machine-admin/src/serviceWorker.ts @@ -18,8 +18,8 @@ const isLocalhost = Boolean( 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}$/ - ) + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/, + ), ); type Config = { @@ -50,7 +50,7 @@ export function register(config?: Config) { 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' + 'worker. To learn more, visit https://bit.ly/CRA-PWA', ); }); } else { @@ -63,7 +63,7 @@ export function register(config?: Config) { function registerValidSW(swUrl: string, config?: Config) { navigator.serviceWorker - .register(swUrl) + .register(swUrl, { updateViaCache: 'none' }) .then(registration => { registration.onupdatefound = () => { const installingWorker = registration.installing; @@ -78,7 +78,7 @@ function registerValidSW(swUrl: string, config?: 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 https://bit.ly/CRA-PWA.' + 'tabs for this page are closed. See https://bit.ly/CRA-PWA.', ); // Execute callback @@ -107,7 +107,9 @@ function registerValidSW(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. - fetch(swUrl) + fetch(swUrl, { + headers: { 'Service-Worker': 'script' }, + }) .then(response => { // Ensure service worker exists, and that we really are getting a JS file. const contentType = response.headers.get('content-type'); @@ -128,7 +130,7 @@ function checkValidServiceWorker(swUrl: string, config?: Config) { }) .catch(() => { console.log( - 'No internet connection found. App is running in offline mode.' + 'No internet connection found. App is running in offline mode.', ); }); } diff --git a/apps/js-machine-front/src/main.tsx b/apps/js-machine-front/src/main.tsx index 0b0a4b6..46196a9 100644 --- a/apps/js-machine-front/src/main.tsx +++ b/apps/js-machine-front/src/main.tsx @@ -9,4 +9,12 @@ ReactDOM.render(, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: https://bit.ly/CRA-PWA -serviceWorker.register(); +serviceWorker.register({ + onUpdate: registration => { + if (registration.waiting) { + // update service worker + // user have to reload page to use new version + registration.waiting.postMessage({ type: 'SKIP_WAITING' }); + } + }, +}); diff --git a/apps/js-machine-front/src/serviceWorker.ts b/apps/js-machine-front/src/serviceWorker.ts index c18aa1b..4fa1d93 100644 --- a/apps/js-machine-front/src/serviceWorker.ts +++ b/apps/js-machine-front/src/serviceWorker.ts @@ -18,8 +18,8 @@ const isLocalhost = Boolean( 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}$/ - ) + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/, + ), ); type Config = { @@ -50,7 +50,7 @@ export function register(config?: Config) { 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' + 'worker. To learn more, visit https://bit.ly/CRA-PWA', ); }); } else { @@ -63,7 +63,7 @@ export function register(config?: Config) { function registerValidSW(swUrl: string, config?: Config) { navigator.serviceWorker - .register(swUrl) + .register(swUrl, { updateViaCache: 'none' }) .then(registration => { registration.onupdatefound = () => { const installingWorker = registration.installing; @@ -78,7 +78,7 @@ function registerValidSW(swUrl: string, config?: 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 https://bit.ly/CRA-PWA.' + 'tabs for this page are closed. See https://bit.ly/CRA-PWA.', ); // Execute callback @@ -107,7 +107,9 @@ function registerValidSW(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. - fetch(swUrl) + fetch(swUrl, { + headers: { 'Service-Worker': 'script' }, + }) .then(response => { // Ensure service worker exists, and that we really are getting a JS file. const contentType = response.headers.get('content-type'); @@ -128,7 +130,7 @@ function checkValidServiceWorker(swUrl: string, config?: Config) { }) .catch(() => { console.log( - 'No internet connection found. App is running in offline mode.' + 'No internet connection found. App is running in offline mode.', ); }); }