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-widget-types-controller.php

    r56586 r59899  
    146146     */
    147147    public function get_items( $request ) {
     148        if ( $request->is_method( 'HEAD' ) ) {
     149            // Return early as this handler doesn't add any response headers.
     150            return new WP_REST_Response();
     151        }
     152
    148153        $data = array();
    149154        foreach ( $this->get_widgets() as $widget ) {
     
    298303        // Restores the more descriptive, specific name for use within this method.
    299304        $widget_type = $item;
     305
     306        // Don't prepare the response body for HEAD requests.
     307        if ( $request->is_method( 'HEAD' ) ) {
     308            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php */
     309            return apply_filters( 'rest_prepare_widget_type', new WP_REST_Response(), $widget_type, $request );
     310        }
    300311
    301312        $fields = $this->get_fields_for_response( $request );
Note: See TracChangeset for help on using the changeset viewer.