Skip to content

Commit

Permalink
fix previous version cache
Browse files Browse the repository at this point in the history
  • Loading branch information
SalatielSauer committed Dec 23, 2024
1 parent 73ec71a commit 2f75523
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 39 deletions.
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ <h2 id="filesystem-status"></h2>
newWorker.addEventListener("statechange", () => {
if (newWorker.state === "installed" && navigator.serviceWorker.controller) {
console.log("New OGZ-Editor version is available; please refresh.");
FS_fileStatus.update(1, `Checking new version...`, "fas fa-spinner fa-spin");
FS_fileStatus.update(1, `Checking new version...`, '⚙️');
}
});
});
});
navigator.serviceWorker.addEventListener("message", (event) => {
if (event.data.type === 1) {
FS_fileStatus.update(1, `Updating OGZ Editor to ${event.data.body}...`, "fas fa-spinner fa-spin");
if (event.data.type === "CACHE_UPDATED") {
FS_fileStatus.update(1, `Updating OGZ Editor to ${event.data.body}...`, '⚙️');
window.location.reload();
}
});
Expand Down
115 changes: 79 additions & 36 deletions pwapp.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,93 @@
const version = '0.2';
const version = '0.21';
const cacheName = `ogzeditor-${version}`;
console.log('running', cacheName);
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(cacheName).then((cache) => {
return cache.addAll([
'/OGZ-Editor/index.html',
'/OGZ-Editor/styles/styles.css',
'/OGZ-Editor/styles/texteditor.css',
'/OGZ-Editor/styles/sauerfont.ttf',
'/OGZ-Editor/scripts/main.js',
'/OGZ-Editor/scripts/mini-jsocta.js',
'/OGZ-Editor/scripts/jsocta_helpers.js',
'/OGZ-Editor/scripts/texteditor.js',
'/OGZ-Editor/scripts/worker.js',
'/OGZ-Editor/scripts/external/gifuct.js'
]).then(() => self.skipWaiting());
})
const assetsToCache = [
'/OGZ-Editor/index.html',
'/OGZ-Editor/styles/styles.css',
'/OGZ-Editor/styles/texteditor.css',
'/OGZ-Editor/styles/sauerfont.ttf',
'/OGZ-Editor/scripts/main.js',
'/OGZ-Editor/scripts/mini-jsocta.js',
'/OGZ-Editor/scripts/jsocta_helpers.js',
'/OGZ-Editor/scripts/texteditor.js',
'/OGZ-Editor/scripts/worker.js',
'/OGZ-Editor/scripts/external/gifuct.js'
];

console.log('Service Worker initializing with cache:', cacheName);

// install event
self.addEventListener('install', (event) => {
console.log('[Service Worker] Install Event');
event.waitUntil(
caches.open(cacheName)
.then((cache) => {
console.log('[Service Worker] Caching all assets');
return cache.addAll(assetsToCache);
})
.then(() => {
console.log('[Service Worker] Skip waiting on install');
return self.skipWaiting();
})
.catch((error) => {
console.error('[Service Worker] Failed to cache assets during install:', error);
})
);
});

// activate event
self.addEventListener('activate', (event) => {
console.log('[Service Worker] Activate Event');
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== cacheName) {
return caches.delete(key); // remove old caches
}
}));
}).then(() => {
self.clients.matchAll().then(clients => {
clients.forEach(client => {
client.postMessage({type: 1, body: version});
caches.keys()
.then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key.startsWith('ogzeditor-') && key !== cacheName) {
console.log('[Service Worker] Deleting old cache:', key);
return caches.delete(key);
}
return Promise.resolve();
})
);
})
.then(() => {
console.log('[Service Worker] Claiming clients for current page');
return self.clients.claim();
})
.then(() => {
return self.clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage({ type: 'CACHE_UPDATED', version });
});
});
});
return self.clients.claim();
})
})
.catch((error) => {
console.error('[Service Worker] Failed to activate:', error);
})
);
});

// fetch event
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.open(cacheName)
.then(cache => cache.match(event.request, {ignoreSearch: true}))
.then(response => {
return response || fetch(event.request);
caches.match(event.request, { ignoreSearch: true })
.then((response) => {
if (response) {
return response;
}
// clone the request as fetch consumes the request
return fetch(event.request).then((fetchResponse) => {
if (fetchResponse && fetchResponse.status === 200 && fetchResponse.type === 'basic') {
const responseClone = fetchResponse.clone();
caches.open(cacheName).then((cache) => {
cache.put(event.request, responseClone);
});
}
return fetchResponse;
});
})
.catch((error) => {
console.error('[Service Worker] Fetch failed:', error);
})
);
});
});

0 comments on commit 2f75523

Please sign in to comment.