Skip to content

fix(snapshot): use link.sheet for CSS capture on WebKit#1825

Open
d0ugal wants to merge 2 commits into
rrweb-io:mainfrom
d0ugal:fix/link-sheet-inline-css
Open

fix(snapshot): use link.sheet for CSS capture on WebKit#1825
d0ugal wants to merge 2 commits into
rrweb-io:mainfrom
d0ugal:fix/link-sheet-inline-css

Conversation

@d0ugal

@d0ugal d0ugal commented May 6, 2026

Copy link
Copy Markdown

Problem

When inlineStylesheet is enabled, rrweb searches document.styleSheets to find the CSSStyleSheet for each <link> element and inline its rules as _cssText.

On WebKit (Safari), at readyState: "interactive", external stylesheets loaded via <link> are not yet listed in document.styleSheets — only inline <style> elements appear. This means full snapshots taken at page load capture _cssText: "" for all external stylesheets, producing unstyled replays.

However, HTMLLinkElement.sheet is already populated at this point on WebKit.

Fix

Check link.sheet before falling back to document.styleSheets.find():

const stylesheet =
  (n as HTMLLinkElement).sheet ||
  Array.from(doc.styleSheets).find((s) => {
    return s.href === (n as HTMLLinkElement).href;
  });

Testing

Verified with Angular apps across Chrome, Firefox, and Safari (Playwright WebKit)

On WebKit at readyState "interactive", external stylesheets are not yet
listed in document.styleSheets, but HTMLLinkElement.sheet is already
populated. Fall back to link.sheet before searching document.styleSheets
so that inlineStylesheet captures CSS correctly on Safari/WebKit.
@changeset-bot

changeset-bot Bot commented May 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5c3e5f9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 19 packages
Name Type
rrweb-snapshot Patch
rrweb Patch
rrdom Patch
rrdom-nodejs Patch
rrweb-player Patch
@rrweb/all Patch
@rrweb/replay Patch
@rrweb/record Patch
@rrweb/types Patch
@rrweb/packer Patch
@rrweb/utils Patch
@rrweb/web-extension Patch
rrvideo Patch
@rrweb/rrweb-plugin-console-record Patch
@rrweb/rrweb-plugin-console-replay Patch
@rrweb/rrweb-plugin-sequential-id-record Patch
@rrweb/rrweb-plugin-sequential-id-replay Patch
@rrweb/rrweb-plugin-canvas-webrtc-record Patch
@rrweb/rrweb-plugin-canvas-webrtc-replay Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@d0ugal d0ugal marked this pull request as ready for review May 11, 2026 12:09
@eoghanmurray

Copy link
Copy Markdown
Contributor

That's super and yes I think .sheet is fully usable now (maybe it always was?)

I've made a similar change in the StyleSheet Assets branch which will become rrweb 2.1:
4206030

I think you might not need the fallback to searching the array?

Fix inline stylesheet capture on WebKit by reading CSS rules from `HTMLLinkElement.sheet` before falling back to `document.styleSheets`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants