Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support renderToPipeableStream and eliminate the need for EmotionCache in React 19 #3283

Open
circlingthesun opened this issue Dec 6, 2024 · 1 comment

Comments

@circlingthesun
Copy link

The problem

To avoid putting style tags next to a component one has to use EmotionCache, extract the CSS, and follow a somewhat cumbersome server setup for SSR. This setup also does not appear to support renderToPipeableStream.

Proposed solution

In React 19 style tags can be hoisted to the head if you add an href and precedence to your style tags. Adding these attributes to style tags inserted by Emotion eliminates the need to use EmotionCache.

See: https://react.dev/reference/react-dom/components/style#special-rendering-behavior

I see no real drawbacks. On might need to detect the React version to avoid unnecessary attributes on older versions of React.

Alternative solutions

It seems a lot more complicated to support renderToPipeableStream with EmotionCache as can be seen by the issues that have been open for years.

Additional context

I've forked @emotion/styled into my project and simply replaced:

<style
  {...{
    [`data-emotion`]: `${cache.key} ${serializedNames}`,
    dangerouslySetInnerHTML: { __html: rules },
    nonce: cache.sheet.nonce,
  }}
/>

with

<style
  {...{
    [`data-emotion`]: `${cache.key} ${serializedNames}`,
    dangerouslySetInnerHTML: { __html: rules },
    nonce: cache.sheet.nonce,
    href: serializedNames,
    precedence: 'lower'
  }}
/>

and it worked flawlessly.

@Andarist
Copy link
Member

Andarist commented Dec 6, 2024

We'll be working on React 19 support this month.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants