Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track patched XHR requests for network breadcrumbs #2055

Merged
merged 1 commit into from
Dec 14, 2023

Conversation

yousif-bugsnag
Copy link
Contributor

Goal

Fixes two issues with the current xhr monkey patch for network breadcrumbs:

  • previously XMLHttpRequest.send was overridden on the instance in order to record the duration of the request. However this causes a potential incompatibility issue with other libraries that may be monkey patching XHR and calling this.send - as observed with msw, where both libraries call into each other causing an infinite loop.
  • If an XHR instance is re-used (i.e. open is called more than once) event listeners from previous uses were not removed, resulting in duplicate breadcrumbs being logged.

Design

send is now overridden on the prototype rather than the instance, and WeakMaps are used to store metadata and event listeners associated with tracked XHR instances.

The open override now just adds the request instance to a weak map along with it's method and url. When send is called, the request metadata and any existing event listeners are retrieved - if there are previous event listeners for the request these are removed before new ones are added.

XHR is not monkey-patched (and XHR breadcrumbs are disabled) if WeakMap is not available - this affects IE<=10.

Testing

Updated unit tests and relied on existing e2e tests.

Copy link

@bugsnag/browser bundle size diff

Minified Minfied + Gzipped
Before 44.42 kB 13.55 kB
After 44.71 kB 13.63 kB
± ⚠️ +291 bytes ⚠️ +84 bytes

code coverage diff

<temporarily disabled>

Generated by 🚫 dangerJS against fe52193

@yousif-bugsnag yousif-bugsnag merged commit c9ccb54 into integration/v8 Dec 14, 2023
154 checks passed
@yousif-bugsnag yousif-bugsnag deleted the PLAT-11296/fix-xhr-monkey-patch branch December 14, 2023 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants