Skip to content

[@emotion/sheet] Prevent stale before element references#3364

Open
tkajtoch wants to merge 2 commits intoemotion-js:mainfrom
tkajtoch:fix/sheet-verify-before-element
Open

[@emotion/sheet] Prevent stale before element references#3364
tkajtoch wants to merge 2 commits intoemotion-js:mainfrom
tkajtoch:fix/sheet-verify-before-element

Conversation

@tkajtoch
Copy link
Copy Markdown

@tkajtoch tkajtoch commented Mar 4, 2026

What:

This PR fixes a bug in @emotion/sheet that causes a NotFoundError to be thrown when a stale (removed, moved, disconnected, or otherwise not a child of the container) element is set in the before property.

This can happen internally and externally. We noticed it breaking when multiple @emotion/react's <Global /> components are being reinserted into the same container, but it can be caused by anything that controls the before property from the outside (which is technically how that property is supposed to be used).

Why:

To prevent @emotion/sheet from crashing user applications.

How:

A stale element reference can happen in many ways. Since before is externally controllable, we have to check whether it references an expected element and is up to date. I do it by confirming it's a child of the container element, which should be good enough to prevent this issue.

All other conditions within _insertTag seem to reference the actual state of the DOM, which is safe considering its synchronous nature.

Checklist:

  • Documentation - N/A
  • Tests
  • Code complete
  • Changeset

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 87aa1ef

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

This PR includes changesets to release 1 package
Name Type
@emotion/sheet 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

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci bot commented Mar 4, 2026

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@tkajtoch
Copy link
Copy Markdown
Author

tkajtoch commented Mar 4, 2026

dtslint CI is failing due to TypeScript 6.0 deprecation errors unrelated to changes made in this PR.

@tkajtoch
Copy link
Copy Markdown
Author

tkajtoch commented Mar 4, 2026

@Andarist would you be able to take a look at this or assign another maintainer please?

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