flagon-useralejs icon indicating copy to clipboard operation
flagon-useralejs copied to clipboard

Bug(authHeaders): Auth headers ignored for sendOnClose

Open EandrewJones opened this issue 1 year ago • 2 comments

Problem

The sendOnClose method to flush logs before the user closes/tabs away from the client app does not utilize any header information, nor can it.

/**
 * Attempts to flush the remaining logs when the window is closed.
 * @param  {Array} logs   Array of logs to be flushed.
 * @param  {Object} config Configuration object to be read from.
 */
export function sendOnClose(logs, config) {
  window.addEventListener("pagehide", function () {
    if (config.on && logs.length > 0) {
      navigator.sendBeacon(config.url, JSON.stringify(logs));
      logs.splice(0); // clear log queue
    }
  });
}

Cause

navigator.sendBeacon does not support auth headers, so this will fail if auth is required.

Solution

The alternative is to use fetch() with keepalive: true as per official docs and SO suggestions.

EandrewJones avatar Jan 17 '24 20:01 EandrewJones

Assigned to @rc10house

EandrewJones avatar May 07 '24 17:05 EandrewJones

Created Pull Request

rc10house avatar May 09 '24 15:05 rc10house