fullstack.web/swa/keycodes/service-worker.js

45 lines
821 B
JavaScript
Raw Normal View History

2022-12-22 06:57:51 +00:00
/**
* The name of the current cache
* @type {String}
*/
const CACHE_NAME = 'v6';
/**
* Files to cache
* @type {Array}
*/
const fileCache = [
'/',
'/index.html',
'/scripts.js',
'/style.css'
];
this.oninstall = event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(fileCache))
);
};
this.onfetch = event => {
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
return response;
}
return fetch(event.request)
.then(res => {
const r = res.clone();
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, r);
});
return res; // Don't wait for the request to cache
});
})
);
};