From 13104caf45469cc883f6f1d1fd022bd4ac45f90c Mon Sep 17 00:00:00 2001 From: Mark-Fenng Date: Mon, 31 Oct 2022 20:57:45 +1100 Subject: [PATCH] fix: bug when handling shadow doms On the website https://mixpanel.com/project/2195193/view/139237/app/dashboards#id=3679845, the bottom chart makes the recorder crash (infinite loop) --- packages/rrweb/src/record/mutation.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index 5f54842b95..d13fea4428 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -394,15 +394,20 @@ export default class MutationBuffer { } // nextId !== -1 && parentId === -1 This branch can happen if the node is the child of shadow root else { - const nodeInShadowDom = _node.value; - // Get the host of the shadow dom and treat it as parent node. - const shadowHost: Element | null = nodeInShadowDom.getRootNode - ? (nodeInShadowDom.getRootNode() as ShadowRoot)?.host - : null; - const parentId = this.mirror.getId(shadowHost); - if (parentId !== -1) { - node = _node; - break; + const unhandledNode = _node.value; + // If the node is the direct child of a shadow root, we treat the shadow host as its parent node. + if ( + unhandledNode.parentNode && + unhandledNode.parentNode.nodeType === + Node.DOCUMENT_FRAGMENT_NODE + ) { + const shadowHost = (unhandledNode.parentNode as ShadowRoot) + .host; + const parentId = this.mirror.getId(shadowHost); + if (parentId !== -1) { + node = _node; + break; + } } } }