ibrohm's picture
Initial deploy via assistant API
eb4179c verified
const CACHE_NAME = 'mtextile-v3';
const ASSETS = [
'/',
'/index.html',
'/catalog.html',
'/product.html',
'/cart.html',
'/checkout.html',
'/wishlist.html',
'/profile.html',
'/css/global.css',
'/css/components.css',
'/css/pages.css',
'/js/store.js',
'/js/products.js',
'/js/app.js'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
// Network First Strategy
event.respondWith(
fetch(event.request)
.then((response) => {
const clone = response.clone();
// Cache the fresh response
caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
return response;
})
.catch(() => {
// Return fallback from cache if offline
return caches.match(event.request);
})
);
});