Make WordPress Core

Opened 7 months ago

Last modified 4 days ago

#52992 accepted enhancement

REST API: Avoid unnecessarily preparing item links

Reported by: dlh Owned by: rachelbaker
Milestone: 5.9 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests needs-testing
Focuses: Cc:


The bundled REST API controllers currently prepare links for each item in a REST response regardless of whether the links will appear in the response or are needed to fulfill an _embed request.

For example, when responding to the request from the block editor for available hierarchical terms:


WP_REST_Terms_Controller will call prepare_links() for all found terms even though the _links property is omitted from each, adding unnecessary processing that delays the response to the client.

Based on a discussion in the core Slack, the change proposed for this ticket would:

  1. Include _links by default in WP_REST_Controller::get_fields_for_response(), and include _embedded if the request object includes the _embed parameter. As discussed in Slack, these fields are included as a convention. The fields aren't typically included in the item schema, and because prepare_links() isn't a method defined on the base controller, it isn't possible for the controller to know before the response is prepared whether it will actually include links.
  1. Wrap the calls to prepare_links() and add_links() in each of the bundled controllers in checks for whether the _links field or _embedded field is included in the response.

Lastly, during the Slack discussion, Timothy asked:

Do you think we should bother checking for each individual link? We could theoretically do rest_is_field_included( '_links.self' ), etc...

I've opted to not do this in the current patch, simply to avoid the complexity of modifying the prepare_links() signatures across the various controllers to accept the requested fields or the request object, but I'm happy to do so if desired.

Change History (4)

This ticket was mentioned in PR #1179 on WordPress/wordpress-develop by dlh01.

7 months ago

  • Keywords has-unit-tests added

#2 @rachelbaker
7 months ago

  • Keywords needs-testing added
  • Milestone changed from Awaiting Review to 5.8
  • Owner set to rachelbaker
  • Status changed from new to accepted

Thanks for putting together the patch @dlh. I added needs testing to ensure this is more of a performance change versus a breaking API change.

#3 @desrosj
5 months ago

  • Milestone changed from 5.8 to 5.9

Today is 5.8 feature freeze. Unfortunately this one ran out of time.

Punting to 5.9 as there has been good recent momentum.

This ticket was mentioned in Slack in #core-restapi by spacedmonkey. View the logs.

4 days ago

Note: See TracTickets for help on using tickets.