Skip to content

Commit

Permalink
Implement partial regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
haqadn committed Oct 20, 2024
1 parent 5b6bbb1 commit 4857a5d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion projects/plugins/boost/app/lib/critical-css/Regenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function start() {
// If this is a cloud CSS request, we need to trigger the generation
// of the CSS and return the URL to the CSS file.
$cloud_css = new Cloud_CSS();
$cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_USER_REQUEST );
$cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_USER_REQUEST, $cloud_css->get_all_providers() );
Cloud_CSS_Followup::schedule();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function get_current_request_css() {
*
* @return array
*/
public function get_provider_sources() {
public function get_provider_sources( $context_posts = array() ) {
$sources = array();

$wp_core_provider_urls = WP_Core_Provider::get_critical_source_urls();
$wp_core_provider_urls = WP_Core_Provider::get_critical_source_urls( $context_posts );
$flat_wp_core_urls = array();
foreach ( $wp_core_provider_urls as $urls ) {
$flat_wp_core_urls = array_merge( $flat_wp_core_urls, $urls );
Expand All @@ -127,7 +127,7 @@ public function get_provider_sources() {

// For each provider,
// Gather a list of URLs that are going to be used as Critical CSS source.
foreach ( $provider::get_critical_source_urls() as $group => $urls ) {
foreach ( $provider::get_critical_source_urls( $context_posts ) as $group => $urls ) {
if ( empty( $urls ) ) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ public function handle_save_post( $post_id, $post ) { // phpcs:ignore VariableAn
return;
}

$this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST );
$this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST, $this->get_all_providers( array( $post ) ) );
}

public function regenerate_cloud_css( $reason ) {
$result = $this->generate_cloud_css( $reason, $this->get_existing_sources() );
public function regenerate_cloud_css( $reason, $providers ) {
$result = $this->generate_cloud_css( $reason, $providers );
if ( is_wp_error( $result ) ) {
$state = new Critical_CSS_State();
$state->set_error( $result->get_error_message() )->save();
Expand All @@ -171,18 +171,22 @@ public function regenerate_cloud_css( $reason ) {
* Called when stored Critical CSS has been invalidated. Triggers a new Cloud CSS request.
*/
public function handle_critical_css_invalidated() {
$this->regenerate_cloud_css( self::REGENERATE_REASON_INVALIDATED );
$this->regenerate_cloud_css( self::REGENERATE_REASON_INVALIDATED, $this->get_all_providers() );
Cloud_CSS_Followup::schedule();
}

public function get_all_providers( $context_posts = array() ) {
$source_providers = new Source_Providers();
return $source_providers->get_provider_sources( $context_posts );
}

public function get_existing_sources() {
$state = new Critical_CSS_State();
$data = $state->get();
if ( ! empty( $data['providers'] ) ) {
$providers = $data['providers'];
} else {
$source_providers = new Source_Providers();
$providers = $source_providers->get_provider_sources();
$providers = $this->get_all_providers();
}

return $providers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function run() {
$state = new Critical_CSS_State();
if ( $state->has_errors() ) {
$cloud_css = new Cloud_CSS();
$cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_FOLLOWUP );
$cloud_css->regenerate_cloud_css( Cloud_CSS::REGENERATE_REASON_FOLLOWUP, $cloud_css->get_existing_sources() );
}
}

Expand Down

0 comments on commit 4857a5d

Please sign in to comment.