Index

Sending requests when a page is un loading

Testing to see if we can reliably make a final fetch request as a page unloads. This uses the pagehide event and the visibilitychange to hidden to guess that the page is about to unload. It then tries to make two fetches for each event, one with keepalive set, as well as a beacon request.

Pages can be closed in many ways. Click on a link, tab closed, page refresh, back button etc. This will log if the standard way to track these is reliable.

Results - Chrome browser

If you look at browser network requests with preserve logs you can see the request being sent, and if they get cancelled. For pings the cancellation is not an issue if the request makes it to the server.

Page Refresh

The Network tab indicated all requests sent with later ones being cancelled. The starting of loading the next page seems to be the point where incomplete requests get cancelled, which makes sense.

visibilitychange hidden with fetch keepalive or a beacon seems to be the most reliable. Not seen it fail yet.

pagehide, especially a fetch without keepalive is least reliable.

visibilitychange hidden can happen multiple times. e.g. switching tabs. This least to the problem that with hidden, we do not know if the page is being unloaded or just being hidden. However pagehide is less reliable. My commercial code reacts only to the first visibilitychange hidden or pagehide.

This is just early observations, time to wait and gain some data

Log

performance.now()