Multiple instances of emotion overwrite each other
See original GitHub issueCurrent behavior:
We’ve microservice architecture so each team can have their own emotion instance, in the same time they can even import other libraries that also use emotion. All apps getting loaded asynchronously. And when the next app is loaded, it creates own emotion instance that overwrites previous (at least it looks like so, but old <style /> tags still exist in DOM).
Yep, In dev
mode we’re getting the warn, but in production
build all sheets getting overwritten.
To reproduce: Main repo
npm i
to install pseudolibraries and depsnpm run start
to see the warnnrm run build
to see how one library overwrites another after 2 seconds (public
folder)
Expected behavior: Allow multiple instances of emotion
Environment information:
- Main repo with emotion@10*
- Libraries repos with emotion@11*
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:19 (7 by maintainers)
Top Results From Across the Web
Material-ui overrides react emotion rules - Stack Overflow
The issue was resolved by changing the order of rendering the style rules. I created a wrapper that needs to wrap the entire...
Read more >Best Practices - Emotion
There are two main approaches for sharing styles across an Emotion application. To illustrate the two methods, let's imagine we're developing an application ......
Read more >Style library interoperability - Material UI - MUI
In MUI, all child elements have an increased specificity of 2: .parent .child {} . When writing overrides, you need to do the...
Read more >Why We're Breaking Up with CSS-in-JS - DEV Community
On the other hand, if you're using CSS-in-JS, you can write your styles directly ... Multiple instances of Emotion get loaded at once....
Read more >How to ensure your Emotion CSS-in-JS styles are indeed ...
To make sure the Emotion styles are overwriting the CSS Module styles in ... 2. Now that we have our own Emotion instance...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In this situation, you really should use separate caches with unique keys - unfortunately
<style/>
elements are shared resources in the global document and we can’t easily distinguish which belong to each of those microfrontends.The problematic part though is that you are using hard dependencies for
@emotion/react
in each one and thus you load multiple instances of Emotion 11 so their context identities won’t be shared. I guess that because you are in this microfrontend architecture this is a hard requirement for you so I would advise you to just exportCacheProvider
+createCache
from each of your libraries~ and just wrap each React subtree in those CacheProviders.So in the end you would handle things like this:
Note that this doesn’t fix your problem with Emotion 11 hijacking Emotion 10 styles - we’ve ensured that we don’t try to hijack Emotion 10 SSR styles but we have missed the case with dynamically created Emotion 10 styles (or assumed that it will just work thanks to the same thing we’ve done for SSRed styles).
You can workaround this by doing this:
before you fetch a bundle containing Emotion 11 code.
We can’t do much for the first thing but we probably should try to handle the second problem and I will give this a thought.
Is there any update on this? I’m currently just doing a dev build until this issue is fixed. If it isnt soon I might just switch to styled components. Currently using @emotion/styled for a bunch of components and i’d prefer not to convert these to use the css prop