Skip to content

Commit 383d02d

Browse files
committed
update record and playback to account for parent node missing when it will be added later
1 parent 83c66c4 commit 383d02d

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

.changeset/dirty-scissors-promise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"rrweb": patch
3+
---
4+
5+
updating record and playback side to account for mutations where a node is missing a parent but it gets added in a different iteration of the mutation

packages/rrweb/src/record/mutation.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,11 @@ export default class MutationBuffer {
367367
}
368368

369369
for (const n of this.movedSet) {
370+
const parentNode = dom.parentNode(n);
370371
if (
371372
isParentRemoved(this.removesSubTreeCache, n, this.mirror) &&
372-
!this.movedSet.has(dom.parentNode(n)!)
373+
!this.movedSet.has(parentNode!) &&
374+
(!this.addedSet.has(parentNode!))
373375
) {
374376
continue;
375377
}

packages/rrweb/src/replay/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import {
8383
getPositionsAndIndex,
8484
uniqueTextMutations,
8585
StyleSheetMirror,
86+
type ResolveTree
8687
} from '../utils';
8788
import getInjectStyleRules from './styles/inject-style';
8889
import './styles/style.css';
@@ -1707,6 +1708,18 @@ export class Replayer {
17071708
appendNode(mutation);
17081709
});
17091710

1711+
const nodeIdsToBeAdded = (resolveTrees: Array<ResolveTree>) => {
1712+
const ids = new Set();
1713+
for (const tree of resolveTrees) {
1714+
ids.add(tree.value.node.id);
1715+
if (tree.children && tree.children.length > 0) {
1716+
const res = nodeIdsToBeAdded(tree.children);
1717+
res.forEach(id => ids.add(id));
1718+
}
1719+
}
1720+
return ids;
1721+
};
1722+
17101723
const startTime = Date.now();
17111724
while (queue.length) {
17121725
// transform queue to resolve tree
@@ -1721,7 +1734,8 @@ export class Replayer {
17211734
}
17221735
for (const tree of resolveTrees) {
17231736
const parent = mirror.getNode(tree.value.parentId);
1724-
if (!parent) {
1737+
const ids = nodeIdsToBeAdded(resolveTrees);
1738+
if (!parent && !ids.has(tree.value.parentId)) {
17251739
this.debug(
17261740
'Drop resolve tree since there is no parent for the root node.',
17271741
tree,

packages/rrweb/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export function polyfill(win = window) {
333333
}
334334
}
335335

336-
type ResolveTree = {
336+
export type ResolveTree = {
337337
value: addedNodeMutation;
338338
children: ResolveTree[];
339339
parent: ResolveTree | null;

0 commit comments

Comments
 (0)