Skip to content

Commit

Permalink
Register service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Nov 2, 2023
1 parent e1bcf9c commit e134857
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import App from './App';
import {ModalContextProvider} from './context/ModalContextProvider';
import {SettingsProvider} from './context/SettingsProvider';
// import reportWebVitals from './reportWebVitals';
// import * as serviceWorkerRegistration from './serviceWorkerRegistration';
import * as serviceWorkerRegistration from './serviceWorkerRegistration';

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
Expand All @@ -21,7 +21,7 @@ root.render(
// 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://cra.link/PWA
// serviceWorkerRegistration.register();
serviceWorkerRegistration.register();

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
Expand Down
25 changes: 21 additions & 4 deletions src/serviceWorkerRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ function registerValidSW(swUrl: string, config?: Config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
if (registration.waiting) {
registration.waiting.postMessage('SKIP_WAITING');
}

registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
Expand All @@ -72,10 +76,12 @@ function registerValidSW(swUrl: string, config?: Config) {
// 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.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://cra.link/PWA.'
);
// To avoid having outdated clients, we use skipWaiting() to
// activate the new service worker immediately.
// This is followed by a page refresh to fetch new assets.
if (registration.waiting) {
registration.waiting.postMessage('SKIP_WAITING');
}

// Execute callback
if (config && config.onUpdate) {
Expand Down Expand Up @@ -140,3 +146,14 @@ export function unregister() {
});
}
}

let refreshing = false;
// When a new service worker is installed we forcefully reload the page
// to ensure that all assets are loaded with the new worker.
// https://whatwebcando.today/articles/handling-service-worker-updates/
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (!refreshing) {
window.location.reload();
refreshing = true;
}
});

0 comments on commit e134857

Please sign in to comment.