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

Don't prepare the response body for HEAD requests #7970

Open
wants to merge 20 commits into
base: trunk
Choose a base branch
from

Conversation

anton-vlasenko
Copy link

Trac ticket: https://core.trac.wordpress.org/ticket/56481


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented Dec 7, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link

@ironprogrammer ironprogrammer left a comment

Choose a reason for hiding this comment

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

Thanks for putting this all together, @anton-vlasenko 🙌🏻 I've got a name tweak suggestion for some of the newly added methods to consider.

*
* @param string $method HTTP method to use.
*/
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {

Choose a reason for hiding this comment

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

Suggested change
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {
public function test_get_items_returns_only_ids_for_head_requests( $method ) {

For this and other same-named tests in the updated test classes under this PR, returns_only_fetches has two verbs, but should only use one for clarity.

Scanning other test classes, "return" is used more extensively than "fetch", so IMHO these new methods should use returns_only_ids. What do you think?

Copy link
Author

@anton-vlasenko anton-vlasenko Dec 9, 2024

Choose a reason for hiding this comment

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

Well spotted, @ironprogrammer! Having two verbs here doesn't make sense. This is likely the result of extensive copy-pasting between different test classes. :)

I'm leaning towards test_get_items_fetches_only_ids_for_head_requests, because what I meant is that the get_items() method fetches only the *id* column from the database for HEAD requests, as opposed to fetching all columns for GET requests.

Fixed in 9c91e45.

*
* @param string $method HTTP method to use.
*/
public function test_get_items_only_fetches_ids_for_head_requests( $method ) {

Choose a reason for hiding this comment

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

Suggested change
public function test_get_items_only_fetches_ids_for_head_requests( $method ) {
public function test_get_items_returns_only_ids_for_head_requests( $method ) {

Suggestion per previous note.

Copy link
Author

Choose a reason for hiding this comment

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

*
* @param string $method HTTP method to use.
*/
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {

Choose a reason for hiding this comment

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

Suggested change
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {
public function test_get_items_returns_only_ids_for_head_requests( $method ) {

Suggestion per above.

Copy link
Author

Choose a reason for hiding this comment

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

*
* @param string $method HTTP method to use.
*/
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {

Choose a reason for hiding this comment

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

Suggested change
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {
public function test_get_items_returns_only_ids_for_head_requests( $method ) {

Suggestion per above.

Copy link
Author

Choose a reason for hiding this comment

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

*
* @param string $method HTTP method to use.
*/
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {

Choose a reason for hiding this comment

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

Suggested change
public function test_get_items_returns_only_fetches_ids_for_head_requests( $method ) {
public function test_get_items_returns_only_ids_for_head_requests( $method ) {

Suggestion per above.

Copy link
Author

Choose a reason for hiding this comment

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

@Mamaduka
Copy link
Member

Fantastic work, @anton-vlasenko!

Let me know how I can help to land this in WP 6.8. I think it would improve editors' loading performance.

@anton-vlasenko
Copy link
Author

Fantastic work, @anton-vlasenko!

Thank you, @Mamaduka.

Let me know how I can help to land this in WP 6.8. I think it would improve editors' loading performance.

Thank you for offering your help!
A code review and/or testing report could help with landing this in WordPress 6.8.
This ticket is likely on @TimothyBJacobs's radar, so hopefully, he'll be able to approve and commit it when he has the time, provided there’s no additional feedback from the code review.

@WordPress WordPress deleted a comment from github-actions bot Dec 12, 2024
@desrosj desrosj added the props-bot Adding this label triggers the Props Bot workflow for a PR. label Dec 12, 2024
Copy link

github-actions bot commented Dec 12, 2024

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 props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @jonnynews.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props antonvlasenko, ironprogrammer, swissspidy, spacedmonkey, mamaduka, timothyblynjacobs.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot removed the props-bot Adding this label triggers the Props Bot workflow for a PR. label Dec 12, 2024
@TimothyBJacobs
Copy link
Member

Yeah I'm planning on landing this this weekend.

Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

I think we need to also disable any meta and term cache priming as well, as this results in database queries.

Also if there are changes to be made for taxononmy endpoint, those changes should also be made for post type endpoint. Otherwise this looks like great work.

$is_head_request = $request->is_method( 'HEAD' );
if ( $is_head_request ) {
// Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
$prepared_args['fields'] = 'ids';
Copy link
Member

Choose a reason for hiding this comment

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

Should we avoid priming comment meta here as well?

Copy link
Author

@anton-vlasenko anton-vlasenko Dec 14, 2024

Choose a reason for hiding this comment

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

Should we avoid priming comment meta here as well?

Yes, I think we should avoid priming comment meta here.
Fixed in 294584e

@anton-vlasenko anton-vlasenko force-pushed the add/short-circuit-head-requests branch from 8a73466 to c7eee61 Compare December 14, 2024 00:54
@anton-vlasenko anton-vlasenko force-pushed the add/short-circuit-head-requests branch from c7eee61 to 294584e Compare December 14, 2024 00:58
@anton-vlasenko anton-vlasenko force-pushed the add/short-circuit-head-requests branch from 9d1f623 to b80fd51 Compare December 14, 2024 01:51
@anton-vlasenko
Copy link
Author

anton-vlasenko commented Dec 14, 2024

I think we need to also disable any meta and term cache priming as well, as this results in database queries.

Also if there are changes to be made for taxononmy endpoint, those changes should also be made for post type endpoint. Otherwise this looks like great work.

Thank you for the review, @spacedmonkey.
Well spotted.
I've fixed these issues in 294584e and b80fd51.

@TimothyBJacobs
Copy link
Member

I think we have a BC issue with the single item route. Right now, if I use the rest_prepare_ filters, I can set headers on the returned response object. After these changes, this no longer happens.

I think we might want to move the is_method( 'HEAD' ) cehcks into the prepare_item_for_response methods, and prepare the empty response object there. That way, if someone adds a header, it is maintained. If they add a property, that's fine because it'll get omitted by the server.

@anton-vlasenko
Copy link
Author

anton-vlasenko commented Dec 17, 2024

I think we have a BC issue with the single item route. Right now, if I use the rest_prepare_ filters, I can set headers on the returned response object. After these changes, this no longer happens.

I think we might want to move the is_method( 'HEAD' ) cehcks into the prepare_item_for_response methods, and prepare the empty response object there. That way, if someone adds a header, it is maintained. If they add a property, that's fine because it'll get omitted by the server.

Thank you for the review, @TimothyBJacobs.
While I agree that this is a BC break, I tried searching for any plugins that add headers using the rest_prepare_ filters and couldn’t find any:

It could theoretically happen, but I’m not aware of any such cases.

That said, I also agree that GET and HEAD requests should behave similarly, with the exception that HEAD requests don’t include a response body.

I’m working on a fix.


Copy link

@jonnynews jonnynews left a comment

Choose a reason for hiding this comment

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

There are 42 files in the endpoints directory. Some of these are sub classes. But there are other endpoints that are not sub classes of the files edited in this PR. Is this applied all the endpoints that need this change?

@anton-vlasenko
Copy link
Author

anton-vlasenko commented Dec 17, 2024

There are 42 files in the endpoints directory. Some of these are sub classes. But there are other endpoints that are not sub classes of the files edited in this PR. Is this applied all the endpoints that need this change?

@jonnynews No, it hasn’t been applied to all the classes that need editing, as this PR focuses only on the most commonly used REST controllers.
Do you think the other controllers can be addressed in a follow-up PR, or would you prefer to fix everything in a single PR?

@jonnynews
Copy link

There are 42 files in the endpoints directory. Some of these are sub classes. But there are other endpoints that are not sub classes of the files edited in this PR. Is this applied all the endpoints that need this change?

@jonnynews No, it hasn’t been applied to all the classes that need editing, as this PR focuses only on the most commonly used REST controllers. Do you think the other controllers can be addressed in a follow-up PR, or would you prefer to fix everything in a single PR?

In core, we normally do these things one commit.

@jonnynews
Copy link

Some controllers might benefit from this work.

WP_REST_Block_Pattern_Categories_Controller
WP_REST_Block_Types_Controller
WP_REST_Font_Collections_Controller
WP_REST_Global_Styles_Revisions_Controller
WP_REST_Pattern_Directory_Controller
WP_REST_Search_Controller
WP_REST_Sidebars_Controller
WP_REST_Templates_Controller
WP_REST_Widget_Types_Controller
WP_REST_Widgets_Controller

To clear, as I am asking, I believe it just a simply copy and paste for most of these endpoints, but correct me if I am wrong.

@anton-vlasenko
Copy link
Author

anton-vlasenko commented Dec 17, 2024

Some controllers might benefit from this work.

WP_REST_Block_Pattern_Categories_Controller
WP_REST_Block_Types_Controller
WP_REST_Font_Collections_Controller
WP_REST_Global_Styles_Revisions_Controller
WP_REST_Pattern_Directory_Controller
WP_REST_Search_Controller
WP_REST_Sidebars_Controller
WP_REST_Templates_Controller
WP_REST_Widget_Types_Controller
WP_REST_Widgets_Controller

To clear, as I am asking, I believe it just a simply copy and paste for most of these endpoints, but correct me if I am wrong.

Yes, I believe it is. PHPUnit tests would need to be added as well (which can also be partially copy-pasted). I'll work on this tomorrow. Thank you for the review.

@anton-vlasenko anton-vlasenko force-pushed the add/short-circuit-head-requests branch 2 times, most recently from c1dc6e3 to c9c4d20 Compare December 20, 2024 17:45
@anton-vlasenko anton-vlasenko force-pushed the add/short-circuit-head-requests branch from c9c4d20 to 8bf96cf Compare December 20, 2024 17:51
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.

8 participants