fullstack.web/www/swa/keycodes/service-worker.js
2022-12-22 14:57:51 +08:00

45 lines
821 B
JavaScript

/**
* 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
});
})
);
};