Skip to content
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(react-tooltip): use useIsomorphicLayoutEffect to avoid SSR warnings #17894

Merged
merged 17 commits into from Apr 22, 2021

Conversation

layershifter
Copy link
Member

@layershifter layershifter commented Apr 21, 2021

Pull request checklist

Description of changes

This PR replaces React.useLayoutEffect with useIsomorphicLayoutEffect as it produces warnings during SSR.
Also modifies ESLint preset to prevent future usages. Warnings in existing places of v8 code have been suppressed.

See more details: https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a

@size-auditor
Copy link

size-auditor bot commented Apr 21, 2021

Asset size changes

Project Bundle Baseline Size New Size Difference
office-ui-fabric-react fluentui-react-components-FluentProvider 133.714 kB 133.698 kB BelowBaseline     -16 bytes

ExceedsTolerance Over Tolerance (1024 B) ExceedsBaseline Over Baseline BelowBaseline Below Baseline New New Deleted  Removed 1 kB = 1000 B

Baseline commit: 88a432afc115f1750a3bc1afa67a3d90960db903 (build)

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 21, 2021

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 bfb7f8b:

Sandbox Source
Fluent UI Button Configuration
codesandbox-react-template Configuration
codesandbox-react-northstar-template Configuration

@fabricteam
Copy link
Collaborator

fabricteam commented Apr 21, 2021

Perf Analysis

No significant results to display.

All results

Scenario Render type Master Ticks PR Ticks Iterations Status
Avatar mount 928 918 5000
BaseButton mount 898 900 5000
Breadcrumb mount 43434 43388 5000
ButtonNext mount 538 539 5000
Checkbox mount 1521 1538 5000
CheckboxBase mount 1294 1266 5000
ChoiceGroup mount 4691 4684 5000
ComboBox mount 944 975 1000
CommandBar mount 10190 10134 1000
ContextualMenu mount 6151 6167 1000
DefaultButton mount 1117 1101 5000
DetailsRow mount 3573 3650 5000
DetailsRowFast mount 3653 3655 5000
DetailsRowNoStyles mount 3456 3435 5000
Dialog mount 1407 1409 1000
DocumentCardTitle mount 1811 1836 1000
Dropdown mount 3164 3186 5000
FocusTrapZone mount 1776 1775 5000
FocusZone mount 1734 1836 5000
IconButton mount 1774 1716 5000
Label mount 341 347 5000
Layer mount 1814 1769 5000
Link mount 459 467 5000
MakeStyles mount 1782 1803 50000
MenuButton mount 1433 1434 5000
MessageBar mount 2020 2003 5000
Nav mount 3232 3267 1000
OverflowSet mount 1037 1026 5000
Panel mount 1385 1415 1000
Persona mount 809 814 1000
Pivot mount 1440 1422 1000
PrimaryButton mount 1263 1265 5000
Rating mount 7472 7487 5000
SearchBox mount 1298 1249 5000
Shimmer mount 2507 2479 5000
Slider mount 1960 1967 5000
SpinButton mount 4934 4935 5000
Spinner mount 423 414 5000
SplitButton mount 3172 3126 5000
Stack mount 494 497 5000
StackWithIntrinsicChildren mount 1528 1527 5000
StackWithTextChildren mount 4508 4499 5000
SwatchColorPicker mount 10122 10122 5000
Tabs mount 1418 1399 1000
TagPicker mount 2404 2404 5000
TeachingBubble mount 11822 11787 5000
Text mount 418 414 5000
TextField mount 1377 1355 5000
ThemeProvider mount 1165 1181 5000
ThemeProvider virtual-rerender 609 607 5000
ThemeProviderNext mount 9169 9240 5000
Toggle mount 797 826 5000
buttonNative mount 117 118 5000

Perf Analysis (Fluent)

Perf comparison
Status Scenario Fluent TPI Fabric TPI Ratio Iterations Ticks
🦄 Avatar.Fluent 0.17 0.47 0.36:1 2000 340
🦄 Button.Fluent 0.11 0.2 0.55:1 5000 549
🔧 Checkbox.Fluent 0.63 0.35 1.8:1 1000 627
🎯 Dialog.Fluent 0.15 0.21 0.71:1 5000 769
🔧 Dropdown.Fluent 3.09 0.42 7.36:1 1000 3094
🔧 Icon.Fluent 0.12 0.06 2:1 5000 621
🦄 Image.Fluent 0.08 0.13 0.62:1 5000 377
🔧 Slider.Fluent 1.58 0.45 3.51:1 1000 1581
🔧 Text.Fluent 0.07 0.03 2.33:1 5000 373
🦄 Tooltip.Fluent 0.14 0.9 0.16:1 5000 705

