Skip to content

Commit 73a4e5c

Browse files
author
Paul Boocock
committed
Update beacon support to handle "gotchas" (close #716)
1 parent 8fd807f commit 73a4e5c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/js/lib/helpers.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,37 @@
348348
}
349349
};
350350

351+
/**
352+
* Attempt to get a value from localStorage
353+
*
354+
* @param string key
355+
* @return string The value obtained from localStorage, or
356+
* undefined if localStorage is inaccessible
357+
*/
358+
object.attemptGetSessionStorage = function (key) {
359+
try {
360+
return sessionStorage.getItem(key);
361+
} catch(e) {
362+
return null;
363+
}
364+
};
365+
366+
/**
367+
* Attempt to write a value to localStorage
368+
*
369+
* @param string key
370+
* @param string value
371+
* @return boolean Whether the operation succeeded
372+
*/
373+
object.attemptWriteSessionStorage = function (key, value) {
374+
try {
375+
sessionStorage.setItem(key, value);
376+
return true;
377+
} catch(e) {
378+
return false;
379+
}
380+
};
381+
351382
/**
352383
* Finds the root domain
353384
*/

src/js/out_queue.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@
300300
if (batch.length > 0) {
301301
var beaconStatus;
302302

303-
if (useBeacon) {
303+
if (useBeacon && helpers.attemptGetSessionStorage('sp_corsPreflight')) {
304304
const headers = { type: 'application/json' };
305305
const blob = new Blob([encloseInPayloadDataEnvelope(attachStmToEvent(batch))], headers);
306306
try {
@@ -319,6 +319,7 @@
319319

320320
if (!useBeacon || !beaconStatus) {
321321
xhr.send(encloseInPayloadDataEnvelope(attachStmToEvent(batch)));
322+
helpers.attemptWriteSessionStorage('sp_corsPreflight', true);
322323
}
323324
}
324325

0 commit comments

Comments
 (0)