Skip to content

Commit

Permalink
Boost: Reduce unnecessary CSS regenerations (#40891)
Browse files Browse the repository at this point in the history
  • Loading branch information
dilirity authored Jan 17, 2025
1 parent 830ba64 commit 54d732a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
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.

0 comments on commit 54d732a

Please sign in to comment.