Skip to content

fix(utils): use Zone.js unpatched MutationObserver on Safari#1826

Open
d0ugal wants to merge 2 commits into
rrweb-io:masterfrom
d0ugal:fix/zonejs-mutation-observer-safari
Open

fix(utils): use Zone.js unpatched MutationObserver on Safari#1826
d0ugal wants to merge 2 commits into
rrweb-io:masterfrom
d0ugal:fix/zonejs-mutation-observer-safari

Conversation

@d0ugal
Copy link
Copy Markdown

@d0ugal d0ugal commented May 6, 2026

Problem

When Angular's Zone.js is present, it monkey-patches MutationObserver. rrweb's getUntaintedPrototype detects the patched version and falls back to creating a hidden iframe, then uses iframe.contentWindow.MutationObserver to get an unpatched observer.

On Safari/WebKit, a MutationObserver obtained from an iframe's contentWindow silently never fires callbacks when observing nodes in the parent document. This means rrweb captures zero DOM mutations — full snapshots work, but all incremental changes are lost.

Fix

Zone.js stores original unpatched globals under globalThis[Zone.__symbol__(key)]. Before attempting the iframe fallback, check if Zone.js has stored the native MutationObserver and use it directly.

const zoneAlternative = angularZoneUnpatchedAlternative(key);
if (zoneAlternative) {
  return zoneAlternative.prototype;
}

This is the same approach used by PostHog: PostHog/posthog-js#1687

Testing

Verified with Angular apps (Zone.js present) across Chrome, Firefox, and Safari (Playwright WebKit). After the fix, Safari sessions capture 146+ mutations where previously they captured zero.

Angular's Zone.js monkey-patches MutationObserver, causing rrweb's
getUntaintedPrototype to fall back to creating an iframe and using its
contentWindow.MutationObserver. On Safari/WebKit, a MutationObserver
from an iframe's contentWindow silently never fires callbacks when
observing the parent document — resulting in zero DOM mutations captured.

Zone.js stores the original unpatched globals under a symbol key
(Zone.__symbol__). Use this to retrieve the native MutationObserver
before attempting the iframe fallback.

Ref: PostHog/posthog-js#1687
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 6, 2026

⚠️ No Changeset found

Latest commit: 5b25627

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@d0ugal d0ugal marked this pull request as ready for review May 11, 2026 12:09
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.

1 participant