Fix RichText rerendering when it shouldn't#12161
Conversation
The prepareEditableTree and onChangeEditableValue function stacks would have a new reference on every render. This prevents that by memoized the stack based on the previous stack and the newly added function.
| * | ||
| * @return {Object} The annotations props object. | ||
| */ | ||
| const getAnnotationObject = memize( ( annotations ) => { |
There was a problem hiding this comment.
Instead of memoizing function, I think it would be better for the registerFormatType function to do the memoization itself using a shallow comparison. This way it would work with any implementation of __experimentalGetPropsForEditableTreePreparation
That said, we're kind of running out of time, so I'm not considering this as blocking at the moment as it's a forward-compatible change.
youknowriad
left a comment
There was a problem hiding this comment.
I confirm that this does fix the Yoast incompatibility issue.
| import createSelector from 'rememo'; | ||
| import { get, flatMap } from 'lodash'; | ||
|
|
||
| const emptyArray = []; |
There was a problem hiding this comment.
Minor: I think in other places we called this EMPTY_ARRAY.
There was a problem hiding this comment.
Wouldn't be bad to have the same comment:
gutenberg/packages/editor/src/store/selectors.js
Lines 57 to 66 in 563ef41
There was a problem hiding this comment.
Minor: I think in other places we called this
EMPTY_ARRAY.
Related guideline: https://github.com/WordPress/gutenberg/blob/master/docs/contributors/coding-guidelines.md#constants
| dispatch( 'core/rich-text' ).addFormatTypes( settings ); | ||
|
|
||
| const emptyArray = []; | ||
| const getFunctionStackMemoized = memize( ( previousStack = emptyArray, newFunction ) => { |
There was a problem hiding this comment.
Would it work to keep the cache size to one?
There was a problem hiding this comment.
Also, why do both this and prepareEditableTree need to be memoized? Could use some more clarification.
There was a problem hiding this comment.
Probably not as we need one per RichText I think.
There was a problem hiding this comment.
I don't think so, because a different function could be returned based on richTextIdentifier/blockClientId.
There was a problem hiding this comment.
Just wondering because it's probably best to keep cache size as small as possible. I'm guessing the issue is just consecutive calls and nothing else.
There was a problem hiding this comment.
So how big is this cache going to grow?
There was a problem hiding this comment.
That's a good question though
|
|
||
| dispatch( 'core/rich-text' ).addFormatTypes( settings ); | ||
|
|
||
| const emptyArray = []; |
|
Just wondering, does this also fix #11595 (comment)? |
8ba55a9 to
2fc48e9
Compare
|
@iseulde In my testing it doesn't fix it completely |
|
That is what I concluded too. It's probably not far away. I will dive into that further to completely fix it. But I thought that doesn't have to be in 4.5.1. |
| /** | ||
| * External dependencies | ||
| */ | ||
| import memize from 'memize'; |
There was a problem hiding this comment.
I don't see this added as a dependency.
| "@wordpress/i18n": "file:../i18n", | ||
| "@wordpress/rich-text": "file:../rich-text", | ||
| "lodash": "^4.17.10", | ||
| "memize": "^1.0.5", |
There was a problem hiding this comment.
I think we also need it on the server side.
There was a problem hiding this comment.
No we don't, there's no script for memize
closes #12150
Description
The prepareEditableTree and onChangeEditableValue function stacks would have a new reference on every render. This prevents that by memoized the stack based on the previous stack and the newly added function.
How has this been tested?
Tested using Yoast SEO. I am working on e2e tests to test that RichText's doesn't rerender too often.
Screenshots
Types of changes
Checklist: