Service worker 如何防止 'fetch' 消耗过多磁盘 space?

How Service worker prevent 'fetch' from consuming too much disk space?

请问有什么策略可以防止addEventListener('fetch' && cache.put(占满用户的磁盘space?无论如何要为缓存中的每个请求设置过期时间?

According to the service worker spec:

The Cache objects are exactly what authors have to manage themselves. The Cache objects do not get updated unless authors explicitly request them to be. The Cache objects do not expire unless authors delete the entries.

请注意 the browser can throw your cache away 如果它想要(并且您应该假设有时它会这样做),特别是如果它决定您存储太多 - 它不会让您填满用户的整个驱动器.

似乎也没有任何检查缓存大小的方法。但是请注意,作品中有相关规范,例如 Quota Management API。上面链接的 Jake Archibald 的文章给出了以下使用此 API 的示例(这将为您提供 all 站点存储的结果,而不仅仅是您的缓存):

navigator.storageQuota.queryInfo("temporary").then(function(info) {
  console.log(info.quota);
  // Result: <quota in bytes>
  console.log(info.usage);
  // Result: <used data in bytes>
});

不过我只是在 Chrome Canary 中尝试过这个,storageQuota 是未定义的——我认为这还没有在任何地方实现。如果我错了,希望有人能纠正我。