Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: duplicated elements in shadow doms
clean observers before taking new full snapshots
  • Loading branch information
YunFeng0817 committed Jan 17, 2023
commit ba29607748e0fd08a82e4886945f14978e71c049
44 changes: 29 additions & 15 deletions packages/rrweb/src/record/shadow-dom-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class ShadowDomManager {
private scrollCb: scrollCallback;
private bypassOptions: BypassOptions;
private mirror: Mirror;
private observerHandlers: (() => void)[] = [];
private restorePatches: (() => void)[] = [];

constructor(options: {
Expand Down Expand Up @@ -66,7 +67,7 @@ export class ShadowDomManager {
if (!isNativeShadowDom(shadowRoot)) return;
if (this.shadowDoms.has(shadowRoot)) return;
this.shadowDoms.add(shadowRoot);
initMutationObserver(
const observer = initMutationObserver(
{
...this.bypassOptions,
doc,
Expand All @@ -76,14 +77,17 @@ export class ShadowDomManager {
},
shadowRoot,
);
initScrollObserver({
...this.bypassOptions,
scrollCb: this.scrollCb,
// https://gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813
// scroll is not allowed to pass the boundary, so we need to listen the shadow document
doc: (shadowRoot as unknown) as Document,
mirror: this.mirror,
});
this.observerHandlers.push(() => observer.disconnect());
this.observerHandlers.push(
initScrollObserver({
...this.bypassOptions,
scrollCb: this.scrollCb,
// https://gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813
// scroll is not allowed to pass the boundary, so we need to listen the shadow document
doc: (shadowRoot as unknown) as Document,
mirror: this.mirror,
}),
);
// Defer this to avoid adoptedStyleSheet events being created before the full snapshot is created or attachShadow action is recorded.
setTimeout(() => {
if (
Expand All @@ -94,12 +98,14 @@ export class ShadowDomManager {
shadowRoot.adoptedStyleSheets,
this.mirror.getId(shadowRoot.host),
);
initAdoptedStyleSheetObserver(
{
mirror: this.mirror,
stylesheetManager: this.bypassOptions.stylesheetManager,
},
shadowRoot,
this.observerHandlers.push(
initAdoptedStyleSheetObserver(
{
mirror: this.mirror,
stylesheetManager: this.bypassOptions.stylesheetManager,
},
shadowRoot,
),
);
}, 0);
}
Expand Down Expand Up @@ -135,6 +141,14 @@ export class ShadowDomManager {

public clearCache() {
this.shadowDoms = new WeakSet();
this.observerHandlers.forEach((handler) => {
try {
handler();
} catch (e) {
//
}
});
this.observerHandlers = [];
}

public reset() {
Expand Down