sw-toolbox icon indicating copy to clipboard operation
sw-toolbox copied to clipboard

Explore utilities for making it easier to clean up caches

Open addyosmani opened this issue 9 years ago • 1 comments

From #130

Maybe we could come up with some sort of optional helper, though, that makes tidying up custom caches easier. Maybe something like expireCaches(prefix, suffix) that will, during activate, delete any cache that starts with prefix but does not end with suffix. This should help with versioned caches where the prefix will contain the scope and the suffix will be the current version. So if you have a cache called scope + '-images-' + version, then when you upgrade from version 4 to version 5 the version 4 cache will be deleted for you.

addyosmani avatar Aug 31 '16 16:08 addyosmani

Maybe something like this:

function deleteCache(pattern) {
    return cache.keys().then(function (keys) {
        return Promise.all(
            keys
            .filter(function(key) {
                return pattern.test(key)
            })
            .map(function(key){
                return cache.delete(key)
            })
        )
    })
}

Note this code is untested, basically it expects a RegEx, all caches that matches that RegEx will be deleted.

I'm not entirely sure if this works because this is the first time I work with ServiceWorkers and I haven't tested this code yet. I based this on this code just changed the startWith for a RegEx for better control.

pablomayobre avatar Sep 26 '16 05:09 pablomayobre