Skip to content

Commit

Permalink
Disable preparing meta for HEAD requests to improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-vlasenko committed Dec 14, 2024
1 parent 2e17f2b commit c7eee61
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ public function get_items( $request ) {
if ( $is_head_request ) {
// Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
$prepared_args['fields'] = 'ids';
// Disable priming comment meta for HEAD requests to improve performance.
$prepared_args['update_comment_meta_cache'] = false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ static function ( $format ) {
if ( $is_head_request ) {
// Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
$args['fields'] = 'ids';
// Disable the priming of post terms and metadata for HEAD requests to improve performance.
$args['update_post_term_cache'] = false;
$args['update_post_meta_cache'] = false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ public function get_items( $request ) {
if ( $is_head_request ) {
// Force the 'fields' argument. For HEAD requests, only term IDs are required.
$prepared_args['fields'] = 'ids';
// Disable priming term meta for HEAD requests to improve performance.
$prepared_args['update_term_meta_cache'] = false;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/tests/rest-api/rest-categories-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1288,11 +1288,15 @@ public function test_get_items_only_fetches_ids_for_head_requests( $method ) {
if ( $is_head_request ) {
$this->assertArrayHasKey( 'fields', $query->query_vars, 'The fields parameter is not set in the query vars.' );
$this->assertSame( 'ids', $query->query_vars['fields'], 'The query must fetch only term IDs.' );
$this->assertArrayHasKey( 'update_term_meta_cache', $query->query_vars, 'The update_term_meta_cache key is missing in the query vars.' );
$this->assertFalse( $query->query_vars['update_term_meta_cache'], 'The update_term_meta_cache value should be false for HEAD requests.' );
} else {
$this->assertTrue(
! array_key_exists( 'fields', $query->query_vars ) || 'ids' !== $query->query_vars['fields'],
'The fields parameter should not be forced to "ids" for non-HEAD requests.'
);
$this->assertArrayHasKey( 'update_term_meta_cache', $query->query_vars, 'The update_term_meta_cache key is missing in the query vars.' );
$this->assertTrue( $query->query_vars['update_term_meta_cache'], 'The update_term_meta_cache value should be true for HEAD requests.' );
}

if ( ! $is_head_request ) {
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3516,8 +3516,12 @@ public function test_get_items_only_fetches_ids_for_head_requests( $method ) {
if ( $is_head_request ) {
$this->assertArrayHasKey( 'fields', $query->query_vars, 'The fields parameter is not set in the query vars.' );
$this->assertSame( 'ids', $query->query_vars['fields'], 'The query must fetch only post IDs.' );
$this->assertArrayHasKey( 'update_comment_meta_cache', $query->query_vars, 'The update_comment_meta_cache key is missing in the query vars.' );
$this->assertFalse( $query->query_vars['update_comment_meta_cache'], 'The update_comment_meta_cache value should be false for HEAD requests.' );
} else {
$this->assertTrue( ! array_key_exists( 'fields', $query->query_vars ) || 'ids' !== $query->query_vars['fields'], 'The fields parameter should not be forced to "ids" for non-HEAD requests.' );
$this->assertArrayHasKey( 'update_comment_meta_cache', $query->query_vars, 'The update_comment_meta_cache key is missing in the query vars.' );
$this->assertTrue( $query->query_vars['update_comment_meta_cache'], 'The update_comment_meta_cache value should be true for non-HEAD requests.' );
return;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1987,9 +1987,17 @@ public function test_get_items_only_fetches_ids_for_head_requests( $method ) {
$this->assertSame( 'ids', $query->query['fields'], 'The query must fetch only post IDs.' );
$this->assertArrayHasKey( 'fields', $query->query_vars, 'The fields parameter is not set in the query vars.' );
$this->assertSame( 'ids', $query->query_vars['fields'], 'The query must fetch only post IDs.' );
$this->assertArrayHasKey( 'update_post_term_cache', $query->query_vars, 'The "update_post_term_cache" parameter is missing in the query vars.' );
$this->assertFalse( $query->query_vars['update_post_term_cache'], 'The "update_post_term_cache" parameter must be false for HEAD requests.' );
$this->assertArrayHasKey( 'update_post_meta_cache', $query->query_vars, 'The "update_post_meta_cache" parameter is missing in the query vars.' );
$this->assertFalse( $query->query_vars['update_post_meta_cache'], 'The "update_post_meta_cache" parameter must be false for HEAD requests.' );
} else {
$this->assertTrue( ! array_key_exists( 'fields', $query->query ) || 'ids' !== $query->query['fields'], 'The fields parameter should not be forced to "ids" for non-HEAD requests.' );
$this->assertTrue( ! array_key_exists( 'fields', $query->query_vars ) || 'ids' !== $query->query_vars['fields'], 'The fields parameter should not be forced to "ids" for non-HEAD requests.' );
$this->assertArrayHasKey( 'update_post_term_cache', $query->query_vars, 'The "update_post_term_cache" parameter is missing in the query vars.' );
$this->assertTrue( $query->query_vars['update_post_term_cache'], 'The "update_post_term_cache" parameter must be true for non-HEAD requests.' );
$this->assertArrayHasKey( 'update_post_meta_cache', $query->query_vars, 'The "update_post_meta_cache" parameter is missing in the query vars.' );
$this->assertTrue( $query->query_vars['update_post_meta_cache'], 'The "update_post_meta_cache" parameter must be true for non-HEAD requests.' );
}

if ( ! $is_head_request ) {
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/tests/rest-api/rest-tags-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1542,11 +1542,15 @@ public function test_get_items_only_fetches_ids_for_head_requests( $method ) {
if ( $is_head_request ) {
$this->assertArrayHasKey( 'fields', $query->query_vars, 'The fields parameter is not set in the query vars.' );
$this->assertSame( 'ids', $query->query_vars['fields'], 'The query must fetch only term IDs.' );
$this->assertArrayHasKey( 'update_term_meta_cache', $query->query_vars, 'The update_term_meta_cache key is missing in the query vars.' );
$this->assertFalse( $query->query_vars['update_term_meta_cache'], 'The update_term_meta_cache value should be false for HEAD requests.' );
} else {
$this->assertTrue(
! array_key_exists( 'fields', $query->query_vars ) || 'ids' !== $query->query_vars['fields'],
'The fields parameter should not be forced to "ids" for non-HEAD requests.'
);
$this->assertArrayHasKey( 'update_term_meta_cache', $query->query_vars, 'The update_term_meta_cache key is missing in the query vars.' );
$this->assertTrue( $query->query_vars['update_term_meta_cache'], 'The update_term_meta_cache value should be true for HEAD requests.' );
}

if ( ! $is_head_request ) {
Expand Down

0 comments on commit c7eee61

Please sign in to comment.