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-global-styles-revisions-controller.php

    r59630 r59899  
    164164        }
    165165
     166        $is_head_request = $request->is_method( 'HEAD' );
     167
    166168        if ( wp_revisions_enabled( $parent ) ) {
    167169            $registered = $this->get_collection_params();
     
    185187                    $query_args[ $wp_param ] = $request[ $api_param ];
    186188                }
     189            }
     190
     191            if ( $is_head_request ) {
     192                // Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
     193                $query_args['fields'] = 'ids';
     194                // Disable priming post meta for HEAD requests to improve performance.
     195                $query_args['update_post_term_cache'] = false;
     196                $query_args['update_post_meta_cache'] = false;
    187197            }
    188198
     
    229239        }
    230240
    231         $response = array();
    232 
    233         foreach ( $revisions as $revision ) {
    234             $data       = $this->prepare_item_for_response( $revision, $request );
    235             $response[] = $this->prepare_response_for_collection( $data );
    236         }
    237 
    238         $response = rest_ensure_response( $response );
     241        if ( ! $is_head_request ) {
     242            $response = array();
     243
     244            foreach ( $revisions as $revision ) {
     245                $data       = $this->prepare_item_for_response( $revision, $request );
     246                $response[] = $this->prepare_response_for_collection( $data );
     247            }
     248
     249            $response = rest_ensure_response( $response );
     250        } else {
     251            $response = new WP_REST_Response();
     252        }
    239253
    240254        $response->header( 'X-WP-Total', (int) $total_revisions );
     
    276290     */
    277291    public function prepare_item_for_response( $post, $request ) {
     292        // Don't prepare the response body for HEAD requests.
     293        if ( $request->is_method( 'HEAD' ) ) {
     294            return new WP_REST_Response();
     295        }
     296
    278297        $parent               = $this->get_parent( $request['parent'] );
    279298        $global_styles_config = $this->get_decoded_global_styles_json( $post->post_content );
Note: See TracChangeset for help on using the changeset viewer.