45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
const CACHE_NAME = 'jyotish-shell-v1';
|
|
const APP_SHELL = [
|
|
'/',
|
|
'/index.html',
|
|
'/style.css',
|
|
'/main.js',
|
|
'/api-bridge.js',
|
|
'/manifest.webmanifest',
|
|
'/pwa-icon.svg'
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => cache.addAll(APP_SHELL))
|
|
.then(() => self.skipWaiting())
|
|
.catch(() => undefined)
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys()
|
|
.then(keys => Promise.all(keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key))))
|
|
.then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
const request = event.request;
|
|
if (request.method !== 'GET') return;
|
|
const url = new URL(request.url);
|
|
if (url.pathname.startsWith('/api/')) return;
|
|
|
|
event.respondWith(
|
|
fetch(request)
|
|
.then(response => {
|
|
const copy = response.clone();
|
|
caches.open(CACHE_NAME).then(cache => cache.put(request, copy));
|
|
return response;
|
|
})
|
|
.catch(() => caches.match(request).then(cached => cached || (request.mode === 'navigate' ? caches.match('/index.html') : undefined)))
|
|
);
|
|
});
|