-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Performance: Avoid calculating html block styles in useSelect #42026
Conversation
Size Change: +45 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
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 works like a charm. Thanks, @talldan!
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { SandBox } from '@wordpress/components'; | ||
|
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.
Nitpick: Extra space 👾
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.
Oh no, and I finally got the tests passing. 😄
Thanks for the review.
Nice catch! |
What?
Fixes #41618
That issue notes that when the HTML block is present in the editor, it continually performs expensive style calculations, even when it isn't the selected block.
Why?
The issue is that the expensive function call is being made in
useSelect
, which is being triggered on every render.How?
In this PR I've changed the code so that
useSelect
is used only to get styles from the editor settings. Computation is only performed with auseMemo
hook, which will only re-trigger if the styles change.The styles are also only used for the preview, so I've moved that into a separate component, and now styles will also only be calculated when the preview is active.
Testing Instructions
Testing that there are no regressions requires using dev tools to inspect the block
trunk
version of Gutenberg open an editorcomponents-sandbox
iframe. Expand thehead
element of that iframe and take a look at the stylesheetsTo test the performance improvement, the simplest way is to use a logpoint or similar while an html block is present in a post and then carry out normal editing tasks.