Skip to content

Commit

Permalink
feat: avoid refreshing if already on latest app version
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Sep 19, 2024
1 parent 171c2ec commit e4c753d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ _self.addEventListener('activate', (event: ExtendableEvent) => {

export type NEW_VERSION_AVAILABLE_MESSAGE = {
type: 'NEW_VERSION_AVAILABLE';
version: string;
requiresHardRefresh: boolean;
};

Expand Down Expand Up @@ -56,19 +57,20 @@ async function checkAppVersion(version: string, requiresHardRefresh: boolean) {
await cache.put(VERSION_KEY, new Response(version));

// Update clients
await updateClients(requiresHardRefresh);
await updateClients(version, requiresHardRefresh);
}
} catch (error) {
console.error('Error checking version:', error);
}
}

async function updateClients(requiresHardRefresh: boolean) {
async function updateClients(version: string, requiresHardRefresh: boolean) {
await _self.clients.claim();
const clients = await _self.clients.matchAll();
clients.forEach(client => {
postMessage(client, {
type: 'NEW_VERSION_AVAILABLE',
version,
requiresHardRefresh,
});
});
Expand Down
4 changes: 4 additions & 0 deletions src/utils/registerServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ class ServiceWorkerManager {
'type' in event.data ? (event as ServiceWorkerMessage) : null;
switch (message?.data.type) {
case 'NEW_VERSION_AVAILABLE': {
if (message.data.version === connectConfig.appVersion) {
return;
}
if (message.data.requiresHardRefresh) {
if (this.debug) {
console.log('New version available');
}

window.location.reload(); // Reload the page to load the new version
}
break;
Expand Down

0 comments on commit e4c753d

Please sign in to comment.