more readability: use fetch and cache
This commit is contained in:
parent
c208c7415b
commit
62d58f9059
16
cachersw.js
16
cachersw.js
@ -52,9 +52,8 @@ self.addEventListener('install', (event) => {
|
||||
*
|
||||
*
|
||||
*/
|
||||
/*
|
||||
self.fetch_and_cache = function (evreq) {
|
||||
fetch(evreq).then((response) => {
|
||||
return fetch(evreq).then((response) => {
|
||||
return caches.open('v1').then((cache) => {
|
||||
console.log('fetch and cache ' + evreq);
|
||||
cache.put(evreq, response.clone());
|
||||
@ -64,7 +63,6 @@ self.fetch_and_cache = function (evreq) {
|
||||
});
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
@ -94,17 +92,7 @@ self.addEventListener('fetch', (event) => {
|
||||
// but in case of success response will have value
|
||||
if (cachedResponse !== undefined)
|
||||
return cachedResponse;
|
||||
return fetch(event.request).then(function (response) {
|
||||
// response may be used only once
|
||||
// we need to save clone to put one copy in cache
|
||||
// and serve second one
|
||||
let responseClone = response.clone();
|
||||
|
||||
caches.open('v1').then(function (cache) {
|
||||
cache.put(event.request, responseClone);
|
||||
});
|
||||
return response;
|
||||
});
|
||||
return self.fetch_and_cache(event.request)
|
||||
}));
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user