Make WordPress Core


Ignore:
Timestamp:
03/02/2025 10:05:08 PM (3 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Improve performance for HEAD requests.

By default, the REST API responds to HEAD rqeuests by calling the GET handler and omitting the body from the response. While convenient, this ends up performing needless work that slows down the API response time.

This commit adjusts the Core controllers to specifically handle HEAD requests by not preparing the response body.

Fixes #56481.
Props antonvlasenko, janusdev, ironprogrammer, swissspidy, spacedmonkey, mukesh27, mamaduka, timothyblynjacobs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r59461 r59899  
    270270     */
    271271    public function get_items( $request ) {
     272        if ( $request->is_method( 'HEAD' ) ) {
     273            // Return early as this handler doesn't add any response headers.
     274            return new WP_REST_Response();
     275        }
     276
    272277        $query = array();
    273278        if ( isset( $request['wp_id'] ) ) {
     
    669674     */
    670675    public function prepare_item_for_response( $item, $request ) {
     676        // Don't prepare the response body for HEAD requests.
     677        if ( $request->is_method( 'HEAD' ) ) {
     678            return new WP_REST_Response();
     679        }
     680
    671681        /*
    672682         * Resolve pattern blocks so they don't need to be resolved client-side
Note: See TracChangeset for help on using the changeset viewer.