Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using fetch API as transport for Rollbar
function fetchTransport(payload) {
fetch(`${window.Rollbar.options.endpoint}item/`, {
method: 'POST',
body: JSON.stringify(payload),
redirect: 'follow',
headers: new Headers({ 'Content-Type': 'application/json; charset=UTF-8',
'X-Rollbar-Access-Token': window.Rollbar.options.accessToken,
}),
})
.catch(err => console.error('Rollbar fetch transport failed', err));
}
// Normal Rollbar init goes here (in this example - Browserify version used)
const rollbarConfig = {
accessToken: 'ROLLBAR_API_TOKEN',
// ...
}
// Check if fetch supported and use it instead of XHR tranport
if ('fetch' in window) {
rollbarConfig.enabled = false;
rollbarConfig.transform = fetchTransport;
rollbarConfig.payload.notifier = { name: 'rollbar-browser-fetch' };
}
// now init Rollbar
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.