Skip to content

Prevent effective asset cache headers audit from running on local/development environments #2035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove static asset caching from site health for local and dev envior…
…nment
hbhalodia committed Jun 3, 2025

Verified

This commit was signed with the committer’s verified signature.
hbhalodia Hit Bhalodia
commit 490d093da55ce473743dc987dc2bb681a8e7c396
Original file line number Diff line number Diff line change
@@ -26,6 +26,16 @@ function perflab_effective_asset_cache_headers_add_test( array $tests ): array {
'label' => __( 'Effective Caching Headers', 'performance-lab' ),
'test' => 'perflab_effective_asset_cache_headers_assets_test',
);

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
/*

* Static assets are expected to not have effective cache headers in non-production environments.
*
* GH Issue: https://github.com/WordPress/performance/issues/2031
*/
if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) {
unset( $tests['direct']['effective_asset_cache_headers'] );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of unsetting something that was set, why not just short-circuit the function to avoid setting it in the first place, or rather wrap the setting with the if statement, so the entire function body can be:

if ( ! in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) {
	$tests['direct']['effective_asset_cache_headers'] = array(
		'label' => __( 'Effective Caching Headers', 'performance-lab' ),
		'test'  => 'perflab_effective_asset_cache_headers_assets_test',
	);
}
return $tests;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it can be work and even more instead of wrapping it in if, we can bail early like,

/*
* Bail early.
*/
if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) {
	return $tests;
}

$tests['direct']['effective_asset_cache_headers'] = array(
	'label' => __( 'Effective Caching Headers', 'performance-lab' ),
	'test'  => 'perflab_effective_asset_cache_headers_assets_test',
);

return $tests;


return $tests;
}
add_filter( 'site_status_tests', 'perflab_effective_asset_cache_headers_add_test' );
add_filter( 'site_status_tests', 'perflab_effective_asset_cache_headers_add_test', 100 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change to priority 100?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to make sure it would run after all the filters, I thought it would be filter somewhere else as well, but priority 100 can be removed though.

Original file line number Diff line number Diff line change
@@ -40,6 +40,14 @@ public function test_perflab_effective_asset_cache_headers_add_test(): void {

$tests = perflab_effective_asset_cache_headers_add_test( $tests );

// Test for local env.
$this->assertArrayNotHasKey( 'effective_asset_cache_headers', $tests['direct'] );

// Mock the env to production and call test again.
// @todo Mock wp_get_environment_type here.
$tests = perflab_effective_asset_cache_headers_add_test( $tests );

// Test for production env.
$this->assertArrayHasKey( 'effective_asset_cache_headers', $tests['direct'] );
$this->assertEquals( 'Effective Caching Headers', $tests['direct']['effective_asset_cache_headers']['label'] );
$this->assertEquals( 'perflab_effective_asset_cache_headers_assets_test', $tests['direct']['effective_asset_cache_headers']['test'] );
@@ -51,7 +59,7 @@ public function test_perflab_effective_asset_cache_headers_add_test(): void {
* @covers ::perflab_effective_asset_cache_headers_add_test
*/
public function test_perflab_effective_asset_cache_headers_add_test_is_attached_to_site_status_tests(): void {
$this->assertNotFalse( has_filter( 'site_status_tests', 'perflab_effective_asset_cache_headers_add_test' ) );
$this->assertEquals( 100, has_filter( 'site_status_tests', 'perflab_effective_asset_cache_headers_add_test' ) );
}

/**