【发布时间】:2019-09-11 22:33:35
【问题描述】:
我正在尝试缓存特定的 url,每个 url 都有 md5 哈希,如果 url 用新的 md5 更新,我想删除当前的缓存并添加新的缓存。 缓存网址:http://www.mysite.lo/cards/index.php?md5=f51c2ef7795480ef2e0b1bd24c9e07
function shouldFetch(event) {
if ( event.request.url.indexOf( '/cards/') == -1 ) {
return false;
}
return true;
}
self.addEventListener('fetch', function(event) {
if (shouldFetch(event)) {
event.respondWith(
caches.match(event.request).then(function(response) {
if (response !== undefined) {
return response;
} else {
return fetch(event.request).then(function (response) {
let responseClone = response.clone();
caches.open('v1').then(function (cache) {
cache.put(event.request, responseClone);
});
return response;
}).catch(function (err) {
return caches.match(event.request);
});
}
})
);
}
});
我知道我们可以使用 caches.delete() 等等,但我只想在 md5 从新请求更新时调用它。 谢谢
【问题讨论】: