TLDR Web Dev · ARTICLES & TUTORIALS

SAY BYE WITH JAVASCRIPT BEACON

hemath.dev2 min read

Summary from the TLDR Web Dev newsletter

The JavaScript Beacon API provides a reliable way to send data to servers when users leave a website. This is unlike traditional methods like XMLHttpRequest or fetch in “beforeunload” events, which often fail because browsers don't wait for JavaScript execution when navigating away. The Beacon API works as a "fire-and-forget" mechanism with a simple syntax (`navigator.sendBeacon()`), allowing the browser to send the request immediately without waiting for a response, though it's limited to small amounts of data and only supports POST requests.