ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

[BUG] Use AbortController to abort fetch requets on pagehide

Open rggammon opened this issue 2 months ago • 0 comments

Missing Telemetry When Switching Tabs - Request for AbortController Integration

Problem Description

I'm experiencing missing telemetry data when users switch tabs, with the beacon API disabled as it is not reliable. Fetch requests appear to get stuck in a pending state when tabs are backgrounded and don't resume properly when foregrounded, leading to telemetry loss.

Perhaps these requests can be configured to be aborted? Then, an offline or localstorage channel can retry when the tab is foregrounded

Current Behavior (Without Beacon API)

  1. On pagehide: Fetch requests initiated before tab backgrounding remain in pending state indefinitely
  2. On Tab Foregrounding: Pending fetch requests don't resume

Observed Issues in SenderPostManager.ts

  • _doFetchSender function doesn't implement request cancellation
  • Fetch requests can hang indefinitely during tab backgrounding
  • doAwaitResponse callback may never execute for suspended requests

Proposed Solution

I'd like to propose the following enhancements:

1. AbortController Integration

  • Add AbortController support to _doFetchSender function
  • Cancel active fetch requests on pagehide event if beacon is not being used or if telemetry is stored through an offline channel.
  • Error handling will allow using localstorage type channel to queue telemetry for later sending

Use Case

My application needs reliable telemetry delivery, and I prefer to:

  • Disable beacon API, as beacon is not working consistently and we don't want gaps
  • Use a local storage channel to store telemetry when pending fetches are aborted
  • Abort hanging requests cleanly rather than letting them accumulate
  • Ensure telemetry is eventually sent if/when users return to tab

rggammon avatar Oct 21 '25 18:10 rggammon