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

    r59630 r59899  
    254254        }
    255255
     256        $is_head_request = $request->is_method( 'HEAD' );
     257
    256258        if ( wp_revisions_enabled( $parent ) ) {
    257259            $registered = $this->get_collection_params();
     
    286288            if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
    287289                $args['orderby'] = 'date ID';
     290            }
     291
     292            if ( $is_head_request ) {
     293                // Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
     294                $args['fields'] = 'ids';
     295                // Disable priming post meta for HEAD requests to improve performance.
     296                $args['update_post_term_cache'] = false;
     297                $args['update_post_meta_cache'] = false;
    288298            }
    289299
     
    336346        }
    337347
    338         $response = array();
    339 
    340         foreach ( $revisions as $revision ) {
    341             $data       = $this->prepare_item_for_response( $revision, $request );
    342             $response[] = $this->prepare_response_for_collection( $data );
    343         }
    344 
    345         $response = rest_ensure_response( $response );
     348        if ( ! $is_head_request ) {
     349            $response = array();
     350
     351            foreach ( $revisions as $revision ) {
     352                $data       = $this->prepare_item_for_response( $revision, $request );
     353                $response[] = $this->prepare_response_for_collection( $data );
     354            }
     355
     356            $response = rest_ensure_response( $response );
     357        } else {
     358            $response = new WP_REST_Response();
     359        }
    346360
    347361        $response->header( 'X-WP-Total', (int) $total_revisions );
     
    574588
    575589        setup_postdata( $post );
     590
     591        // Don't prepare the response body for HEAD requests.
     592        if ( $request->is_method( 'HEAD' ) ) {
     593            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */
     594            return apply_filters( 'rest_prepare_revision', new WP_REST_Response(), $post, $request );
     595        }
    576596
    577597        $fields = $this->get_fields_for_response( $request );
Note: See TracChangeset for help on using the changeset viewer.