-
Notifications
You must be signed in to change notification settings - Fork 144
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
chore: Replace useLayoutEffect during server rendering to reduce excessive warnings #79
Conversation
4223a17
to
0cded47
Compare
Codecov Report
@@ Coverage Diff @@
## main #79 +/- ##
==========================================
+ Coverage 92.29% 92.33% +0.04%
==========================================
Files 532 533 +1
Lines 15460 15466 +6
Branches 4257 4258 +1
==========================================
+ Hits 14269 14281 +12
+ Misses 1106 1100 -6
Partials 85 85
Continue to review full report at Codecov.
|
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 not quite convinced about this - it feels like we're hiding away what are real warnings if we ever want to properly support SSR. It's potentially even useful for customers to see the warnings, to know that the components they're using aren't fully SSR-compliant?
As you say - a good topic for team discussion...
@@ -160,6 +160,11 @@ | |||
{ | |||
"group": ["lodash", "lodash/*"], | |||
"message": "lodash is a commonjs module, which breaks webpack esm optimizations." | |||
}, | |||
{ | |||
"group": ["react"], |
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 guess this would still not stop people from using React.useLayoutEffect
? But hopefully that would get caught in PR as incorrect style anyway
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.
Yeah, ESLint isn't super comprehensive here. I could probably throw in a no-restricted-properties
for React.useLayoutEffect
, but it's just linting, there are always ways around it. But this should catch it 99% of the time.
Some additional context to this fix: Supporting SSR is different to removing This solution isn't new either; you can see this being used in Carbon, Material UI, Ant Design (via rc-util), Fluent UI, and so on. It is annoying that this hack has essentially been standardized in the React world, but that's the way it is. Of course, in an ideal world, we wouldn't be using |
I think what I particularly dislike about the solution is that it's non-transparently assigning a no-op function in SSR world (i.e. unless you know that |
Interesting, I hadn't thought of that. I want to stick with the "convention" of replacing effect types which everyone else uses just in case (if React starts considering hook ordering with server components or whatever), so that if React changes something in v19, they'll break everyone, not just us :P EDIT: I'll also add a comment clarifying why we need this in the source. |
Closing this PR for now following team discussion. We feel like we're hiding away potentially valid warnings and implying that our components are fully SSR-friendly, which they aren't. |
Description
Very low-hanging SSR fruit. SSRing or SSGing any components with a
useLayoutEffect
causes React to print out a massive warning per instance that crowds out the render logs, leading to less than ideal DX and making real issues harder to debug. This isn't going to be fixed on React's side, so the best place to fix this is on our side.A pretty common solution to this is to create a custom hook that runs useEffect on the server and useLayoutEffect on the client, which this PR introduces. Also had to upgrade ESLint because one of the config options wasn't supported until recently (
importNames
).I'm sure this'll inspire some healthy debate. We can discuss this as a team tomorrow :)
How has this been tested?
Added a unit test for the hook, as well as eslint checks to prevent regressions in this area.
Documentation changes
[Do the changes include any API documentation changes?]
Related Links
Review checklist
The following items are to be evaluated by the author(s) and the reviewer(s).
Correctness
CONTRIBUTING.md
.CONTRIBUTING.md
.Security
checkSafeUrl
function.Testing
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.