-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix moving of client-side inserted style tags from Emotion 10 when intending to hydrate Emotion 11 styles resulting in losing styles #2361
Conversation
🦋 Changeset detectedLatest commit: b80e1c6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
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. Latest deployment of this branch, based on commit b80e1c6:
|
Codecov Report
|
I've updated this so it fixes the issue. The issue is very specific to v10 and v11 on the same page, multiple Emotion 11 caches already don't suffer from this problem. (ofc they will suffer from other issues but nothing as problematic as destroying all previously inserted client-side inserted styles from Emotion 10)
This wouldn't have solved the issue completely because it would still have the issue if someone used Emotion 10 with a custom container that was the body element or inside the body.
That code very intentionally targets all Emotion 11 server rendered style elements(and unintentionally before this change, it targeted client-side inserted Emotion 10 styles which caused the problem you were having) because they have to be moved before the React hydration starts. If we did it on a cache-specific basis and you created a cache inside the render of a React component, the style elements would be moved too late and React would warn and break things, e.g. https://codesandbox.io/s/blissful-fog-r73qb. |
@@ -35,3 +35,145 @@ test('rehydrated styles to head can be flushed', () => { | |||
cache.sheet.flush() | |||
expect(document.documentElement).toMatchSnapshot() | |||
}) | |||
|
|||
test('flushing rehydrated styles in the head only affect styles matching the cache key', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering - was this supposed to test a fix or is it just a new test for something that was not tested up until now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey 👋 yeah it was a bit of both, without the fix for filtering the cache key (data-emotion="emo"
), both style tags would be removed. AFAIK that's why we were having issues using the CacheProvider.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test doesn't test any changed behavior, the styles in this test are both from server-rendering which was already handled correctly.
|
||
createCache({ key: 'css' }) | ||
|
||
expect(document.documentElement).toMatchInlineSnapshot(` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
based on this assertion it's rather hard to see if this asserts the correct thing - maybe it would be better to render those Emotion 10 styles to the body? that way we'd know that they have not been moved to the head
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea this test is a bit of a weird one tbh. It's intended to assert the cause of our original issue which is essentially style tags on the head being re-mounted to the head when a cache provider instantiates itself, resulting in CSSOM styles being lost.
Since we're not able to test CSSOM styles in jest, I feel like it's probably a good idea just to remove it instead.
What do you think? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only test that tests behavior that has changed, I've updated it so it's more clear
Do you have any particular problems in mind? We probably should document or fix potential problems. I think the possible permutations of this problem are:
We can't deal with clashing Have I missed something? |
This is fantastic! Thanks so much for jumping in @mitchellhamilton @Andarist ❤️. |
What I meant was using the same key is always going to have some problems and they are:
For people who don't care about these things(which is the vast majority of people imo), multiple caches with the same key should work fine. I'm not saying people should intentionally use the same cache key but they will probably unintentionally use multiple caches with the same key because different versions of emotion will be run in the same app.
What's the problem you're referencing here that people have worked around(as in specifically this row)? |
9ee0296
to
c2b499d
Compare
c2b499d
to
d3ef1d2
Compare
Hey folks, just pushed up some changes to address your feedback. LMK if there's anything else you'd like me to look at 👍 |
Are there any other reasons why 'css' was used as the default key and not some kind of unique ID generated at runtime? |
We need keys to match from a server-render to a client-render. |
…e/emotion into emotion-cache-issue
I'm not aware of any problems there - I've just skipped thinking about those entirely but included them in the table for completeness of the listed combinations 😅 |
Any timeline when this will be released? |
Closes: #2210
Related: #2209
What:
@emotion/cache
currently modifies style tags 'belonging' to othercacheProviders
.@emotion/cache
also currently searches for style tags in both the head and body and remounts them to the head.Why:
How:
Checklist:
Reproduction
https://github.com/seancurtis/emotion-concurrency
Screen.Recording.2021-04-28.at.4.07.29.pm.mov
Emotion11.mp4