Skip to content
All writing ‹ Part 02 of 11 · Mobile Debugging: When There Is No DevTools
Engineering · 4 min read

When the Page Closes, Does the Request Still Arrive?

When a page closes, the browser aborts its pending fetch calls. Which requests survive, why keepalive and sendBeacon do, and what they cannot do for you.

You send a request, and the user leaves the page one millisecond later. Did the request arrive? For a normal fetch, often not.

The problem

When a page goes away, the browser cancels the page’s pending requests. A click ping (a background request that reports the tap) is sent exactly at that moment: the user taps, and the page is about to leave. So a plain fetch can die halfway. The same problem hits analytics events, “save on close”, and logout calls on any website.

Page close vs browser close The browser app owns the network. When a page closes, keepalive and sendBeacon requests are still sent, and fetch and XMLHttpRequest are aborted. When the browser closes, all pending requests die. Browser app one process, owns the network Page closes Still sent keepalive, sendBeacon Aborted fetch, XMLHttpRequest Browser closes Pending requests die the process is gone
A page close is not a browser close. The browser still runs, so it can finish the requests that asked to outlive the page.

A page close means that the tab closes, the tab goes to another page, or the app closes the WebView (the browser engine inside the app). The browser app stays open.

Which requests survive

Method Survives page close? Why
fetch(url) No The browser aborts it with the page.
XMLHttpRequest No The browser aborts it with the page.
XMLHttpRequest that waits (sync), in unload No Chrome 80 and later block it while the page closes.
<img> pixel: new Image().src = url Not reliable An old trick. Most browsers delay the close to load it, which slows the next page.
fetch(url, {keepalive: true}) Yes The browser finishes it. All keepalive bodies share 64 KB.
navigator.sendBeacon(url, data) Yes POST only. The browser queues it and sends it later.

XMLHttpRequest is the older browser API for background requests. unload is the last event of a closing page. A pixel is a 1x1 image whose URL carries the data.

fetch("https://ads.example/click", { keepalive: true });
navigator.sendBeacon("https://ads.example/click", payload);

Both are fire and forget: the page never reads the answer. A true from sendBeacon means that the browser queued the request, not that the server got it. Backend analogy: publish to a queue without waiting for the acknowledgment.

In the example

For example, a click script (the ad’s JavaScript that records the tap and opens the ad’s page) can send its ping with fetch and keepalive. The feed around it (the scrolling list of posts and ads in the app) can report its own click with sendBeacon. Here, both pings reached their servers on every tap. So both sides counted clicks while the user saw nothing open (The Data Says Clicked, the User Says Nothing Happened).

That is the trap. These tools keep the count alive. They never open the landing page (the page that the ad promises to open). The navigation must happen on its own path.

keepalive keeps the message alive. It does not carry the user anywhere.

References


Related: revisit The Data Says Clicked, the User Says Nothing Happened, see the pings in a recording with Record the Phone Traffic, Then Read It, or jump to Takeaway: Check One Layer at a Time, and Test Each Guess with the Right Tool. Back to the series overview.

↑↓ navigate ↵ open esc close