-
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.
- Loading branch information
1 parent
73ec71a
commit 2f75523
Showing
2 changed files
with
82 additions
and
39 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 |
---|---|---|
@@ -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); | ||
}) | ||
); | ||
}); | ||
}); |