flagon-useralejs
flagon-useralejs copied to clipboard
Bug(authHeaders): Auth headers ignored for sendOnClose
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.
Assigned to @rc10house
Created Pull Request