Imamudin Naseem
Imamudin Naseem

Reputation: 1702

What is the lifespan of assets cached by service worker?

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?

Upvotes: 2

Views: 1875

Answers (1)

AaronHolland
AaronHolland

Reputation: 1685

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

Upvotes: 1

Related Questions