diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index 097d1a8fd5..c947b7d304 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -188,6 +188,7 @@ export default class MutationBuffer { private shadowDomManager: observerParam['shadowDomManager']; private canvasManager: observerParam['canvasManager']; private processedNodeManager: observerParam['processedNodeManager']; + private unattachedDoc: HTMLDocument; public init(options: MutationBufferParam) { ( @@ -497,14 +498,6 @@ export default class MutationBuffer { if (isIgnored(m.target, this.mirror)) { return; } - let unattachedDoc; - try { - // avoid upsetting original document from a Content Security point of view - unattachedDoc = document.implementation.createHTMLDocument(); - } catch (e) { - // fallback to more direct method - unattachedDoc = this.doc; - } switch (m.type) { case 'characterData': { const value = m.target.textContent; @@ -597,7 +590,17 @@ export default class MutationBuffer { value, ); if (attributeName === 'style') { - const old = unattachedDoc.createElement('span'); + if (!this.unattachedDoc) { + try { + // avoid upsetting original document from a Content Security point of view + this.unattachedDoc = + document.implementation.createHTMLDocument(); + } catch (e) { + // fallback to more direct method + this.unattachedDoc = this.doc; + } + } + const old = this.unattachedDoc.createElement('span'); if (m.oldValue) { old.setAttribute('style', m.oldValue); }