Add deleteCacheAndMetadata() recipe to docs
See original GitHub issueLibrary Affected: workbox.expiration.Plugin / workbox-runtime-caching
Browser & Platform: all browsers.
Feature Request Description:
Currently, we don’t have a method which can delete all the entries and metadata from IDB and cacheAPI for a given cacheName
. A common use case with workbox runtime caching deployments at scale is to delete cache entries namespaced by a cacheName
+ version
. This requirement stems from basic versioning of caches, passed to runtime strategies, and when a cache version is bumped we want to clean up previous version’s cache & IDB entries, usually in the activate
event of the SW.
This is easily possible to do for CacheAPI entries but hard to work with IDB where the implementation is tightly coupled to Workbox. I’d love to hear thoughts on deleteCacheAndMetadata
API to accept an optional cacheName
parameter that would purge only the CacheAPI and IDB entries for that specific cache name.
Proposed API deleteCacheAndMetadata(cacheName)
Implementation: This should be fairly straightforward to implement for CacheAPI entries. For IDB we are currently using an IDBKeyRange such as IDBKeyRange.bound(cacheName, `${cacheName}\uffff`);
to delete IDB entries manually outside of workbox.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:15 (10 by maintainers)
Top GitHub Comments
@jeffposnick Yeah that sounds good to me! Thanks.
Though perhaps not the most user-friendly API, you can do what you want today by creating a new
CacheExpiration
instance withmaxEntries
set to 0.Something like this should work:
We could potentially create a convenience method that does something like this; however, after thinking about it a bit, requiring that the developer know the cache name may not be the best API.
Rather than asking them to remove metadata for a particular cache name, I think it might make more sense to have a method that clears all metadata for entries not in a specific whitelist of caches. We could even default to removing everything not in the list of currently used caches (e.g.
await caches.keys()
)That would allow you to easily clear unused entries for a user who may be upgrading from a really old version of the service worker.
What do you think?