forked from acaldas/document-model-electron
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #476 from powerhouse-inc/455-force-page-refresh-wh…
…en-a-new-version-is-deployed feat: added browser refresh when a new version of connect is deployed
- Loading branch information
Showing
6 changed files
with
297 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const VERSION_CACHE = 'version-cache'; | ||
const VERSION_KEY = 'app-version'; | ||
|
||
self.addEventListener('install', event => { | ||
self.skipWaiting(); | ||
}); | ||
|
||
self.addEventListener('activate', event => { | ||
event.waitUntil(self.clients.claim()); | ||
}); | ||
|
||
|
||
self.addEventListener('message', async event => { | ||
if (event.data && event.data.type === 'SET_APP_VERSION') { | ||
const cache = await caches.open(VERSION_CACHE); | ||
await cache.put(VERSION_KEY, new Response(event.data.version)); | ||
} | ||
}); | ||
|
||
async function checkForUpdates() { | ||
try { | ||
const response = await fetch('/version.json', { cache: 'no-store' }); | ||
const newVersion = await response.json(); | ||
const cache = await caches.open(VERSION_CACHE); | ||
const cachedResponse = await cache.match(VERSION_KEY); | ||
|
||
let currentVersion = ''; | ||
|
||
if (cachedResponse) { | ||
currentVersion = await cachedResponse.text(); | ||
} | ||
|
||
if (currentVersion === '') { | ||
// Initial cache | ||
await cache.put(VERSION_KEY, new Response(newVersion.version)); | ||
} else if (currentVersion !== newVersion.version) { | ||
// New version detected | ||
console.log('Current version:', currentVersion); | ||
console.log('New version:', newVersion.version); | ||
|
||
const clients = await self.clients.matchAll(); | ||
clients.forEach(client => { | ||
client.postMessage({ type: 'NEW_VERSION_AVAILABLE', requiresHardRefresh: newVersion.requiresHardRefresh }); | ||
}); | ||
|
||
// Update the stored version | ||
await cache.put(VERSION_KEY, new Response(newVersion.version)); | ||
} | ||
} catch (error) { | ||
console.error('Error checking version:', error); | ||
} | ||
} | ||
|
||
// Check for updates every minute | ||
setInterval(checkForUpdates, 60 * 1000); // 60 seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
type SET_APP_VERSION_MESSAGE = { | ||
type: 'SET_APP_VERSION'; | ||
version: string; | ||
}; | ||
|
||
export type ServiceWorkerPostMessage = SET_APP_VERSION_MESSAGE; | ||
|
||
type NEW_VERSION_AVAILABLE_MESSAGE = { | ||
type: 'NEW_VERSION_AVAILABLE'; | ||
requiresHardRefresh: boolean; | ||
}; | ||
|
||
export type ServiceWorkerMessage = NEW_VERSION_AVAILABLE_MESSAGE; | ||
|
||
class ServiceWorkerManager { | ||
ready = false; | ||
debug = false; | ||
registration: ServiceWorkerRegistration | null = null; | ||
|
||
constructor(debug = false) { | ||
this.debug = debug; | ||
} | ||
|
||
setDebug(debug: boolean) { | ||
this.debug = debug; | ||
} | ||
|
||
registerServiceWorker(debug = false) { | ||
this.debug = debug; | ||
|
||
if ('serviceWorker' in navigator) { | ||
window.addEventListener('load', () => { | ||
navigator.serviceWorker | ||
.register('/service-worker.js') | ||
.then(registration => { | ||
// Listen for messages from the service worker | ||
if (this.debug) { | ||
console.log( | ||
'ServiceWorker registered: ', | ||
registration, | ||
); | ||
} | ||
|
||
navigator.serviceWorker.addEventListener( | ||
'message', | ||
event => { | ||
if (this.debug) { | ||
console.log( | ||
'ServiceWorker message: ', | ||
event, | ||
); | ||
} | ||
|
||
if ( | ||
event.data && | ||
event.data.type === | ||
'NEW_VERSION_AVAILABLE' && | ||
event.data.requiresHardRefresh === true | ||
) { | ||
if (this.debug) { | ||
console.log('New version available'); | ||
} | ||
window.location.reload(); // Reload the page to load the new version | ||
} | ||
}, | ||
); | ||
|
||
if (navigator.serviceWorker.controller) { | ||
navigator.serviceWorker.controller.postMessage({ | ||
type: 'SET_APP_VERSION', | ||
version: process.env.APP_VERSION, | ||
}); | ||
} | ||
|
||
this.ready = true; | ||
this.registration = registration; | ||
}) | ||
.catch(error => { | ||
console.error( | ||
'ServiceWorker registration failed: ', | ||
error, | ||
); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
sendMessage(message: ServiceWorkerPostMessage) { | ||
if (this.ready && this.registration) { | ||
const serviceWorker = | ||
this.registration.active || | ||
this.registration.waiting || | ||
this.registration.installing; | ||
if (serviceWorker) { | ||
if (this.debug) { | ||
console.log('Sending message to service worker: ', message); | ||
} | ||
serviceWorker.postMessage(message); | ||
} else { | ||
console.error('No active service worker found.'); | ||
} | ||
} else { | ||
console.error('Service Worker is not ready yet.'); | ||
} | ||
} | ||
} | ||
|
||
const serviceWorkerManager = new ServiceWorkerManager(); | ||
export default serviceWorkerManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters