Skip to content

Loading a second emotion bundle shouldn't break the first #1945

@craigsketchley

Description

@craigsketchley

Current behavior:

I'm trying to run emotion 11 in a browser extension content script. The trouble is if that content script then runs in a page that is already running an emotion 11 app, then the app styles break. After a chat with @Andarist on the emotion-slack, he suggested I open an issue here and that this is probably the culprit:

const ssrStyles = document.querySelectorAll(`style[data-emotion]`)
// get SSRed styles out of the way of React's hydration
// document.head is a safe place to move them to
Array.prototype.forEach.call(ssrStyles, (node: HTMLStyleElement) => {
;((document.head: any): HTMLHeadElement).appendChild(node)
})

To reproduce:

  1. Build and install a browser extension that uses emotion 11 in a content script (I've made a test extension here: https://github.com/craigsketchley/emotion-11-extension).
  2. Visit a page that uses emotion 11 in production mode (I've made a test app here: https://github.com/craigsketchley/emotion-11-app).
  3. Watch styles break as soon as the content script loads.

Expected behavior:

Loading a second bundle which includes emotion 11 shouldn't break the styles of the existing emotion 11 app already running in the page.

Environment information:

In the app and extension:

  • react version: 16.13.1
  • @emotion/cache version: 11.0.0-next.13
  • @emotion/react version: 11.0.0-next.13
  • @emotion/styled version: 11.0.0-next.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions