diff --git a/packages/rrweb/src/replay/index.ts b/packages/rrweb/src/replay/index.ts index c1f9fbb0d2..9c002a997d 100644 --- a/packages/rrweb/src/replay/index.ts +++ b/packages/rrweb/src/replay/index.ts @@ -1602,7 +1602,10 @@ export class Replayer { const mirror = this.usingVirtualDom ? this.virtualDom.mirror : this.mirror; type TNode = typeof mirror extends Mirror ? Node : RRNode; - d.removes = d.removes.filter((mutation) => { + // filter into a local list rather than reassigning `d.removes`: the event + // object is re-applied on every seek, so a node that is only transiently + // absent here must not drop the removal from later replays + const removes = d.removes.filter((mutation) => { // warn of absence from mirror before we start applying each removal // as earlier removals could remove a tree that includes a later removal if (!mirror.getNode(mutation.id)) { @@ -1611,7 +1614,7 @@ export class Replayer { } return true; }); - d.removes.forEach((mutation) => { + removes.forEach((mutation) => { const target = mirror.getNode(mutation.id); if (!target) { // no need to warn here, an ancestor may have already been removed @@ -1905,7 +1908,7 @@ export class Replayer { uniqueTextMutations(d.texts).forEach((mutation) => { const target = mirror.getNode(mutation.id); if (!target) { - if (d.removes.find((r) => r.id === mutation.id)) { + if (removes.find((r) => r.id === mutation.id)) { // no need to warn, element was already removed return; } @@ -1925,7 +1928,7 @@ export class Replayer { d.attributes.forEach((mutation) => { const target = mirror.getNode(mutation.id); if (!target) { - if (d.removes.find((r) => r.id === mutation.id)) { + if (removes.find((r) => r.id === mutation.id)) { // no need to warn, element was already removed return; } diff --git a/packages/rrweb/test/events/remove-node.ts b/packages/rrweb/test/events/remove-node.ts new file mode 100644 index 0000000000..a6dd824e89 --- /dev/null +++ b/packages/rrweb/test/events/remove-node.ts @@ -0,0 +1,91 @@ +import { EventType, IncrementalSource } from '@sentry/rrweb-types'; +import type { eventWithTime } from '@sentry/rrweb-types'; + +const now = Date.now(); + +// a node (id 10) is added by one mutation and removed by a later one +const events: eventWithTime[] = [ + { + type: EventType.Meta, + data: { + href: 'http://localhost', + width: 1000, + height: 800, + }, + timestamp: now, + }, + { + type: EventType.FullSnapshot, + data: { + node: { + id: 1, + type: 0, + childNodes: [ + { id: 2, name: 'html', type: 1, publicId: '', systemId: '' }, + { + id: 3, + type: 2, + tagName: 'html', + attributes: {}, + childNodes: [ + { + id: 4, + type: 2, + tagName: 'head', + attributes: {}, + childNodes: [], + }, + { + id: 5, + type: 2, + tagName: 'body', + attributes: {}, + childNodes: [], + }, + ], + }, + ], + }, + initialOffset: { top: 0, left: 0 }, + }, + timestamp: now + 10, + }, + // add