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

Related posts

Praise for Going Offline

I got yer social proof right here…

Detecting image requests in service workers

It turns out that you can’t rely on the `accept` header.

Clearleft.com is a progressive web app

The Clearleft website works offline …and about time too!

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

Defining the damn thing over and over again.

Technical balance

Why I had two technical editors working with me on Going Offline, with opposite levels of experience.

Related links

Request with Intent: Caching Strategies in the Age of PWAs – A List Apart

Aaron outlines some sensible strategies for serving up images, including using the Cache API from your service worker script.

Tagged with

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

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

10 years ago I wrote 100 words 078

Day seventy eight.

17 years ago I wrote XEN

XHTML Enemies Network

20 years ago I wrote WordPl@y

Bujumbura

20 years ago I wrote Off to @media

London seems to be geek capital this week.

21 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.

21 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".

22 years ago I wrote Keeping music live

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