Make WordPress Core


Ignore:
Timestamp:
03/02/2025 10:05:08 PM (18 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-widgets-controller.php

    r58704 r59899  
    137137         */
    138138        public function get_items( $request ) {
     139                if ( $request->is_method( 'HEAD' ) ) {
     140                        // Return early as this handler doesn't add any response headers.
     141                        return new WP_REST_Response();
     142                }
     143
    139144                $this->retrieve_widgets();
    140145
     
    679684
    680685                $widget    = $wp_registered_widgets[ $widget_id ];
     686                // Don't prepare the response body for HEAD requests.
     687                if ( $request->is_method( 'HEAD' ) ) {
     688                        /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php */
     689                        return apply_filters( 'rest_prepare_widget', new WP_REST_Response(), $widget, $request );
     690                }
     691
    681692                $parsed_id = wp_parse_widget_id( $widget_id );
    682693                $fields    = $this->get_fields_for_response( $request );
Note: See TracChangeset for help on using the changeset viewer.