Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use navigator.sendBeacon with Rollbar
// This function will be used as transorm call and actually sends the payload
function beaconRollbar(payload) {
// create blob to set content type to JSON - needed for correct HTTP header
const blob = new Blob([JSON.stringify(payload)], { type: 'application/json; charset=UTF-8' });
navigator.sendBeacon(`${window.Rollbar.options.endpoint}item/?post_client_item=${window.Rollbar.options.accessToken}`, blob);
}
/**
* Rollbar normal init code goes here
* const rollbarConfig = { .. }
*/
// Now, check if both Blob and navigator.sendBeacon are supported and switch to it
if (typeof Blob !== 'undefined' && typeof navigator.sendBeacon === 'function') {
// we will disable normal send and send payloads from transorm function
rollbarConfig.enabled = false;
rollbarConfig.transform = beaconRollbar;
rollbarConfig.payload.notifier = { name: 'rollbar-browser-beacon' };
}
// and do normal init now (that's Browserify version)
const rb = Rollbar.init(rollbarConfig);
window.Rollbar = rb;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.