Cache-limiting in Service Workers ���again

Okay, so remember when I was talking about cache-limiting in Service Workers?



It wasn’t quite working:




The cache-limited seems to be working for pages. But for some reason the images cache has blown past its allotted maximum of 20 (you can see the items in the caches under the ���Resources��� tab in Chrome under ���Cache Storage���).



This is almost certainly because I���m doing something wrong or have completely misunderstood how the caching works.




Sure enough, I was doing something wrong. Thanks to Brandon Rozek and Jonathon Lopes for talking me through the problem.



In a nutshell, I’m mixing up synchronous instructions (like “delete the first item from a cache”) with asynchronous events (pretty much anything to do with fetching and caching with Service Workers).



Instead of trying to clean up a cache at the same time as I’m adding a new item to it, it’s better for me to have clean-up function to run at a different time. So I’ve written that function:



var trimCache = function (cacheName, maxItems) {
caches.open(cacheName)
.then(function (cache) {
cache.keys()
.then(function (keys) {
if (keys.length > maxItems) {
cache.delete(keys[0])
.then(trimCache(cacheName, maxItems));
}
});
});
};


But now the question is ���when should I run this function? What’s a good event to trigger a clean-up? I don’t think the activate event is going to work. I probably want something like background sync but I don’t think that’s quite ready for primetime yet.



In the meantime, if you can think of a good way of doing a periodic clean-up like this, please let me know.



Anyone? Anyone? Bueller?



In other Service Worker news, I’ve added a basic Service Worker to The Session. It caches static caches���CSS and JavaScript���and keeps another cache of site section index pages topped up. If the network connection drops (or the server goes down), there’s an offline page that gives a few basic options. Nothing too advanced, but better than nothing.

 •  0 comments  •  flag
Share on Twitter
Published on November 29, 2015 12:36
No comments have been added yet.


Jeremy Keith's Blog

Jeremy Keith
Jeremy Keith isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Jeremy Keith's blog with rss.