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-search-controller.php

    r57839 r59899  
    143143        $ids = $result[ WP_REST_Search_Handler::RESULT_IDS ];
    144144
    145         $results = array();
    146 
    147         foreach ( $ids as $id ) {
    148             $data      = $this->prepare_item_for_response( $id, $request );
    149             $results[] = $this->prepare_response_for_collection( $data );
     145        $is_head_request = $request->is_method( 'HEAD' );
     146        if ( ! $is_head_request ) {
     147            $results = array();
     148
     149            foreach ( $ids as $id ) {
     150                $data      = $this->prepare_item_for_response( $id, $request );
     151                $results[] = $this->prepare_response_for_collection( $data );
     152            }
    150153        }
    151154
     
    163166        }
    164167
    165         $response = rest_ensure_response( $results );
     168        $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $results );
    166169        $response->header( 'X-WP-Total', $total );
    167170        $response->header( 'X-WP-TotalPages', $max_pages );
Note: See TracChangeset for help on using the changeset viewer.