From 68b39c82c48efc0ce09ba6c6e6f0928b557b0255 Mon Sep 17 00:00:00 2001 From: Lucky Feng Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] fix: "Uncaught TypeError: Cannot read property 'insertRule' of null" caused by CSP style-src (#79) --- src/rebuild.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/rebuild.ts b/src/rebuild.ts index 7a4d4422..0d269b0f 100644 --- a/src/rebuild.ts +++ b/src/rebuild.ts @@ -169,6 +169,15 @@ function buildNode( // as setting them triggers a console.error (which shows up despite the try/catch) // Assumption: these attributes are not used to css node.setAttribute('_' + name, value); + } else if ( + tagName === 'meta' && + n.attributes['http-equiv'] === 'Content-Security-Policy' && + name == 'content' + ) { + // 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 '*'". + // And the function insertStyleRules in rrweb replayer will throw an error "Uncaught TypeError: Cannot read property 'insertRule' of null". + node.setAttribute('csp-content', value); + continue; } else { node.setAttribute(name, value); } @@ -255,7 +264,7 @@ export function buildNodeWithSN( } if (n.rootId) { console.assert( - ((map[n.rootId] as unknown) as Document) === doc, + (map[n.rootId] as unknown as Document) === doc, 'Target document should has the same root id.', ); } @@ -318,7 +327,7 @@ function handleScroll(node: INode) { if (n.type !== NodeType.Element) { return; } - const el = (node as Node) as HTMLElement; + const el = node as Node as HTMLElement; for (const name in n.attributes) { if (!(n.attributes.hasOwnProperty(name) && name.startsWith('rr_'))) { continue;