Skip to content

Commit 47b555a

Browse files
committed
ignore prefetch and preload script links
related to: 1. rrweb-io/rrweb#52 2. rrweb-io/rrweb#297 3. rrweb-io/rrweb#597
1 parent 10eead6 commit 47b555a

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/rebuild.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ function getTagName(n: elementNode): string {
5858
}
5959

6060
// based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
61-
function escapeRegExp(string: string) {
62-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
61+
function escapeRegExp(str: string) {
62+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
6363
}
6464

6565
const HOVER_SELECTOR = /([^\\]):hover/;
@@ -84,7 +84,9 @@ export function addHoverClass(cssText: string): string {
8484
}
8585
});
8686

87-
if (selectors.length === 0) return cssText;
87+
if (selectors.length === 0) {
88+
return cssText;
89+
}
8890

8991
const selectorMatcher = new RegExp(
9092
selectors
@@ -170,12 +172,25 @@ function buildNode(
170172
} else if (
171173
tagName === 'meta' &&
172174
n.attributes['http-equiv'] === 'Content-Security-Policy' &&
173-
name == 'content'
175+
name === 'content'
174176
) {
175177
// If CSP contains style-src and inline-style is disabled, there will be an error "Refused to apply inline style because it violates the following Content Security Policy directive: style-src '*'".
176178
// And the function insertStyleRules in rrweb replayer will throw an error "Uncaught TypeError: Cannot read property 'insertRule' of null".
177179
node.setAttribute('csp-content', value);
178180
continue;
181+
} else if (
182+
tagName === 'link' &&
183+
n.attributes.rel === 'preload' &&
184+
n.attributes.as === 'script'
185+
) {
186+
// ignore
187+
} else if (
188+
tagName === 'link' &&
189+
n.attributes.rel === 'prefetch' &&
190+
typeof n.attributes.href === 'string' &&
191+
n.attributes.href.endsWith('.js')
192+
) {
193+
// ignore
179194
} else {
180195
node.setAttribute(name, value);
181196
}

src/snapshot.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ function serializeNode(
369369
} = options;
370370
// Only record root id when document object is not the base document
371371
let rootId: number | undefined;
372-
if ((doc as unknown as INode).__sn) {
373-
const docId = (doc as unknown as INode).__sn.id;
372+
if (((doc as unknown) as INode).__sn) {
373+
const docId = ((doc as unknown) as INode).__sn.id;
374374
rootId = docId === 1 ? undefined : docId;
375375
}
376376
switch (n.nodeType) {
@@ -568,10 +568,17 @@ function slimDOMExcluded(
568568
} else if (sn.type === NodeType.Element) {
569569
if (
570570
slimDOMOptions.script &&
571+
// script tag
571572
(sn.tagName === 'script' ||
573+
// preload link
572574
(sn.tagName === 'link' &&
573575
sn.attributes.rel === 'preload' &&
574-
sn.attributes.as === 'script'))
576+
sn.attributes.as === 'script') ||
577+
// prefetch link
578+
(sn.tagName === 'link' &&
579+
sn.attributes.rel === 'prefetch' &&
580+
typeof sn.attributes.href === 'string' &&
581+
sn.attributes.href.endsWith('.js')))
575582
) {
576583
return true;
577584
} else if (

test/__snapshots__/integration.ts.snap

+11
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@ exports[`[html file]: picture.html 1`] = `
226226
</body></html>"
227227
`;
228228

229+
exports[`[html file]: preload.html 1`] = `
230+
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
231+
<meta charset=\\"UTF-8\\" />
232+
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
233+
<title>Document</title>
234+
<link />
235+
<link />
236+
</head>
237+
<body></body></html>"
238+
`;
239+
229240
exports[`[html file]: shadow-dom.html 1`] = `
230241
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
231242
<meta charset=\\"UTF-8\\" />

test/html/preload.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
<link rel="preload" href="https://example/path/to/preload.js" as="script" />
8+
<link rel="prefetch" href="https://example/path/to/prefetch.js" />
9+
</head>
10+
<body></body>
11+
</html>

0 commit comments

Comments
 (0)