Why
Inline style parsing creates a new CSSStyleSheet on every call. Reuse one sheet per parser to avoid repeated allocations.
This change also addresses stylesheet accumulation observed on an animated development page with Chrome's CSS Inspector enabled. Heap analysis found strong paths through InspectorCSSAgent to the allocated stylesheets.
After applying the same reuse change to the SDK bundle, a follow-up observation recorded no further CSS.styleSheetAdded events after startup while animation and Replay remained active. This was a single instrumented application run, not an isolated memory benchmark.
The allocation concern noted in #286 deserves explicit coverage. This change calls replaceSync() to replace the rules rather than changing a cached rule's declarations. The native browser test retains earlier declarations and verifies that later parses do not change their values.
What changed
- Lazily create one stylesheet per
StyleDeclarationParser.
- Cover repeated parsing, parser isolation, and fallback after construction or replacement fails.
- Add a native browser recorder test for retained declarations, emitted diffs,
!important, and property removal.
Scope
Inline style parsing during recording. No public API or event format changes. This PR establishes allocation reduction, without a runtime speed or memory benchmark claim.
Blast Radius
Repeated style mutations reuse parser state. Native browser coverage checks that earlier parse results remain stable, and unit tests cover the existing detached-element fallback.
Verification
yarn build:all and yarn check-types passed across the workspace.
- In
packages/rrweb, PUPPETEER_HEADLESS=true yarn vitest run test/record.test.ts test/style-declaration-parser.test.ts passed all 36 tests with Chrome Headless Shell 146.0.7680.153 selected via PUPPETEER_EXECUTABLE_PATH.
- The native recorder regression fails without reuse (
expected 2 to be 1). Changed production code passes ESLint, and all changed files pass Prettier. A separate native check with style-src 'none' produced no CSP violations before or after the change.