🔧 Needs work     🎯 On target     🦄 Amazing

Perf tests with no regressions
Scenario Current PR Ticks Baseline Ticks Ratio
LayoutMinimalPerf.default 394 357 1.1:1
RefMinimalPerf.default 266 247 1.08:1
SegmentMinimalPerf.default 369 342 1.08:1
AvatarMinimalPerf.default 217 202 1.07:1
HeaderMinimalPerf.default 392 365 1.07:1
LabelMinimalPerf.default 416 388 1.07:1
ButtonSlotsPerf.default 574 543 1.06:1
GridMinimalPerf.default 361 344 1.05:1
ImageMinimalPerf.default 394 375 1.05:1
ListWith60ListItems.default 669 638 1.05:1
TreeMinimalPerf.default 809 774 1.05:1
AccordionMinimalPerf.default 165 158 1.04:1
ReactionMinimalPerf.default 395 381 1.04:1
TableManyItemsPerf.default 1937 1885 1.03:1
Dialog.Fluent 769 745 1.03:1
CardMinimalPerf.default 586 577 1.02:1
ChatDuplicateMessagesPerf.default 301 296 1.02:1
DropdownMinimalPerf.default 3136 3070 1.02:1
SplitButtonMinimalPerf.default 3764 3696 1.02:1
Button.Fluent 549 540 1.02:1
Text.Fluent 373 366 1.02:1
Tooltip.Fluent 705 694 1.02:1
BoxMinimalPerf.default 351 348 1.01:1
ButtonMinimalPerf.default 182 180 1.01:1
ButtonOverridesMissPerf.default 1701 1679 1.01:1
ButtonUseCssPerf.default 807 799 1.01:1
ButtonUseCssNestingPerf.default 1072 1065 1.01:1
EmbedMinimalPerf.default 4161 4128 1.01:1
InputMinimalPerf.default 1265 1253 1.01:1
ItemLayoutMinimalPerf.default 1265 1247 1.01:1
IconMinimalPerf.default 621 616 1.01:1
TextMinimalPerf.default 361 358 1.01:1
Dropdown.Fluent 3094 3061 1.01:1
AttachmentSlotsPerf.default 1150 1152 1:1
ChatMinimalPerf.default 608 610 1:1
CheckboxMinimalPerf.default 2767 2776 1:1
DialogMinimalPerf.default 737 740 1:1
FlexMinimalPerf.default 298 298 1:1
ListNestedPerf.default 559 557 1:1
LoaderMinimalPerf.default 705 706 1:1
MenuButtonMinimalPerf.default 1563 1566 1:1
PopupMinimalPerf.default 726 723 1:1
SliderMinimalPerf.default 1564 1561 1:1
TableMinimalPerf.default 418 419 1:1
TooltipMinimalPerf.default 955 953 1:1
TreeWith60ListItems.default 185 185 1:1
VideoMinimalPerf.default 640 640 1:1
AlertMinimalPerf.default 275 278 0.99:1
AnimationMinimalPerf.default 416 419 0.99:1
AttachmentMinimalPerf.default 162 163 0.99:1
ChatWithPopoverPerf.default 386 391 0.99:1
DatepickerMinimalPerf.default 45192 45447 0.99:1
TextAreaMinimalPerf.default 487 494 0.99:1
CustomToolbarPrototype.default 3769 3798 0.99:1
ToolbarMinimalPerf.default 934 940 0.99:1
Checkbox.Fluent 627 636 0.99:1
Image.Fluent 377 381 0.99:1
Slider.Fluent 1581 1598 0.99:1
DropdownManyItemsPerf.default 670 686 0.98:1
ProviderMergeThemesPerf.default 1670 1712 0.98:1
RadioGroupMinimalPerf.default 440 448 0.98:1
SkeletonMinimalPerf.default 364 370 0.98:1
DividerMinimalPerf.default 366 376 0.97:1
ListCommonPerf.default 627 644 0.97:1
ListMinimalPerf.default 499 515 0.97:1
MenuMinimalPerf.default 882 906 0.97:1
StatusMinimalPerf.default 686 708 0.97:1
Avatar.Fluent 340 351 0.97:1
CarouselMinimalPerf.default 463 480 0.96:1
HeaderSlotsPerf.default 755 784 0.96:1
RosterPerf.default 1154 1197 0.96:1
ProviderMinimalPerf.default 985 1022 0.96:1
Icon.Fluent 621 669 0.93:1
FormMinimalPerf.default 394 426 0.92:1
PortalMinimalPerf.default 171 190 0.9:1

