I'm writing my first app with Service Workers and am trying to get the registration in order to interact with the pushManager.
I can do this with either of the following:
nagivator.serviceWorker.getRegistration()
or
navigator.serviceWorker.ready.then()
Which should I use and what is the difference?
As far I can understand the first one does not wait until the ServiceWorkerRegistration has an active ServiceWorker. The last one does.
Using ready makes things simpler because it is "designed in a way that the returned promise will never reject. Instead, it waits until the promise resolves with a service worker registration that has an active worker." (see the definition of ready in the Service Worker spec).
Wich in turn will result in fewer failure modes when calling Push API's subscribe method.
Calling subscribe from a getRegistration() context, adds a few extra failure modes:
[...snip...]
[...snip...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With