fix(rrweb): queue insertRule events dropped due to mirror race condition#141
Draft
TueHaulund wants to merge 1 commit intomainfrom
Draft
fix(rrweb): queue insertRule events dropped due to mirror race condition#141TueHaulund wants to merge 1 commit intomainfrom
TueHaulund wants to merge 1 commit intomainfrom
Conversation
CSS-in-JS libraries like Emotion create <style> elements and synchronously call insertRule() before the MutationObserver adds the node to rrweb's mirror. This causes getIdAndStyleId() to return -1 and the events to be silently dropped, resulting in unstyled session replays. Queue dropped insertRule calls for DOM-connected ownerNodes and retry via setTimeout(0) after the MutationObserver has processed the node. Deduplicate against _cssText to avoid emitting events already captured by the mutation serialization. Fixes rrweb-io/rrweb#1230
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CSS-in-JS libraries like Emotion (used by Chakra UI v3/v4) create
<style>elements and synchronously callCSSStyleSheet.insertRule()before theMutationObserveradds the node to rrweb's mirror. This causesgetIdAndStyleId()to return-1and the style rule events to be silently dropped, resulting in unstyled session replays.This is a long-standing issue (rrweb-io/rrweb#1230, rrweb-io/rrweb#928) with no upstream fix.
How it works
insertRuleis called on a sheet whoseownerNodeis connected to the DOM but not yet in the mirror, queue the{rule, index}instead of silently dropping it.setTimeout(0): Schedule a single batched retry that fires after the MutationObserver microtask and any remaining synchronous insertRule calls._cssText: On retry, checkmirror.getMeta(ownerNode)for_cssText. If present, the MutationObserver already serialized the full sheet state — skip the queued events. If absent, emit them as a batchedStyleSheetRulecallback.In the common Emotion case,
_cssTextalways captures all the initial rules, so the retry is effectively a no-op (no duplicate events). The retry acts as a safety net for edge cases where_cssTextisn't set (e.g. CORS restrictions on the stylesheet).Test plan
pnpm turbo run check-types --filter=@posthog/rrweb)