question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add deleteCacheAndMetadata() recipe to docs

See original GitHub issue

Library 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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
adityapunjanicommented, Sep 25, 2019

@jeffposnick Yeah that sounds good to me! Thanks.

1reaction
philipwaltoncommented, Sep 25, 2019

Though perhaps not the most user-friendly API, you can do what you want today by creating a new CacheExpiration instance with maxEntries set to 0.

Something like this should work:

import {CacheExpiration} from 'workbox-expiration/CacheExpiration';

addEventListener('activate', (event) => {
  event.waitUntil(
    new CacheExpiration('my-cache:v1', {maxEntries: 0}).expireEntries(),
  )
});

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manually execute workbox.expiration.Plugin ... - Stack Overflow
Plugin.deleteCacheAndMetadata() has recently been added to workbox. How can I trigger this for a particular route I have setup to cache? The use ......
Read more >
Configuring the Apollo Client cache - Apollo GraphQL Docs
This article describes cache setup and configuration. To learn how to interact with cached data, see Reading and writing data to the cache....
Read more >
Create a service worker with Workbox, Webpack and TypeScript
JavaScript Libraries for adding offline support to web apps…Welcome, Workbox is a set of libraries and Node modules that make it easy to...
Read more >
workbox-expiration - Chrome Developers
Note: This method will not remove the cached entry, call expireEntries() to remove indexedDB ... The deleteCacheAndMetadata function looks like: () => {...}....
Read more >
Article: Document Cache Best Practices and Common Scenarios
This assumes you have read How the Document Cache Shape Works and have ... Adding the cached data to the source profile vs....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found