Skip to content

Commit

Permalink
Disable preparing term meta for HEAD requests to improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-vlasenko authored and Anton Vlasenko committed Dec 14, 2024
1 parent b9a878f commit 2529c71
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
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-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 2529c71

Please sign in to comment.