Skip to content

Commit

Permalink
fix: angular wrapped mutationobserver detection (#1597)
Browse files Browse the repository at this point in the history
* fix: angular wrapped mutationobserver detection

* add change set

* fix

* prettier

* following posthog prod

* manually prettier

* Update .changeset/moody-experts-build.md

Co-authored-by: Justin Halsall <Juice10@users.noreply.github.com>

---------

Co-authored-by: Justin Halsall <Juice10@users.noreply.github.com>
  • Loading branch information
pauldambra and Juice10 authored Dec 6, 2024
1 parent 53b83bb commit bd9eb70
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/moody-experts-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rrweb/record": patch
---

Correctly detect when angular has wrapped mutation observer
19 changes: 18 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ const testableMethods = {

const untaintedBasePrototype: Partial<BasePrototypeCache> = {};

/*
When angular patches things - particularly the MutationObserver -
they pass the `isNativeFunction` check
That then causes performance issues
because Angular's change detection
doesn't like sharing a mutation observer
Checking for the presence of the Zone object
on global is a good-enough proxy for Angular
to cover most cases
(you can configure zone.js to have a different name
on the global object and should then manually run rrweb
outside the Zone)
*/
export const isAngularZonePresent = (): boolean => {
return !!(globalThis as { Zone?: unknown }).Zone;
};

export function getUntaintedPrototype<T extends keyof BasePrototypeCache>(
key: T,
): BasePrototypeCache[T] {
Expand Down Expand Up @@ -63,7 +80,7 @@ export function getUntaintedPrototype<T extends keyof BasePrototypeCache>(
),
);

if (isUntaintedAccessors && isUntaintedMethods) {
if (isUntaintedAccessors && isUntaintedMethods && !isAngularZonePresent()) {
untaintedBasePrototype[key] = defaultObj.prototype as BasePrototypeCache[T];
return defaultObj.prototype as BasePrototypeCache[T];
}
Expand Down

0 comments on commit bd9eb70

Please sign in to comment.