Skip to content
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

Boost: Reduce unnecessary CSS regenerations #40891

Merged
merged 12 commits into from
Jan 17, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class Singular_Post_Provider extends Provider {
*
* @var integer
*/
const MAX_URLS = 20;
const MAX_URLS = 10;

/**
* Minimum number of posts to have Critical CSS generated in order for the whole process to be successful.
*
* @var integer
*/
const MIN_SUCCESS_URLS = 10;
const MIN_SUCCESS_URLS = 5;

// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class Taxonomy_Provider extends Provider {
*
* @var integer
*/
const MAX_URLS = 20;
const MAX_URLS = 10;

/**
* Minimum number of posts to have Critical CSS generated in order for the whole process to be successful.
*
* @var integer
*/
const MIN_SUCCESS_URLS = 10;
const MIN_SUCCESS_URLS = 5;

// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/** @inheritdoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ public function handle_save_post( $post_id, $post ) { // phpcs:ignore VariableAn
return;
}

$this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST, $this->get_all_providers( array( $post ) ) );
// This checks against the latest providers list, not the list
// stored in the database because newly added posts are always
// included in the providers list that will be used to generate
// the Cloud CSS.
if ( $this->is_post_in_latest_providers_list( $post ) ) {
$this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST, $this->get_all_providers( array( $post ) ) );
}
}

public function regenerate_cloud_css( $reason, $providers ) {
Expand All @@ -171,6 +177,26 @@ public function regenerate_cloud_css( $reason, $providers ) {
return $result;
}

/**
* Check if the post is in the latest providers list.
*
* @param int|\WP_Post $post The post to check.
*
* @return bool
*/
public function is_post_in_latest_providers_list( $post ) {
$post_link = get_permalink( $post );
$providers = $this->get_all_providers();

foreach ( $providers as $provider ) {
if ( in_array( $post_link, $provider['urls'], true ) ) {
return true;
}
}

return false;
}

/**
* Called when stored Critical CSS has been invalidated. Triggers a new Cloud CSS request.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Critical CSS: Reduce unnecessary regenerations.
Loading