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/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php

    r59605 r59899  
    3434     */
    3535    const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
     36
     37    /**
     38     * @var string
     39     */
     40    const PARENT_POST_TYPE = 'wp_template';
    3641
    3742    /**
     
    294299
    295300    /**
     301     * @ticket 56481
     302     */
     303    public function test_get_items_should_return_no_response_body_for_head_requests() {
     304        wp_set_current_user( self::$admin_id );
     305        $autosave_post_id = wp_create_post_autosave(
     306            array(
     307                'post_content' => 'Autosave content.',
     308                'post_ID'      => self::$template_post->ID,
     309                'post_type'    => self::PARENT_POST_TYPE,
     310            )
     311        );
     312
     313        $request  = new WP_REST_Request(
     314            'HEAD',
     315            '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves'
     316        );
     317        $response = rest_get_server()->dispatch( $request );
     318        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
     319        $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     320    }
     321
     322    /**
    296323     * Data provider for test_get_items_with_data_provider.
    297324     *
     
    425452            )
    426453        );
     454    }
     455
     456    /**
     457     * @ticket 56481
     458     */
     459    public function test_get_item_should_return_no_response_body_for_head_requests() {
     460        wp_set_current_user( self::$admin_id );
     461
     462        $autosave_post_id = wp_create_post_autosave(
     463            array(
     464                'post_content' => 'Autosave content.',
     465                'post_ID'      => self::$template_post->ID,
     466                'post_type'    => self::PARENT_POST_TYPE,
     467            )
     468        );
     469
     470        $request  = new WP_REST_Request( 'HEAD', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves/' . $autosave_post_id );
     471        $response = rest_get_server()->dispatch( $request );
     472        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
     473        $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    427474    }
    428475
Note: See TracChangeset for help on using the changeset viewer.