[FIX] website_event_track: no crash if serviceWorker unavailable

PR [1] removed the legacy error handler that swallowed promise
rejection errors with anything else than an error (namely, the
"legacyRejectPromiseHandler"). As a consequence, promise rejections
done as "control flow" now lead to error dialogs being displayed.

In particular, it happened on the website (with website_event_track
installed), if the browser doesn't support service workers.

This commit simply leaves the promise pending if the feature is
unavailable.

[1] https://github.com/odoo/odoo/pull/137702

closes odoo/odoo#143516

Signed-off-by: Samuel Degueldre (sad) <sad@odoo.com>
This commit is contained in:
Aaron Bohy
2023-11-24 23:57:38 +00:00
parent 5385b65384
commit 081966e8b0
@@ -6,13 +6,14 @@
* to avoid missed-events (as the browser can trigger it very early in the page lifecycle).
*/
var deferredPrompt = new Promise(function (resolve, reject) {
if (!("serviceWorker" in navigator)) {
return reject();
if ("serviceWorker" in navigator) {
window.addEventListener("beforeinstallprompt", function (ev) {
ev.preventDefault();
resolve(ev);
});
} else {
console.log("ServiceWorker not supported");
}
window.addEventListener("beforeinstallprompt", function (ev) {
ev.preventDefault();
resolve(ev);
});
});
import publicWidget from "@web/legacy/js/public/public_widget";
@@ -57,9 +58,7 @@
.then(this._registerServiceWorker.bind(this))
.then(function () {
// Don't wait for the prompt's Promise as it may never resolve.
deferredPrompt.then(self._showInstallBanner.bind(self)).catch(function () {
console.log("ServiceWorker not supported");
});
deferredPrompt.then(self._showInstallBanner.bind(self));
})
.then(this._prefetch.bind(this));
},
@@ -195,9 +194,6 @@
console.log("User dismissed the install prompt");
}
});
})
.catch(function () {
console.log("ServiceWorker not supported");
});
},
});