40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
const CACHE_NAME = 'soms-v1-static';
|
|
const API_CACHE_NAME = 'soms-v1-api';
|
|
|
|
const STATIC_ASSETS = [
|
|
'./',
|
|
'./index.html',
|
|
'./app.js',
|
|
'./assets/css/custom.css',
|
|
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
const url = new URL(event.request.url);
|
|
|
|
// API requests strategy: Network first, then cache
|
|
if (url.searchParams.has('request')) {
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then((response) => {
|
|
const clonedResponse = response.clone();
|
|
caches.open(API_CACHE_NAME).then((cache) => cache.put(event.request, clonedResponse));
|
|
return response;
|
|
})
|
|
.catch(() => caches.match(event.request))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Static assets strategy: Cache first, then network
|
|
event.respondWith(
|
|
caches.match(event.request).then((response) => response || fetch(event.request))
|
|
);
|
|
});
|