From 33ea150b98d9e54bada11dcbe45a8393f781765e Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Mon, 29 Nov 2021 12:59:18 +0100 Subject: [PATCH] Add try/catch around use of navigator.sendBeacon (#5943) There is a range of historical browser versions where navigator.sendBeacon is available but will throw an error if it's called with an unacceptable mime-typed Blob as the data: https://bugs.chromium.org/p/chromium/issues/detail?id=490015 GitOrigin-RevId: 7b534cb482ba0de920d2e53b9f0afbf3240b20aa --- services/web/frontend/js/infrastructure/event-tracking.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/web/frontend/js/infrastructure/event-tracking.js b/services/web/frontend/js/infrastructure/event-tracking.js index 3acff43638..a864769665 100644 --- a/services/web/frontend/js/infrastructure/event-tracking.js +++ b/services/web/frontend/js/infrastructure/event-tracking.js @@ -49,5 +49,10 @@ function sendBeacon(key, data) { const blob = new Blob([JSON.stringify(data)], { type: 'application/json; charset=UTF-8', }) - navigator.sendBeacon(`/event/${key}`, blob) + try { + navigator.sendBeacon(`/event/${key}`, blob) + } catch (error) { + // Ignored. There's a range of browser for which `navigator.sendBeacon` is available but + // will throw an error if it's called with an unacceptable mime-typed Blob as the data. + } }