Some checks failed
Deploy to GitHub Pages / build-and-deploy (push) Has been cancelled
57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
// Service Worker for Geek Calculator - enables offline functionality
|
|
|
|
const CACHE_NAME = 'geek-calculator-v1';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/index.html',
|
|
'/styles.css',
|
|
'/app.js',
|
|
'/calculator.js',
|
|
'/rpn-calculator.js',
|
|
'/ui.js',
|
|
'/state.js',
|
|
'/utils.js',
|
|
'/manifest.webmanifest'
|
|
];
|
|
|
|
// Install event - cache resources
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Opened cache');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event - serve from cache or network
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
// Return cached version if available, otherwise fetch from network
|
|
if (response) {
|
|
return response;
|
|
}
|
|
return fetch(event.request);
|
|
}
|
|
)
|
|
);
|
|
});
|
|
|
|
// Activate event - clean up old caches
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(cacheNames => {
|
|
return Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if (cacheName !== CACHE_NAME) {
|
|
console.log('Deleting old cache:', cacheName);
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
}); |