Skip to content

Commit

Permalink
refactor message listener
Browse files Browse the repository at this point in the history
  • Loading branch information
secondl1ght committed Jun 1, 2023
1 parent 8faa162 commit f693c5c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ self.addEventListener('activate', (event) => {
event.waitUntil(deleteOldCaches());
});

self.addEventListener('message', async (event) => {
if (event.data === 'CACHE_ASSETS') {
self.addEventListener('message', (event) => {
async function cacheAssets() {
// Create a new cache and add all files to it
const cache = await caches.open(CACHE);
const cached = await cache.match('/cached.txt');

if (cached) return;

event.waitUntil(await cache.addAll(ASSETS));
await cache.addAll(ASSETS);
}

if (event.data === 'CACHE_ASSETS') {
event.waitUntil(cacheAssets());
}
});

Expand Down

0 comments on commit f693c5c

Please sign in to comment.