CodeCoupler Application Basics Service Worker Loader π
What is this?
Registering a service worker is easy: just use navigator.serviceWorker.register(serviceWorkerUrl).
But here's where things get tricky.
Was the service worker loaded right? Are there new versions of the service worker? How do I update the service worker?
A service worker keeps running even if it's just using one tab. Also, you can't update a service worker at any time. It's important to keep an eye on the status of the new service worker.
Is it loading right now? Is it installing right now? Is it already activated? Is it waiting to be activated?
You can solve all of these problems quickly and easily using the ServiceWrokerLoader class.
Setup
Install Bundle:
Define Aliases, Externals and Assets:
Basic Usage
To load a service worker from the location /sw.js:
To load a service worker from specific location:
Handling Registration, Error and Updates via promises:
Handling Registration, Error and Updates via constructor arguments:
Handle Service Worker Registration
Registration errors won't lead to any exceptions. You'll have to handle the case yourself.
You can define callbacks during the initialization process. These will be called after the shared worker registration is successful or after it fails.
Or use a promise result after initialization to get informed about the registration process. If the registration was successful, the promise will be resolved. If the registration fails, the promise will be rejected:
The success callback and the success promise receives the instance as first argument. Via the
instance you have access to the ServiceWorkerRegistration object.
The error callback and the failed promise will receive an error object:
Handling Service Worker Updates
Set up a callback to let you know when there's a new service worker update available. You're responsible for activating the new versionβall you have to do is post a message to your service worker.
Since updating the service worker reloads all tabs that use it, it's a good idea to let the user know first when an update is ready. The user can decide when to install the update and reload pages.
You can set a callback during the initialization process to find out when the update is ready.
Or you can use a promise result after initialization. The promise will never be rejected, only resolved:
The callback and the promise receives the instance as first argument.
Whenever you (or the user) think it's the right time to update the service worker (and reload all tabs using them) you have to post a message like this one:
Then, when it gets the message, the service worker has to call the function self.skipWaiting()
when it receives the message. Here's an example of something you can include in your service worker
to handle the message { codecoupler: "cc-skip-waiting" }:
This will then trigger a reload in all open tabs and windows that use the old service worker version.
It's a good idea to install two fallbacks:
The first thing to do is to check if the service worker is still waiting, so you don't end up with multiple reloads.
The second one is to make sure the reload happens. Usually, the command above should cause a reload
in all tabs using the service worker. But if there's a service worker that doesn't understand this
message, this reload won't happen. So, you should try to unregister all service workers after a
timeout using the helper method forceUpdate.
Also, it's sometimes a good idea to add a note to the URL. That way, when you reload, you'll know that an update has happened.
Here's an example of what a stable update routine could look like:
Reference
Constructor Options
The constructor expects an object with the following properties:
serviceWorkerUrl: Path to service worker source file (Default: "/sw.js").callbackRegistered: Callback after successfull service worker registration.callbackRegistrationFailed: Callback after failed service worker registration.callbackUpdateFound: Callback after service worker update available.
Static Members
async forceUpdate(replaceState)-
This'll unregister the service worker in all the tabs that use it, and then the page will reload. You can use the argument
replaceStateto change the current URL. If you use just a hash like#forced-updatethe same page will reload, but you can use that info for more processing.
Instance Members
promise-
Promise which resolves to the instance as soon as the service worker is loaded. It'll never be rejected.
registration-
ServiceWorkerRegistrationobject of the current registered service worker.