【发布时间】:2016-12-22 13:55:40
【问题描述】:
我正在使用服务工作者的缓存工具,但是,在更新站点后,当我刷新页面时,缓存仍然提供旧数据。
所以根据this post 中的答案,我实施了stale-while-revalidate:
self.addEventListener('fetch', function(event) {
event.respondWith(caches.open(CACHE_NAME).then(function(cache) {
return cache.match(event.request).then(function(response) {
var fetchPromise = fetch(event.request).then(function(networkResponse) {
// if we got a response from the cache, update the cache
if (response) {
console.log("cached page: " + event.request.url);
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
});
// respond from the cache, or the network
return response || fetchPromise;
});
}));
});
连接后,一切正常,我可以看到控制台日志消息。
当我停止服务器并刷新页面时,会出现大量异常。
Uncaught (in promise) TypeError: Failed to fetch
Failed to load resource: net::ERR_CONNECTION_REFUSED
我在 fetch() 上添加了一个 catch 来尝试处理异常,但它仍然失败(并且未调用 catch)。我在 caches.open() 和 respondWith() 上添加了一个捕获但同样的东西。
我知道我可以忽略这些错误,但我宁愿处理它们而不做任何事情(包括不将它们输出到控制台),这样我就可以在我输出的控制台中看到有意义的东西.
如何停止错误消息?
服务安装时的错误不是问题,但也可以很好地捕捉和忽略。
【问题讨论】: