Re: [ServiceWorker] consider exposing size for cache entries (#587)

I think the main problem with getting the size on the Request or Response is that you don't really know until the body is drained.  Fetch resolves when just the headers are available and the body may still be coming in off the network.  Unfortunately content-length is often a lie.  Either you need to read the whole stream into memory (bad for mobile) or write it to disk and check.

I think you would need to add to the cache, then delete any excess over the threshold:

```
fetch(request.clone()).then(function(response) {
  cache.put(request, response.clone()).then(function() {
    deleteExcess(cache, 1024*1024);
  });
  return response;
});

function deleteExcess(cache, maxSize) {
  cache.sizeOfAll('./avatars/', { prefixMatch: true }).then(function(size) {
    if (size < maxSize) {
      return;
    }
    cache.keys().then(function(keys) {
      cache.delete(keys[0]);
      deleteExcess(cache, maxSize);
    });
  });
}
```

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/587#issuecomment-66537638

Received on Wednesday, 10 December 2014 22:37:08 UTC