I am trying to take a specific action if my push registration fails. To make the subscription fail, I have removed the <link>
to the manifest.json
file (I'm using Chrome). I get the following error as expected:
Uncaught (in promise) DOMException: Registration failed - manifest empty or missing
However this error comes from index.html:1
rather than main.js
, where the subscription code lives:
function subscribe() {
navigator.serviceWorker.ready
.then(function(reg) {
reg.pushManager.getSubscription()
.then(function(sub) {
if (!sub) {
reg.pushManager.subscribe({userVisibleOnly: true})
.then(function(subscription) {
console.log('Subscribed to push,', subscription);
});
} else {
console.log('Already subscribed');
}
});
})
.catch(function(e) {
console.log('catch!');
// Do something
});
}
And (I suspect as a result) the catch
block is not triggering. Is this correct behavior or am I likely doing something wrong?
More details:
I am trying to simulate offline behavior, which is why I have removed the link to manifest.json
(which would be unavailable offline unless cached). If the subscription fails because the app is offline, I'd like to take action in the catch
(e.g. queue an analytics hit or update UI).