Browse Source

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
Miguel Serrano 4 years ago
parent
commit
33ea150b98
1 changed files with 6 additions and 1 deletions
  1. 6 1
      services/web/frontend/js/infrastructure/event-tracking.js

+ 6 - 1
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.
+  }
 }