Query block: avoid setAttributes on mount for 'per page'#66532
Query block: avoid setAttributes on mount for 'per page'#66532
Conversation
|
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. |
|
Size Change: -65 B (0%) Total Size: 1.82 MB
ℹ️ View Unchanged
|
| return record.content; | ||
| return window.wp.blocks.__unstableSerializeAndClean( | ||
| window.wp.blocks.parse( record.content ) | ||
| ); |
There was a problem hiding this comment.
This necessary change actually shows that the PR is working! Previously, both the edited entities before and after the template change would have the blocks property. That's because on mount, the query block was editing attributes, and thus creating an "edit". This is no longer the case after the changes in this PR, so the initial entity has no blocks property. Instead, the test will use content, but there's some whitespace and attribute order differences. The solution is to parse this content and serialise it the same way as blocks a few lines above.
That seems fine to do in this case (inherit:true) since we don't have dynamic pagination blocks in the editor, but in this PR doesn't work as intended for custom ones. The reason for this is that custom Query Loop blocks need to have a Also in your PR you don't remove completely the effect in Query Loop, so I guess the Finally I'm curious if this change improves performance and how. Do you have any metrics? |
Good point. Yes a default should be provided there I guess, added it in bcaa6f4. But I didn't see
The edit can still happen, but for another reason that not generally occur, and yes, it should also be eliminated.
This improves performance by eliminating re-renders after the initial mount. Setting attributes in an effect should generally be avoided, it's not a good practice. |
I'm not sure if other consumers (3rd party blocks) rely on that info in some way though, and how to assess that.. |
| [ 'core/post-excerpt' ], | ||
| ]; | ||
|
|
||
| const INHERITED_QUERY_DEFAULT_PER_PAGE = 10; |
There was a problem hiding this comment.
Why do we need this const?
There was a problem hiding this comment.
As a fall back? It used to be the same as the custom query fallback. But the default per-page setting in a fresh WP install is 10, so I used that as the default. Inherited queries would rarely be as small as 3.
Agreed, let's use the attribute default value then. |
|
Can you rebase for the conflicts? |
62fb451 to
c482b0c
Compare
|
@ntsekouras Done :) |
|
Before the So, I still think:
|
|
@ntsekouras I don't get it. |
|
Flaky tests detected in dce564a. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11782192622
|
Exactly. Since before Query Loop would set that property, a variation or pattern could have left it out. Also you can insert a pattern from the inserter directly and you skip the placeholder stage.
Without the default value you have the problem we discussed above. But I still believe you also need the same default in php when we build the query, in case it's not set (my other comment). |
What?
We must avoid mount effects in blocks that set block attributes. In this particular case, it seems that the query block is setting the
perPagequery property on mount for inherited queries, which is in turn set as context to be consumed by the post template block (which actually does the querying.It seems to me that, for inherited queries, the query block should not at all concern itself with the
perPageproperty. It should be something that the post template block ignores and overrides for inherited queries. In fact, everything related to theperPagesetting is disabled elsewhere when the query is set to inherit.Why?
Separation of concerns, avoid passing setting around. Avoid triggering
setAttributeson mount. When block attributes change on mount, it will also cause excessive re-renders and degrade performance.How?
Retrieve the data at the exact point where it's needed.
Testing Instructions
Test the query block (inherited) together with the posts per page settings.
Testing Instructions for Keyboard
Screenshots or screencast