45 lines
914 B
JavaScript
45 lines
914 B
JavaScript
|
|
const CACHE_NAME = 'my-cache-v1';
|
|
|
|
// List of files to be cached
|
|
const urlsToCache = [
|
|
'/',
|
|
'./index.jsx',
|
|
];
|
|
|
|
// Install the service worker
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
// Activate the service worker
|
|
self.addEventListener('activate', (event) => {
|
|
const cacheWhitelist = [CACHE_NAME];
|
|
|
|
event.waitUntil(
|
|
caches.keys().then((cacheNames) => {
|
|
return Promise.all(
|
|
cacheNames.map((name) => {
|
|
if (cacheWhitelist.indexOf(name) === -1) {
|
|
return caches.delete(name);
|
|
}
|
|
return null;
|
|
})
|
|
);
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event
|
|
self.addEventListener('fetch', (event) => {
|
|
event.respondWith(
|
|
caches.match(event.request).then((response) => {
|
|
return response || fetch(event.request);
|
|
})
|
|
);
|
|
});
|