The trimCache function in Going Offline …again

It seems that some code that I wrote in Going Offline is haunted. It’s the trimCache function.

First, there was the issue of a typo. Or maybe it’s more of a brainfart than a typo, but either way, there’s a mistake in the syntax that was published in the book.

Now it turns out that there’s also a problem with my logic.

To recap, this is a function that takes two arguments: the name of a cache, and the maximum number of items that cache should hold.

function trimCache(cacheName, maxItems) {

First, we open up the cache:

caches.open(cacheName)
.then( cache => {

Then, we get the items (keys) in that cache:

cache.keys()
.then(keys => {

Now we compare the number of items (keys.length) to the maximum number of items allowed:

if (keys.length > maxItems) {

If there are too many items, delete the first item in the cache—that should be the oldest item:

cache.delete(keys[0])

And then run the function again:

.then(
    trimCache(cacheName, maxItems)
);

A-ha! See the problem?

Neither did I.

It turns out that, even though I’m using then, the function will be invoked immediately, instead of waiting until the first item has been deleted.

Trys helped me understand what was going on by making a useful analogy. You know when you use setTimeout, you can’t put a function—complete with parentheses—as the first argument?

window.setTimeout(doSomething(someValue), 1000);

In that example, doSomething(someValue) will be invoked immediately—not after 1000 milliseconds. Instead, you need to create an anonymous function like this:

window.setTimeout( function() {
    doSomething(someValue)
}, 1000);

Well, it’s the same in my trimCache function. Instead of this:

cache.delete(keys[0])
.then(
    trimCache(cacheName, maxItems)
);

I need to do this:

cache.delete(keys[0])
.then( function() {
    trimCache(cacheName, maxItems)
});

Or, if you prefer the more modern arrow function syntax:

cache.delete(keys[0])
.then( () => {
    trimCache(cacheName, maxItems)
});

Either way, I have to wrap the recursive function call in an anonymous function.

Here’s a gist with the updated trimCache function.

What’s annoying is that this mistake wasn’t throwing an error. Instead, it was causing a performance problem. I’m using this pattern right here on my own site, and whenever my cache of pages or images gets too big, the trimCaches function would get called …and then wouldn’t stop running.

I’m very glad that—witht the help of Trys at last week’s Homebrew Website Club Brighton—I was finally able to get to the bottom of this. If you’re using the trimCache function in your service worker, please update the code accordingly.

Management regrets the error.

Have you published a response to this? :

Responses

Related posts

Expectations

Offline could be the new normal.

HTTPS + service worker + web app manifest = progressive web app

Defining the damn thing over and over again.

Service worker resources

Hyperlinks to help you get your site working offline.

Acknowledgements

Giving thanks.

Going Offline, available now!

A Book Available.

Related links

Paris Web 2019 - 10 octobre après-midi - Amphithéâtre - YouTube

Here’s the livestream of the talk I gave at Paris Web—Going Offline, complete with French live-captioning and simultaneous interpretation in .

Tagged with

A Book Apart, Front-End Next Steps

If you buy this bundle of books, you get Going Offline in some very, very good company.

Tagged with

Offline Web Experiences with Jeremy Keith « CTRL+CLICK CAST

I had a great time chatting with Lea and Emily about service workers on this episode of their podcast—they’re such great hosts!

Here’s the huffduffed audio.

Tagged with

Tagged with

devMode.fm // Going Offline: Service Workers with Jeremy Keith

I talked for an hour about service workers ‘n’ stuff

(Also available on Huffduffer.)

Tagged with

Previously on this day

7 years ago I wrote Speaking my brains in Boston

Four talks in two days.

8 years ago I wrote Patterns Day

What a day! What a lovely Patterns Day!

10 years ago I wrote Baseline

How low can you go?

11 years ago I wrote A new website for dConstruct 2014

A handsome redesign with a touch of playfulness.

14 years ago I wrote Mobilism transcription

A searchable record of the mobile browser panel.

18 years ago I wrote Pownce

Not Twitter.

20 years ago I wrote Live8blogging

I’m at the Live8 concert in Hyde Park in London. Andy had a spare ticket and kindly invited me along. I bet he’s regretting it now: he has to listen to my curmudgeony whining all day.

21 years ago I wrote Dashboard confessional

One of the most contentious of the new Tiger technologies announced at Apple’s Developer Conference is Dashboard.

23 years ago I wrote Goodbye, Arizona

My time here in the desert is coming to an end. It’s time for me to head back to beach life.