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.
This was originally posted on my own site.