Skip to content

Commit 079ecd4

Browse files
authored
fix: "Uncaught TypeError: Cannot read property 'insertRule' of null" caused by CSP style-src (#79)
1 parent 6710e2c commit 079ecd4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/rebuild.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ function buildNode(
169169
// as setting them triggers a console.error (which shows up despite the try/catch)
170170
// Assumption: these attributes are not used to css
171171
node.setAttribute('_' + name, value);
172+
} else if (
173+
tagName === 'meta' &&
174+
n.attributes['http-equiv'] === 'Content-Security-Policy' &&
175+
name == 'content'
176+
) {
177+
// 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 '*'".
178+
// And the function insertStyleRules in rrweb replayer will throw an error "Uncaught TypeError: Cannot read property 'insertRule' of null".
179+
node.setAttribute('csp-content', value);
180+
continue;
172181
} else {
173182
node.setAttribute(name, value);
174183
}
@@ -255,7 +264,7 @@ export function buildNodeWithSN(
255264
}
256265
if (n.rootId) {
257266
console.assert(
258-
((map[n.rootId] as unknown) as Document) === doc,
267+
(map[n.rootId] as unknown as Document) === doc,
259268
'Target document should has the same root id.',
260269
);
261270
}
@@ -318,7 +327,7 @@ function handleScroll(node: INode) {
318327
if (n.type !== NodeType.Element) {
319328
return;
320329
}
321-
const el = (node as Node) as HTMLElement;
330+
const el = node as Node as HTMLElement;
322331
for (const name in n.attributes) {
323332
if (!(n.attributes.hasOwnProperty(name) && name.startsWith('rr_'))) {
324333
continue;

0 commit comments

Comments
 (0)