2

Some of the articles I have read suggest that items cached by service worker (web Cache API) is stored in system forever. I have come across a scenario when some of the cached resources are evicted automatically for users who revisit my website after a long time(~ > 2 months) I know for a fact that assets cached via HTTP caching are removed by browser after certain time. Does same apply for service worker too?

If that is the case, then how does browser decide what asset it has to remove and is there a way I can tell browser that if it is removing something from cache, then remove everything that are cached with same cache name?

Imamudin Naseem
  • 1,604
  • 18
  • 21

1 Answers1

1

It seems it lasts forever, until it doesn't :) (ie. storage space is low) https://developers.google.com/web/ilt/pwa/caching-files-with-service-worker

You are responsible for implementing how your script (service worker) handles updates to the cache. All updates to items in the cache must be explicitly requested; items will not expire and must be deleted. However, if the amount of cached data exceeds the browser's storage limit, the browser will begin evicting all data associated with an origin, one origin at a time, until the storage amount goes under the limit again. See Browser storage limits and eviction criteria for more information.

If their storage is running low then it may be evicted: (See Storage Limits) https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Browser_storage_limits_and_eviction_criteria

AaronHolland
  • 1,595
  • 1
  • 16
  • 32
  • Thanks @Aaron. Talking about your point - "However, if the amount of cached data exceeds the browser's storage limit, the browser will begin evicting all data associated with an origin". Do you mean whenever browser starts evicting data, it will remove all the data associated with a origin at a time. There is not partial eviction, right? – Imamudin Naseem May 03 '18 at 05:35
  • It would appear that way. It makes sense I guess, because how would a script react to partial data? Anything could happen – AaronHolland May 03 '18 at 05:36
  • In my case, partial eviction is happening which is causing issues. What I mean is let's say my domain xyx.com caches a.js and b.js. Browser is removing only a.js in some scenarios – Imamudin Naseem May 03 '18 at 05:38
  • Hmm, I don't know why that is then. It isn't supposed to work that way. – AaronHolland May 03 '18 at 05:40
  • Cool. Thanks @Aaron – Imamudin Naseem May 03 '18 at 05:41