With service workers comes great flexibility and power, but also much responsibility... in terms of keeping the cache under control and not allowing it to grow unnecessarily.
Is there a way to determine the age of a cached item, i.e. the time it has been sitting in the cache, and purge cached items periodically based on age?
I'm thinking here about something along the following lines:
const RUNTIME = 'runtime-cache';
var getAgeOf = function (request) {
return (*time since request was cached*); // <-- but HOW?
};
var purgeRuntimeCache = function (threshold) {
caches.open(RUNTIME).then(function (cache) {
cache.keys().then(function (keys) {
keys.forEach(function (request, index, array) {
cache.match(request).then(function (response) {
if (getAgeOf(request) > threshold) {
console.log('delete old resource from runtime cache: ' + request.url);
cache.delete(request);
}
});
});
});
});
};