@@ -12,6 +12,7 @@ import * as React from 'react';
export const useMountSync = (callback: () => void) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also mark this as @deprecated? It's not used anywhere and is fundamentally problematic due to useLayoutEffect usage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, marked it by JSDoc & in README.md 👍

@@ -75,6 +75,7 @@ export const useOverflow = ({ onOverflowItemsChanged, rtl, pinnedIndex }: Overfl
return () => containerRef(null);
});

// eslint-disable-next-line no-restricted-properties
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behowell FYI, might be good to look into whether this could safely be updated to use useIsomorphicLayoutEffect, or how to handle SSR properly if that's not possible. (no need for changes in this PR though)

Copy link
Contributor

@behowell behowell Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar enough with SSR to know offhand. Basically this needs to happen at some point--it's ok if it's delayed on the first render after SSR, but it would be bad if it never happened. Is that what useIsomorphicLayoutEffect achieves?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useIsomorphicLayoutEffect does useLayoutEffect normally, and useEffect if server-rendered. For most things that's okay, but depending on what it's being used for, could cause issues like flashing on first render (or maybe bugs in a few cases that are particularly dependent on the timing).

I'm not sure what a good way is to validate SSR. I'm not sure how to manually test it (would have to look that up). For v8 we have an ssr-tests package, but it's limited to extremely basic verification that the components render.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things that are related to DOM calculations/operations probably should never executed on server at all as there is no DOM 🙄

It depends on use case, but sometimes we can simply exclude useLayoutEffect based on environment, for example, it's how it's done in Emotion:
https://github.com/emotion-js/emotion/blob/6c4a9e50f177900f54f7b3368a55b1a7d5e3c002/packages/react/src/global.js#L83-L86

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-components@v9.0.0-alpha.34 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-date-time@v8.0.29 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-divider@v9.0.0-alpha.16 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-compose@v1.0.0-beta.23 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-checkbox@v1.0.0-beta.57 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-button@v9.0.0-alpha.27 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-charting@v5.0.33 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-cards@v1.0.0-beta.87 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-avatar@v9.0.0-alpha.27 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-badge@v9.0.0-alpha.29 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/public-docsite-resources@v8.0.37 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/public-docsite@v8.1.15 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/example-data@v8.1.3 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/jest-serializer-make-styles@v9.0.0-alpha.3 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/eslint-plugin@v1.1.1 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/foundation-legacy@v8.0.5 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/date-time-utilities@v8.0.3 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/font-icons-mdl2@v8.0.5 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/dom-utilities@v2.0.3 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/codemods@v8.0.4 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/common-styles@v1.0.5 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/api-docs@v8.0.29 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/azure-themes@v8.0.31 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-menu@v9.0.0-alpha.12 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-tabster@v9.0.0-alpha.19 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-provider@v9.0.0-alpha.26 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-accordion@v9.0.0-alpha.19 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-image@v9.0.0-alpha.27 has been released which incorporates this pull request.:tada:

Handy links:

@msft-fluent-ui-bot
Copy link
Collaborator

🎉@fluentui/react-examples@v8.19.0 has been released which incorporates this pull request.:tada:

Handy links:

miroslavstastny pushed a commit to miroslavstastny/fluentui that referenced this pull request May 5, 2021
…gs (microsoft#17894)

* fix(react-tooltip): use useIsomorphicLayoutEffect to avoid SSR warnings

* Change files

* fix line length

* disable lint for valid usages

* Change files

* disable lint for valid usages

* Change files

* disable lint for valid usages

* Change files

* disable lint for valid usages

* Change files

* disable lint for valid usages

* Change files

* mark useMountSync() as deprecated

* ignore lint in test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@fluentui/react-tooltip: warning about useLayoutEffect in SSR
7 participants