Make WordPress Core


Ignore:
Timestamp:
03/02/2025 10:05:08 PM (16 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/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php

    r56492 r59899  
    125125
    126126    /**
     127     * @ticket 56481
     128     */
     129    public function test_get_items_with_head_request_should_not_prepare_block_pattern_categories_data() {
     130        wp_set_current_user( self::$admin_id );
     131        $request  = new WP_REST_Request( 'HEAD', static::REQUEST_ROUTE );
     132        $response = rest_get_server()->dispatch( $request );
     133        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     134        $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     135    }
     136
     137    /**
    127138     * Verify capability check for unauthorized request (not logged in).
    128139     */
Note: See TracChangeset for help on using the changeset viewer.