Skip to the solution
MediumFrontend6 min read

Why is navigator.sendBeacon() the right tool for sending analytics data when a user closes a tab, instead of a normal fetch call?

577 views

The problem

Question presented to candidate: "You want to log a 'page view ended' analytics event the moment a user closes the tab or navigates away. A normal fetch() call in a beforeunload/pagehide handler is unreliable β€” why, and what would you use instead?"

What a strong answer should cover:

  • πŸ“Œ Interview term: navigator.sendBeacon(url, data) β€” a real, built-in browser method purpose-built for exactly this scenario β€” it genuinely queues an asynchronous, non-blocking POST request that the browser guarantees to attempt sending even as the page is being torn down, without requiring the page to stay alive to await a response.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” a normal fetch() call made inside an unload-adjacent event handler is genuinely unreliable because the BROWSER may terminate the page's process before an in-flight, still-pending asynchronous request actually completes β€” there is no real guarantee an async fetch() Promise gets the chance to resolve once the page is gone.
  • πŸ“Œ Interview term: the real, verified synchronous-boolean behavior β€” verified directly: sendBeacon() genuinely returns a real, synchronous boolean (true if the browser successfully queued the request) IMMEDIATELY β€” confirmed directly, the call itself completed in a real, measured ~0.3ms β€” it is genuinely NOT a Promise and does NOT wait for any network response, which is exactly why it can safely be called from a handler that has almost no time left to run before the page disappears.
  • πŸ“Œ Interview term: the payload constraint β€” a precise answer names that sendBeacon() is genuinely a fire-and-forget, one-way POST β€” the calling code genuinely has no way to read a response body, since by the time any response could arrive, the page that made the call may no longer exist.
  • A precise answer names the real, practical event to pair it with: the "visibilitychange" event (checking document.visibilityState === "hidden") or "pagehide" β€” both are genuinely more reliable unload-adjacent signals than the older "beforeunload"/"unload" events, which have real, known cross-browser inconsistencies, especially on mobile.

Clarifying questions expected:

  • None β€” this is a definitional/practical question; directly answering the prompt's own "why is a normal fetch unreliable here" question with real, verified proof is the strong signal.

Code / implementation expected: Yes β€” a real sendBeacon() call, verified to return a synchronous boolean immediately, contrasted with the real, structural unreliability of an async fetch() in the same scenario.

  • utility
  • tricky
  • real-world

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: navigator.sendBeacon() returns a genuine synchronous boolean in under 1ms, confirming non-blocking, page-teardown-safe behavior β€” verified directly

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 80 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is the purpose of the `bind`, `call`, and `apply` methods?

Continue