Skip to content

fix: don't throw when removing a style tag that was already detached - #863

Open
vpontis wants to merge 1 commit into
vercel:mainfrom
vpontis:fix-remove-detached-style-tag
Open

vpontis wants to merge 1 commit into
vercel:mainfrom
vpontis:fix-remove-detached-style-tag

Conversation

@vpontis

@vpontis vpontis commented Oct 17, 2026

Copy link
Copy Markdown

When a component unmounts, styled-jsx removes its <style> tag with tag.parentNode.removeChild(tag). If something else already took the tag out of the DOM (browser extensions, scrapers and screenshot tools that strip <style> tags), parentNode is null and this throws TypeError: Cannot read properties of null (reading 'removeChild'). The throw happens during React's commit phase, so the whole tree falls back to the nearest error boundary.

We see a few thousand of these a week in production, mostly from headless Chrome and some from real Chrome users with extensions.

This skips removal when the tag is already detached, in StyleSheet.deleteRule, StyleSheet.flush and StyleSheetRegistry.remove (server-rendered tags). The bookkeeping (_tags[index] = null, delete this._fromServer[styleId]) still runs, so the sheet stays consistent.

Added a test that detaches the tags before deleteRule and flush.

@vpontis
vpontis requested a review from ijjk as a code owner October 17, 2026 15:11
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.

1 participant