Skip to content

Avoid the costly createElement calls during snapshot #1434

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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/avoid-costly-createlement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb-snapshot': patch
---

Avoid the costly createElement calls during snapshot
39 changes: 28 additions & 11 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,29 @@ export function absoluteToDoc(doc: Document, attributeValue: string): string {
if (!attributeValue || attributeValue.trim() === '') {
return attributeValue;
}
const a: HTMLAnchorElement = doc.createElement('a');
a.href = attributeValue;
return a.href;
try {
return new URL(attributeValue, doc.location.href).href;
} catch (e) {
const a = doc.createElement('a');
a.setAttribute('href', attributeValue);
return a.href;
}
}

function isSVGElement(el: Element): boolean {
return Boolean(el.tagName === 'svg' || (el as SVGElement).ownerSVGElement);
}

function getHref() {
// return a href without hash
const a = document.createElement('a');
a.href = '';
return a.href;
function getHrefWithoutHash(doc: Document) {
try {
const url = new URL(doc.location.href);
url.hash = '';
return url.href;
} catch (e) {
const a = doc.createElement('a');
a.setAttribute('href', ''); // this also removes the hash
return a.href;
}
}

export function transformAttribute(
Expand Down Expand Up @@ -243,7 +252,7 @@ export function transformAttribute(
} else if (name === 'srcset') {
return getAbsoluteSrcsetString(doc, value);
} else if (name === 'style') {
return absoluteToStylesheet(value, getHref());
return absoluteToStylesheet(value, getHrefWithoutHash(doc));
} else if (tagName === 'object' && name === 'data') {
return absoluteToDoc(doc, value);
}
Expand Down Expand Up @@ -505,6 +514,7 @@ function serializeNode(
});
case n.TEXT_NODE:
return serializeTextNode(n as Text, {
doc,
needsMask,
maskTextFn,
rootId,
Expand Down Expand Up @@ -535,6 +545,7 @@ function getRootId(doc: Document, mirror: Mirror): number | undefined {
function serializeTextNode(
n: Text,
options: {
doc: Document;
needsMask: boolean | undefined;
maskTextFn: MaskTextFn | undefined;
rootId: number | undefined;
Expand Down Expand Up @@ -566,7 +577,10 @@ function serializeTextNode(
n,
);
}
textContent = absoluteToStylesheet(textContent, getHref());
textContent = absoluteToStylesheet(
textContent,
getHrefWithoutHash(options.doc),
);
}
if (isScript) {
textContent = 'SCRIPT_PLACEHOLDER';
Expand Down Expand Up @@ -660,7 +674,10 @@ function serializeElementNode(
(n as HTMLStyleElement).sheet as CSSStyleSheet,
);
if (cssText) {
attributes._cssText = absoluteToStylesheet(cssText, getHref());
attributes._cssText = absoluteToStylesheet(
cssText,
getHrefWithoutHash(doc),
);
}
}
// form fields
Expand Down