Skip to content

Commit 96b7466

Browse files
authored
fix duplicate shadow doms in the recorder (#1002)
* fix: some shadow doms are observed multiple times and cause duplicate elements in the replayer * fix: in the live mode, the page https://bugs.chromium.org/p/chromium/issues/detail?id=1352333 has duplicate shadow doms in the replayer
1 parent 6007266 commit 96b7466

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

packages/rrweb/scripts/stream.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ void (async () => {
227227
'window.__IS_RECORDING__',
228228
);
229229
if (!isRecording) {
230+
// When the page navigates, I notice this event is emitted twice so that there are two recording processes running in a single page.
231+
// Set recording flag True ASAP to prevent recording twice.
232+
await recordedPage.evaluate('window.__IS_RECORDING__ = true');
230233
await startRecording(recordedPage, serverURL);
231234
}
232235
});

packages/rrweb/src/record/shadow-dom-manager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type BypassOptions = Omit<
1717
};
1818

1919
export class ShadowDomManager {
20+
private shadowDoms = new WeakSet<ShadowRoot>();
2021
private mutationCb: mutationCallBack;
2122
private scrollCb: scrollCallback;
2223
private bypassOptions: BypassOptions;
@@ -55,6 +56,8 @@ export class ShadowDomManager {
5556

5657
public addShadowRoot(shadowRoot: ShadowRoot, doc: Document) {
5758
if (!isNativeShadowDom(shadowRoot)) return;
59+
if (this.shadowDoms.has(shadowRoot)) return;
60+
this.shadowDoms.add(shadowRoot);
5861
initMutationObserver(
5962
{
6063
...this.bypassOptions,
@@ -106,5 +109,6 @@ export class ShadowDomManager {
106109

107110
public reset() {
108111
this.restorePatches.forEach((restorePatch) => restorePatch());
112+
this.shadowDoms = new WeakSet();
109113
}
110114
}

0 commit comments

Comments
 (0)