DEV Community

Adel
Adel

Posted on

What is Beacon request

navigator.sendBeacon() method asynchronously sends an HTTP POST request in reliable way, it's mostly used to send analytics.

it returns true if the user agent successfully queued the data for transfer. Otherwise, it returns false.

Unlike traditional HTTP requests, sendBeacon will always reach to the server even if you unload or exit the page in the middle of the request.

Example

document.addEventListener('visibilitychange', function logData() {
  if (document.visibilityState === 'hidden') {
    navigator.sendBeacon('/log', analyticsData);
  }
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)