Registering service workers

In chapter two of Going Offline, I talk about registering your service worker wrapped up in some feature detection:

<script>
if (navigator.serviceWorker) {
  navigator.serviceWorker.register('/serviceworker.js');
}
</script>

But I also make reference to a declarative way of doing this that isn’t very widely supported:

<link rel="serviceworker" href="/serviceworker.js">

No need for feature detection there. Thanks to the liberal error-handling model of HTML (and CSS), browsers will just ignore what they don’t understand, which isn’t the case with JavaScript.

Alas, it looks like that nice declarative alternative isn’t going to be making its way into browsers anytime soon. It has been removed from the HTML spec. That’s a shame. I have a preference for declarative solutions where possible—they’re certainly easier to teach. But in this case, the JavaScript alternative isn’t too onerous.

So if you’re reading Going Offline, when you get to the bit about someday using the rel value, you can cast a wistful gaze into the distance, or shed a tiny tear for what might have been …and then put it out of your mind and carry on reading.

Have you published a response to this? :

Responses

Code Clarity

‘Registering service workers’ In Jeremy’s very good book on Service Workers (it’s actually about going offline, but there’s a main role for service workers as the lead actor) he shows the feature detection way in which you call a service… adactio.com/journal/13984

1 Share

# Shared by Jan Skovgaard on Friday, June 8th, 2018 at 2:39pm

1 Like

# Liked by Jan Skovgaard on Friday, June 8th, 2018 at 3:00pm

Previously on this day

6 years ago I wrote 100 words 078

Day seventy eight.

13 years ago I wrote XEN

XHTML Enemies Network

16 years ago I wrote WordPl@y

Bujumbura

16 years ago I wrote Off to @media

London seems to be geek capital this week.

17 years ago I wrote Everything in its right place

I’ve made no secret of the fact that I’m a big fan of Apple but even by my kool-aid drinking standards they’ve really outdone themselves this time.

17 years ago I wrote The move

In an article called Stress and Emotional Health over at BBC Health, there’s a list of "top stress makers". The "very high" category lists "moving house".

18 years ago I wrote Keeping music live

I’m just back from a day out in London; Hammersmith, to be precise.