-
-
Notifications
You must be signed in to change notification settings - Fork 400
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(useStyles): manageSheet must be in the same effect as unmanageSheet #1609
Conversation
The problem is that we are actually running those in the browser, so I can't make isInBrowser false without breaking it's semantics. What else can we do? |
I guess the tests which rely on being on the server simply should not run in the browser, we need to move them |
Alternative is that semantics should not rely on running in the browser or server, but on rendering using renderToString vs mounting to the dom. |
Technically the fact we run in the browser in this case is not that important, the react API we use is what's important, right? |
Exactly.
Sheet and dynamic rules management is now correct in terms of React API usage. Also with 21f87ae, both server and client renders will manage the sheet as expected. The only problem now is that all SSR tests are false negative, and to fix them - |
P.S. I am already running this patch in a prod env of mine, regressions from #1608 are gone and everything is working as expected. |
Yeah, but should we run tests without browser or should we add some kind of hint to the logic to let it know what API are we using, so that it doesn't rely on isInBrowser? |
Technically its a valid case to call renderToString in the browser. There are for sure use cases to do so. |
So probably we need to change the flag we rely on to a different semantics and make sure it has correct value depending on the react API being used? |
Sure, was just thinking through usual use cases.
If we go down that way, we'd need another flag, something like global.isSSR = true;
renderToString(<MyComponentWithJSS />); Googling "how to detect SSR", all the answers are |
Just researched a bit, indeed react doesn't have any good way to see a difference, they are relying themselves on the dom existence. In that case we should be basing the code that users use on a foundation that renderToString and alike is used on the server with no global document variable and if you really wanted to do that on the client, you would have to set some flag that would let you do that. Same flag can be used for tests. |
So yeah, some kind of isSSR option on JssProvider would do the trick |
That would work! The Furthermore, this would fix the test issues without too much refactoring - just |
Any news? What are the next steps? |
Next steps: lets add it in this PR? |
Huh? I don't understand the sentence. Are we proceeding with If so, I recon it would be much better as a separate PR, this one has nothing to do with SSRs. |
Merged #1610 |
@kof master is merged. This is now ready. |
great, thank you! |
Great stuff, thanks! Would be awesome if you could release the |
released in 10.9.1-alpha.1 |
#1608 fixed the failing tests caused by #1604, but doing so reintroduced the original bug breaking React 18 support...
manageSheet
must be in a separateuseEffect
because it can run more times thanuseMemo
- like during fast-refresh or running in strict mode. SincemanageSheet
is currently in theuseMemo
,unmanageSheet
fromuseEffect
gets called more times thanmanageSheet
, unmanaging the sheet while in use.All
renderToString
tests broke with #1604 becauseuseEffect
s don't run during SSR. The fix is included here; however, your tests are wrongly set up sinceisInBrowser
will returntrue
when usingrenderToString
.@kof I don't want to tingle with your tests setup, can you please make sure that
isInBrowser
isfalse
when testing withrenderToString
? Doing so will pass all the tests.