I have a case where I need to catch a rejected promise from a Service Worker install event and display an error message.
I've set up a test Service Worker called testsw.js
which immediately throws a rejected promise, like so:
console.log("Hello from SW");
self.addEventListener('install', function (evt) {
evt.waitUntil(Promise.reject(new Error("Test")));
})
and I'm registering the Service Worker using the following code:
navigator.serviceWorker.register('/testsw.js')
.then(function (reg) {
console.log("Service worker successfully registered.", reg);
})
.catch(function (err) {
console.error("Service worker failed to register.", err);
})
The catch
function is never hit in this case, and registering the Service worker results in this console output:
testsw.js:1 Hello from SW
testsw.js:3 Uncaught (in promise) Error: Test
at testsw.js:3
(anonymous) @ testsw.js:3
(index):468 Service worker successfully registered. ServiceWorkerRegistration
I've already tried wrapping the rejected promise in a function and calling that in evt.waitUntil()
, as well as throwing a plain Error, with no change. The Service Worker is still put into the redundant state, which is good, but I still need to know that the installation failed.
Am I misunderstanding how rejected promises work in a Service Worker installation? Is there something misconfigured in the registration that would make it always hit the then
block, even if the promise is rejected? Failing that, is there something in the ServiceWorkerRegistration
class or somewhere else that would tell us that the service worker failed to install or update?
If it makes a difference, this was tested in Chrome 77 and Edge 83 with no success.