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.
Update: Brandon has been tackling this problem and it looks like he’s found the solution: use the page load
event to fire a postMessage
payload to the active Service Worker:
window.addEventListener('load', function() {
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({'command': 'trimCaches'});
}
});
Then inside the Service Worker, I can listen for that message
event and run my cache-trimming function:
self.addEventListener('message', function(event) {
if (event.data.command == 'trimCaches') {
trimCache(pagesCacheName, 35);
trimCache(imagesCacheName, 20);
}
});
So what happens is you visit a page, and the caching happens as usual. But then, once the page and all its assets are loaded, a message is fired off and the caches get trimmed.
I’ve updated my Service Worker and it looks like it’s working a treat.
# Liked by Gunnar Bittersmann on Sunday, November 29th, 2015 at 11:02pm