Conversation
|
Size Change: -203 B (0%) Total Size: 6.84 MB
ℹ️ View Unchanged
|
Mamaduka
left a comment
There was a problem hiding this comment.
I'm not surprised. Most of the flaky tests or behaviors are caused by similar effects. We also have an old PR to change the perPage effect, but it's become stale - #66532.
I noticed that we often duplicate logic from the getEditedPostContent selector in the editor store.
I vaguely remember getEditedPostContent not being performant or something similar, though I don't think it changes much in this case.
| // Replicates the logic found in getEditedPostContent(). | ||
| const value = useMemo( () => { | ||
| if ( content instanceof Function ) { | ||
| return content( { blocks } ); | ||
| } else if ( blocks ) { | ||
| // If we have parsed blocks already, they should be our source of truth. | ||
| // Parsing applies block deprecations and legacy block conversions that | ||
| // unparsed content will not have. | ||
| return __unstableSerializeAndClean( blocks ); | ||
| } | ||
| return content; | ||
| }, [ content, blocks ] ); |
There was a problem hiding this comment.
I think this is code before unification.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
The internals of |
| const { getEditedEntityRecord } = useSelect( coreStore ); | ||
| const { getEditedPostContent } = useSelect( editorStore ); | ||
|
|
||
| function getText() { |
There was a problem hiding this comment.
Do we need this getText function anymore, or could we pass getEditedPostContent directly to useCopyToClipboard below?
As I was working on the React 19 migration in #61521, the
template-reverte2e tests started permanently failing. When comparingcontentBeforeandcontentAfter, there were trivial differences: different order of attributes, different whitespace, added default values for some attributes (queryblock'sperPage).If you look at the #73252 PR where this test was added, you'll see that the test was failing this way from day one:
At that time it was probably just flaky, but now it becomes permanent.
Where does the difference come from? The
getEditedPostContentlogic can calculate the content in three ways:post.contentis a string, return the string. That's the initial state before any edits.post.contentis a function, return the result of the function. That's how some kinds of editing update the content in an efficient (lazy-evaluated) way.post.blocks, serialize the structure and return the serialized string. That's how other kinds of editing work.Right after the
queryblock is rendered, it has a React effect that updates theperPageattribute:https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/query/edit/query-content.js#L96-L116
This code performs an edit without any user involvement, and switches the post content from the "initial string" mode to the "blocks" mode. And the JS serializer serializes the blocks differently from the original. Different order of attributes (based on
block.jsonorder). Different whitespace and newlines.During the
template-reverttest, the Query effect sometimes has time to run, sometimes it doesn't. That's random timing issue that causes the flakiness.My fix is to update the template source so that it matches the JS serializer syntax, and it provides the default
perPagevalue. Then the React effects and content serialization strategies don't matter any more. And the test is more reliable.The second commit fixes something related: I noticed that we often duplicate logic from the
getEditedPostContentselector in theeditorstore. I don't know why we ever did that, but it's quite easy to replace the copy/pasted logic with a single selector call.There is one place where I can't do it: in the "Time to Read" block, we need to the content of the currently edited post, but can't use the
core/editorstore. Maybe the "get content" logic should be at some more universal package, likeblock-editor, where it can be used by any block.