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

    r59882 r59899  
    263263        }
    264264
     265        $is_head_request = $request->is_method( 'HEAD' );
     266        if ( $is_head_request ) {
     267            // Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
     268            $prepared_args['fields'] = 'ids';
     269            // Disable priming comment meta for HEAD requests to improve performance.
     270            $prepared_args['update_comment_meta_cache'] = false;
     271        }
     272
    265273        /**
    266274         * Filters WP_Comment_Query arguments when querying comments via the REST API.
     
    278286        $query_result = $query->query( $prepared_args );
    279287
    280         $comments = array();
    281 
    282         foreach ( $query_result as $comment ) {
    283             if ( ! $this->check_read_permission( $comment, $request ) ) {
    284                 continue;
    285             }
    286 
    287             $data       = $this->prepare_item_for_response( $comment, $request );
    288             $comments[] = $this->prepare_response_for_collection( $data );
     288        if ( ! $is_head_request ) {
     289            $comments = array();
     290
     291            foreach ( $query_result as $comment ) {
     292                if ( ! $this->check_read_permission( $comment, $request ) ) {
     293                    continue;
     294                }
     295
     296                $data       = $this->prepare_item_for_response( $comment, $request );
     297                $comments[] = $this->prepare_response_for_collection( $data );
     298            }
    289299        }
    290300
     
    304314        }
    305315
    306         $response = rest_ensure_response( $comments );
     316        $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $comments );
    307317        $response->header( 'X-WP-Total', $total_comments );
    308318        $response->header( 'X-WP-TotalPages', $max_pages );
     
    10411051        // Restores the more descriptive, specific name for use within this method.
    10421052        $comment = $item;
     1053
     1054        // Don't prepare the response body for HEAD requests.
     1055        if ( $request->is_method( 'HEAD' ) ) {
     1056            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
     1057            return apply_filters( 'rest_prepare_comment', new WP_REST_Response(), $comment, $request );
     1058        }
    10431059
    10441060        $fields = $this->get_fields_for_response( $request );
Note: See TracChangeset for help on using the changeset viewer.