Skip to content

Commit

Permalink
Create sw.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloodinaz authored Jun 15, 2024
1 parent 7d9e6a8 commit 3dfae3e
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const CACHE_NAME = 'cache-v2';
const urlsToCache = [
'/index.html',
'/robots.txt',
'/manifest.json',
'/privacy.html',
'/termofuse.html',
'/index.js',
'/icons/icon-72x72.png',
'/icons/icon-96x96.png',
'/icons/icon-128x128.png',
'/icons/icon-144x144.png',
'/icons/icon-152x152.png',
'/icons/icon-192x192.png',
'/icons/icon-384x384.png',
'/icons/icon-512x512.png',
];

self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(urlsToCache))
.then(() => console.log('Cache successfully initialized'))
.catch(error => console.log('Cache initialization failed:', error))
);
});

self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
return response;
}
return fetch(event.request);
})
.catch(error => console.log('Fetch error:', error))
);
});

self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});

0 comments on commit 3dfae3e

Please sign in to comment.