@splitsoftware/splitio-commons 1.7.1 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGES.txt CHANGED
@@ -1,3 +1,6 @@
1
+ 1.7.2 (October 14, 2022)
2
+ - Bugfixing - Handle `Navigator.sendBeacon` API exceptions in the browser, and fallback to regular Fetch/XHR transport in case of error.
3
+
1
4
  1.7.1 (October 5, 2022)
2
5
  - Updated default value of `scheduler.featuresRefreshRate` config parameter to 60 seconds.
3
6
 
@@ -100,13 +100,14 @@ var BrowserSignalListener = /** @class */ (function () {
100
100
  if (!cache.isEmpty()) {
101
101
  var dataPayload = fromCacheToPayload ? fromCacheToPayload(cache.pop()) : cache.pop();
102
102
  if (!this._sendBeacon(url, dataPayload, extraMetadata)) {
103
- postService(JSON.stringify(dataPayload)).catch(function () { }); // no-op just to catch a possible exception
103
+ postService(JSON.stringify(dataPayload)).catch(function () { }); // no-op to handle possible promise rejection
104
104
  }
105
105
  }
106
106
  };
107
107
  /**
108
108
  * _sendBeacon method.
109
109
  * Util method that check if beacon API is available, build the payload and send it.
110
+ * Returns true if beacon API was used successfully, false otherwise.
110
111
  */
111
112
  BrowserSignalListener.prototype._sendBeacon = function (url, data, extraMetadata) {
112
113
  // eslint-disable-next-line compat/compat
@@ -121,8 +122,13 @@ var BrowserSignalListener = /** @class */ (function () {
121
122
  (0, objectAssign_1.objectAssign)(json, extraMetadata);
122
123
  // Stringify the payload
123
124
  var payload = JSON.stringify(json);
124
- // eslint-disable-next-line compat/compat
125
- return navigator.sendBeacon(url, payload);
125
+ // https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
126
+ try { // eslint-disable-next-line compat/compat
127
+ return navigator.sendBeacon(url, payload);
128
+ }
129
+ catch (e) {
130
+ return false;
131
+ }
126
132
  }
127
133
  return false;
128
134
  };
@@ -97,13 +97,14 @@ var BrowserSignalListener = /** @class */ (function () {
97
97
  if (!cache.isEmpty()) {
98
98
  var dataPayload = fromCacheToPayload ? fromCacheToPayload(cache.pop()) : cache.pop();
99
99
  if (!this._sendBeacon(url, dataPayload, extraMetadata)) {
100
- postService(JSON.stringify(dataPayload)).catch(function () { }); // no-op just to catch a possible exception
100
+ postService(JSON.stringify(dataPayload)).catch(function () { }); // no-op to handle possible promise rejection
101
101
  }
102
102
  }
103
103
  };
104
104
  /**
105
105
  * _sendBeacon method.
106
106
  * Util method that check if beacon API is available, build the payload and send it.
107
+ * Returns true if beacon API was used successfully, false otherwise.
107
108
  */
108
109
  BrowserSignalListener.prototype._sendBeacon = function (url, data, extraMetadata) {
109
110
  // eslint-disable-next-line compat/compat
@@ -118,8 +119,13 @@ var BrowserSignalListener = /** @class */ (function () {
118
119
  objectAssign(json, extraMetadata);
119
120
  // Stringify the payload
120
121
  var payload = JSON.stringify(json);
121
- // eslint-disable-next-line compat/compat
122
- return navigator.sendBeacon(url, payload);
122
+ // https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
123
+ try { // eslint-disable-next-line compat/compat
124
+ return navigator.sendBeacon(url, payload);
125
+ }
126
+ catch (e) {
127
+ return false;
128
+ }
123
129
  }
124
130
  return false;
125
131
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio-commons",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "description": "Split Javascript SDK common components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
@@ -114,7 +114,7 @@ export class BrowserSignalListener implements ISignalListener {
114
114
  if (!cache.isEmpty()) {
115
115
  const dataPayload = fromCacheToPayload ? fromCacheToPayload(cache.pop()) : cache.pop();
116
116
  if (!this._sendBeacon(url, dataPayload, extraMetadata)) {
117
- postService(JSON.stringify(dataPayload)).catch(() => { }); // no-op just to catch a possible exception
117
+ postService(JSON.stringify(dataPayload)).catch(() => { }); // no-op to handle possible promise rejection
118
118
  }
119
119
  }
120
120
  }
@@ -122,6 +122,7 @@ export class BrowserSignalListener implements ISignalListener {
122
122
  /**
123
123
  * _sendBeacon method.
124
124
  * Util method that check if beacon API is available, build the payload and send it.
125
+ * Returns true if beacon API was used successfully, false otherwise.
125
126
  */
126
127
  private _sendBeacon(url: string, data: any, extraMetadata?: {}) {
127
128
  // eslint-disable-next-line compat/compat
@@ -138,8 +139,12 @@ export class BrowserSignalListener implements ISignalListener {
138
139
  // Stringify the payload
139
140
  const payload = JSON.stringify(json);
140
141
 
141
- // eslint-disable-next-line compat/compat
142
- return navigator.sendBeacon(url, payload);
142
+ // https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
143
+ try { // eslint-disable-next-line compat/compat
144
+ return navigator.sendBeacon(url, payload);
145
+ } catch (e) {
146
+ return false;
147
+ }
143
148
  }
144
149
  return false;
145
150
  }
@@ -35,6 +35,7 @@ export declare class BrowserSignalListener implements ISignalListener {
35
35
  /**
36
36
  * _sendBeacon method.
37
37
  * Util method that check if beacon API is available, build the payload and send it.
38
+ * Returns true if beacon API was used successfully, false otherwise.
38
39
  */
39
40
  private _sendBeacon;
40
41
  }