Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update record and playback to account for parent node missing when it… #34

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
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
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 @@ -398,9 +398,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 > 0) {
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 @@ -354,7 +354,7 @@ export function polyfill(win = window) {
}
}

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