Monitoring Errors while Offline

Many modern applications and PWAs support operating while the user is offline. When errors occur while offline, the errors cannot be sent to TrackJS, and will be discarded by default.

If you want to change this behavior and forward the errors on later once the user is online again, you can use the following extension that hooks into onError. This code will store any failed errors in a queue, and try to send them once receiving the “online” signal.

let offlineErrors = [];

function sendOrQueueError(errorPayload) {
  fetch("https://capture.trackjs.com/capture", {
    method: "POST",
    body: JSON.stringify(errorPayload)
  }).catch((err) => {
    offlineErrors.push(errorPayload);
  });
}

window.addEventListener("online", () => {
  offlineErrors.forEach((errorPayload) => {
    fetch("https://capture.trackjs.com/capture", {
      method: "POST",
      body: JSON.stringify(errorPayload)
    });
  });
  offlineErrors.length = 0;
});

TrackJS.install({
  token: "YOUR_TOKEN",
  onError: function(errorPayload) {
    sendOrQueueError(error);
    return false;
  }
});
Resend Offline Errors