Skip to content

[Bug]: Replayer drops adopted stylesheets on seek when their first host was removed #342

Closed

Description

@patjakubik

Preflight Checklist

  • I have searched the issue tracker for a bug report that matches the one I want to file, without success.

What package is this bug report for?

rrweb (the Replayer)

Version

@sentry-internal/rrweb 2.43.0 (Sentry's player); recorded with @sentry/browser 11.0.0. Also reproduces with upstream rrweb 2.2.0.

Expected Behavior

Seeking (pause(t) / play(t)) leaves every shadow root with the same adopted stylesheets as playing straight through to t.

Actual Behavior

With the default useVirtualDom: true, seeking leaves shadow roots without their adopted stylesheets whenever those sheets were first recorded with a host that's no longer in the DOM at the seek target. Playing straight through, or seeking with useVirtualDom: false, works.

The recorder emits a constructed stylesheet's rules only once, with the first AdoptedStyleSheet event that uses it; later hosts adopting the same sheet send just its styleId. While seeking with the virtual DOM, the Replayer queues AdoptedStyleSheet events and applies them after the diff. By then, a host removed during the seek (for example after a client-side navigation) isn't in the mirror, and applyAdoptedStyleSheet returns before it creates that event's sheets:

private applyAdoptedStyleSheet(data: adoptedStyleSheetData) {
  const targetHost = this.mirror.getNode(data.id);
  if (!targetHost) return; // the sheets in data.styles are never created

Every later event that refers to those styleIds finds nothing in styleMirror, retries 10 times and gives up, and that shadow root ends up unstyled.

We hit this with Shopify Polaris web components, where every component shares sheets such as a common :host sheet. Sentry session replays of an embedded Shopify admin app look right when played from the start, but after a seek most components are unstyled.

Steps to Reproduce

  1. Save this as an HTML file and serve it from any static server:
<!doctype html>
<html><head><meta charset="utf-8"></head><body>
<div id="app"></div>
<div id="player"></div>
<script type="module">
import { record, Replayer } from "https://cdn.jsdelivr.net/npm/@sentry-internal/[email protected]/+esm"

// One constructed stylesheet shared by every <x-card>, the way web component
// libraries such as Shopify Polaris style their shadow roots.
const sheet = new CSSStyleSheet()
sheet.replaceSync(":host { display: block; padding: 16px; background: lightblue; }")
customElements.define("x-card", class extends HTMLElement {
  constructor() {
    super()
    this.attachShadow({ mode: "open" }).adoptedStyleSheets = [sheet]
    this.shadowRoot.innerHTML = "<slot></slot>"
  }
})

const events = []
const stop = record({ emit: (e) => events.push(e) })
const app = document.getElementById("app")
const wait = (ms) => new Promise((r) => setTimeout(r, ms))

await wait(100)
app.innerHTML = "<x-card>Page A</x-card>" // the sheet's rules are emitted with this host
await wait(100)
app.innerHTML = ""                        // host removed (client-side navigation)
await wait(100)
app.innerHTML = "<x-card>Page B</x-card>" // adopts the same sheet: styleIds only, no rules
await wait(100)
stop()

const end = events.at(-1).timestamp - events[0].timestamp
const results = {}
for (const [name, config] of [["virtual DOM (default)", {}], ["useVirtualDom: false", { useVirtualDom: false }]]) {
  const root = document.createElement("div")
  document.getElementById("player").append(root)
  const replayer = new Replayer(events, { root, ...config })
  replayer.pause(end + 1) // seek past the last event
  await wait(1500)        // past the adopt retry loop
  results[name] =
    replayer.iframe.contentDocument.querySelector("x-card").shadowRoot.adoptedStyleSheets.length
}
console.log("Page B <x-card> adopted sheets after seeking (expected 1):", JSON.stringify(results))
</script>
</body></html>
  1. Open it and look at the console:
Page B <x-card> adopted sheets after seeking (expected 1): {"virtual DOM (default)":0,"useVirtualDom: false":1}

To try upstream rrweb, swap the import for https://cdn.jsdelivr.net/npm/[email protected]/+esm; the result is the same.

Testcase Gist URL

None: the page above records and replays its own events.

Additional Information

Suggested fix: create the sheets even when the host is gone, and skip only the adopting:

 private applyAdoptedStyleSheet(data: adoptedStyleSheetData) {
   const targetHost = this.mirror.getNode(data.id);
-  if (!targetHost) return;
   // Create StyleSheet objects which will be adopted after.
   data.styles?.forEach((style) => {
     let newStyleSheet: CSSStyleSheet | null = null;
     let hostWindow: IWindow | null = null;
-    if (hasShadowRoot(targetHost))
+    if (!targetHost)
+      // The host is gone (e.g. removed during a virtual-DOM seek), but later
+      // events can still reference these styleIds, so create them anyway.
+      hostWindow = this.iframe.contentWindow;
+    else if (hasShadowRoot(targetHost))
       hostWindow = targetHost.ownerDocument?.defaultView || null;
     else if (targetHost.nodeName === '#document')
       hostWindow = (targetHost as Document).defaultView;
     ...
   });
+  if (!targetHost) return;

We tested an equivalent patch (applied by overriding Replayer.prototype.applyAdoptedStyleSheet) against a real Polaris recording. After seeking, every shadow root had all its sheets again, matching straight-through playback.

One caveat: a constructed sheet can only be adopted within the document that created it, so a sheet created this way for a host inside a nested iframe would belong to the wrong document. A sturdier fix would keep each styleId's rules and create the sheet on first adoption, in the adopting host's window.

Environment: Chrome 155, macOS.

Investigated and written up with help from Claude Code; I've run the reproduction above and checked the results myself.

Activity

  1. linear-code commented on Oct 25, 2026

    @linear-code
  2. logaretm commented on Nov 5, 2026

    @logaretm
    Member

    Thanks for reporting this, will take a look at it.

  3. self-assigned this
    on Nov 5, 2026
  4. added a commit that references this issue on Nov 9, 2026
    9fbebef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions