Skip to content

Commit

Permalink
update record and playback to account for parent node missing when it…
Browse files Browse the repository at this point in the history
… will be added later
  • Loading branch information
mikeyagner committed Jan 27, 2025
1 parent 83c66c4 commit ac862c4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-scissors-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb": patch
---

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
4 changes: 3 additions & 1 deletion packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ export default class MutationBuffer {
}

for (const n of this.movedSet) {
const parentNode = dom.parentNode(n);
if (
isParentRemoved(this.removesSubTreeCache, n, this.mirror) &&
!this.movedSet.has(dom.parentNode(n)!)
!this.movedSet.has(parentNode!) &&
(!this.addedSet.has(parentNode!))
) {
continue;
}
Expand Down
16 changes: 15 additions & 1 deletion packages/rrweb/src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import {
getPositionsAndIndex,
uniqueTextMutations,
StyleSheetMirror,
type ResolveTree
} from '../utils';
import getInjectStyleRules from './styles/inject-style';
import './styles/style.css';
Expand Down Expand Up @@ -1707,6 +1708,18 @@ export class Replayer {
appendNode(mutation);
});

const nodeIdsToBeAdded = (resolveTrees: Array<ResolveTree>) => {
const ids = new Set();
for (const tree of resolveTrees) {
ids.add(tree.value.node.id);
if (tree.children && tree.children.length > 1) {
const res = nodeIdsToBeAdded(tree.children);
res.forEach(id => ids.add(id));
}
}
return ids;
};

const startTime = Date.now();
while (queue.length) {
// transform queue to resolve tree
Expand All @@ -1721,7 +1734,8 @@ export class Replayer {
}
for (const tree of resolveTrees) {
const parent = mirror.getNode(tree.value.parentId);
if (!parent) {
const ids = nodeIdsToBeAdded(resolveTrees);
if (!parent && !ids.has(tree.value.parentId)) {
this.debug(
'Drop resolve tree since there is no parent for the root node.',
tree,
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export function polyfill(win = window) {
}
}

type ResolveTree = {
export type ResolveTree = {
value: addedNodeMutation;
children: ResolveTree[];
parent: ResolveTree | null;
Expand Down

0 comments on commit ac862c4

Please sign in to comment.