2

I have this module test:

var myCache = await caches.open("test"); // create a new cache
await myCache.add(new Request("/index.html")); // page was sucessfully stored
await caches.delete("test");
myCache.match("/index.html"); // I still read from deleted cache!

After the cache was deleted, I don't see it anymore in browser inspector, but I still get the Response from the .match method. I'd expect error here, please explain this unexpected behaviour.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • You mean that you are still reading the specific item that you deleted? – DonCarleone Aug 10 '23 at 00:36
  • @DonCarleone yes (/index.html as file is not deleted, just the cache) – Jan Turoň Aug 10 '23 at 00:37
  • Are you sure that it is actually being deleted? – DonCarleone Aug 10 '23 at 00:41
  • What environment are you running this in? Is it in an aws lambda function or something like that? You might need permissions to delete stuff. Just spitballing here.. – DonCarleone Aug 10 '23 at 00:42
  • @DonCarleone yes ([caches.delete](https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage/delete) returns true). I test in browser console (Opera) – Jan Turoň Aug 10 '23 at 00:45
  • 2
    Why are you adding to `myCache` but deleting from `caches` instead of also from `myCache` ? – Mike 'Pomax' Kamermans Aug 10 '23 at 00:45
  • @Mike'Pomax'Kamermans myCache is [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) and caches is [CacheStorage](https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage) both have delete method, but with completely different meaning. – Jan Turoň Aug 10 '23 at 00:47
  • Exactly: why do you think that calling delete on the cacheStorage itself deletes anything for the _specific_ cache instance you built? Or, to rephrase and repeat the original question: why are you not using `myCache.delete`? (and don't immediately answer that: _try it_. And then if it works, hurray your problem got solved. And if not: hurray, put that in your post because that's important information) – Mike 'Pomax' Kamermans Aug 10 '23 at 00:51
  • @Mike'Pomax'Kamermans I expect the cache instance to have its resources released or at least to make them unavailable. Not sure about the reasons (guessing that the variable reference blocks deletion or maybe browser cache comes into play), I'd like to know the real cause for sure. – Jan Turoň Aug 10 '23 at 00:58
  • because you are deleting `test` but the reference framework and garbage collector are not deleting `myCache` because it is still accessible – Tachibana Shin Aug 10 '23 at 01:01
  • Try calling caches.open again, don't use same instance.. – Keith Aug 10 '23 at 01:07
  • anyway i'm not so sure about this maybe it's a bug because `caches` belongs to the `filesystem` communication area and not `js` the garbage collector shouldn't behave like that – Tachibana Shin Aug 10 '23 at 01:10
  • @TachibanaShin it is indeed so, se my answer with the relevant links and quotations confirming your words. – Jan Turoň Aug 10 '23 at 01:12

1 Answers1

1

Seems like a confusion on MDN site where they describe CacheStorage.delete as

The delete() method of the CacheStorage interface finds the Cache object matching the cacheName, and if found, deletes the Cache object and returns a Promise that resolves to true. If no Cache object is found, it resolves to false.

If fact the object is NOT deleted. The W3C specifies that the cacheJobPromise just

Remove the relevant name to cache map.

So just the key in the map is deleted, not the object itself.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169