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-font-collections-controller.php

    r57686 r59899  
    9090        $collections_page = array_slice( $collections_all, ( $page - 1 ) * $per_page, $per_page );
    9191
     92        $is_head_request = $request->is_method( 'HEAD' );
     93
    9294        $items = array();
    9395        foreach ( $collections_page as $collection ) {
     
    98100                continue;
    99101            }
     102
     103            /*
     104             * Skip preparing the response body for HEAD requests.
     105             * Cannot exit earlier due to backward compatibility reasons,
     106             * as validation occurs in the prepare_item_for_response method.
     107             */
     108            if ( $is_head_request ) {
     109                continue;
     110            }
     111
    100112            $item    = $this->prepare_response_for_collection( $item );
    101113            $items[] = $item;
    102114        }
    103115
    104         $response = rest_ensure_response( $items );
     116        $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $items );
    105117
    106118        $response->header( 'X-WP-Total', (int) $total_items );
     
    176188            }
    177189
     190            /**
     191             * Don't prepare the response body for HEAD requests.
     192             * Can't exit at the beginning of the method due to the potential need to return a WP_Error object.
     193             */
     194            if ( $request->is_method( 'HEAD' ) ) {
     195                /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
     196                return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
     197            }
     198
    178199            foreach ( $data_fields as $field ) {
    179200                if ( rest_is_field_included( $field, $fields ) ) {
     
    181202                }
    182203            }
     204        }
     205
     206        /**
     207         * Don't prepare the response body for HEAD requests.
     208         * Can't exit at the beginning of the method due to the potential need to return a WP_Error object.
     209         */
     210        if ( $request->is_method( 'HEAD' ) ) {
     211            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
     212            return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
    183213        }
    184214
Note: See TracChangeset for help on using the changeset viewer.