Skip to content

Conversation

@LiamSarsfield
Copy link
Contributor

@LiamSarsfield LiamSarsfield commented Apr 18, 2025

Resolves HOG-84:Invalidate LCP when theme is switched

Proposed changes:

  • Introduces a new LCP_Invalidator class to handle the invalidation (clearing) of stored LCP (Largest Contentful Paint) analysis data.
  • LCP data is now reset when:
    • A "major" environment change occurs (currently, this is triggered by after_switch_theme).
    • The Jetpack Boost plugin is deactivated (jetpack_boost_deactivate action).
  • Adds a new action jetpack_boost_lcp_invalidated which fires after LCP data has been cleared by the invalidator.
  • Updates the Lcp class to hook into jetpack_boost_lcp_invalidated to restart the LCP analysis process after invalidation.
  • Deprecates the jetpack_boost_critical_css_environment_changed action in favor of the more general jetpack_boost_environment_changed. The old action will still work but trigger a deprecated notice.
  • Updates the Cloud_CSS class to use the new jetpack_boost_environment_changed action.
  • Also removed cornerstone-specific env change handling in Cloud_CSS

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

pc9hqz-3tr-p2

Does this pull request change what data or activity we track or use?

N/A

Testing instructions:

Prerequisites:

  • Jetpack Boost plugin installed and activated.

  • LCP Optimization feature enabled.

    Click here for SQL to check LCP State Option and Storage Posts
    -- Check LCP State (in options table)
    SELECT option_name, option_value
    FROM wp_options -- Replace wp_ with your table prefix if different
    WHERE option_name = 'jetpack_boost_ds_lcp_state';
    
    -- Check LCP Storage (in posts table)
    SELECT ID, post_name, post_content, post_status, post_date_gmt, post_modified_gmt
    FROM wp_posts -- Replace wp_ with your table prefix if different
    WHERE post_type = 'jb_store_lcp';

Testing Theme Switch (Major Change):

  1. Note the current LCP state option value and any existing LCP storage posts. Pay attention to the data, and note the post_modified_gmt timestamps of the existing posts.

    Click here for SQL to check initial LCP State and Storage
    -- Check LCP State (in options table)
    SELECT option_name, option_value
    FROM wp_options -- Replace wp_ with your table prefix if different
    WHERE option_name = 'jetpack_boost_ds_lcp_state';
    
    -- Check LCP Storage (in posts table)
    SELECT ID, post_name, post_content, post_status, post_date_gmt, post_modified_gmt
    FROM wp_posts -- Replace wp_ with your table prefix if different
    WHERE post_type = 'jb_store_lcp';
  2. Go to Appearance -> Themes.

  3. Activate a different theme. Note the approximate time of activation.

  4. Wait a few seconds for the analysis to complete (it should be quick).

  5. Re-check the jetpack_boost_ds_lcp_state option in the wp_options table. Verify its value has been updated (it won't have a timestamp, but the content should reflect recent analysis).

  6. Re-check the wp_posts table for jb_store_lcp posts. Verify that new posts now exist and their post_date_gmt or post_modified_gmt timestamps are after the time you activated the theme.

    Click here for SQL to check updated LCP State and new Storage posts
    -- Check LCP State (in options table)
    SELECT option_name, option_value
    FROM wp_options -- Replace wp_ with your table prefix if different
    WHERE option_name = 'jetpack_boost_ds_lcp_state';
    -- Expect option_value to contain data reflecting the new analysis state
    
    -- Check LCP Storage (in posts table)
    SELECT ID, post_name, post_content, post_status, post_date_gmt, post_modified_gmt
    FROM wp_posts -- Replace wp_ with your table prefix if different
    WHERE post_type = 'jb_store_lcp' AND post_status != 'trash';
    -- Expect new posts corresponding to visited pages with recent timestamps

Testing Plugin Deactivation:

  1. Ensure there is some LCP data stored (check state option and storage posts).

    Click here for SQL to check initial LCP State and Storage
    -- Check LCP State (in options table)
    SELECT option_name, option_value
    FROM wp_options -- Replace wp_ with your table prefix if different
    WHERE option_name = 'jetpack_boost_ds_lcp_state';
    
    -- Check LCP Storage (in posts table)
    SELECT ID, post_name, post_content, post_status, post_date_gmt, post_modified_gmt
    FROM wp_posts -- Replace wp_ with your table prefix if different
    WHERE post_type = 'jb_store_lcp';
  2. Go to Plugins -> Installed Plugins.

  3. Deactivate the Jetpack Boost plugin.

  4. Verify that the jetpack_boost_ds_lcp_state option has been cleared or reset, and that all jb_store_lcp posts have been deleted/trashed.

    Click here for SQL to verify LCP State is cleared and Storage posts are deleted
    -- Check LCP State (in options table)
    SELECT option_name, option_value
    FROM wp_options -- Replace wp_ with your table prefix if different
    WHERE option_name = 'jetpack_boost_ds_lcp_state';
    -- Expect option_value to be empty or reflect a default/cleared state
    
    -- Check LCP Storage (in posts table)
    SELECT COUNT(*) as post_count
    FROM wp_posts -- Replace wp_ with your table prefix if different
    WHERE post_type = 'jb_store_lcp' AND post_status != 'trash';
    -- Expect post_count to be 0

Testing Deprecated Action:

  1. Add the following PHP snippet to your site (e.g., in a custom plugin or your theme's functions.php).

    Click here for PHP Snippet to test deprecated action
    <?php
    /**
     * Plugin Name: Test Boost Deprecated Action
     * Description: Logs when the deprecated jetpack_boost_critical_css_environment_changed action fires.
     */
    
    add_action( 'jetpack_boost_critical_css_environment_changed', 'my_test_boost_deprecated_action_listener', 10, 2 );
    
    function my_test_boost_deprecated_action_listener( $is_major_change, $change_type ) {
        // Log a message to the PHP error log when the deprecated action runs
        error_log( 
            sprintf( 
                'Deprecated action jetpack_boost_critical_css_environment_changed fired. Is major: %s, Type: %s', 
                var_export( $is_major_change, true ), // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
                esc_html( $change_type ) 
            ) 
        );
    }
  2. Ensure WP_DEBUG and WP_DEBUG_LOG are enabled in your wp-config.php file so that deprecation notices and the custom log message are written to /wp-content/debug.log.

  3. Trigger an environment change (e.g., switch themes via Appearance -> Themes, or save a published post/page).

  4. Check your /wp-content/debug.log file. Verify that:

    • The custom log message "Deprecated action jetpack_boost_critical_css_environment_changed fired..." appears.
    • A PHP Deprecated notice appears for jetpack_boost_critical_css_environment_changed, indicating it was called and suggesting jetpack_boost_environment_changed instead.

Testing Cloud_CSS Update:

  1. Ensure Cloud CSS is active and configured.
  2. Trigger environment changes (switching themes, saving posts/pages).
  3. Verify that Cloud CSS generation/invalidation still behaves correctly (e.g., CSS is regenerated/invalidated appropriately based on the change type and is_major_change flag passed to jetpack_boost_environment_changed).

@github-actions
Copy link
Contributor

github-actions bot commented Apr 18, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Boost plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Apr 18, 2025
@jp-launch-control
Copy link

jp-launch-control bot commented Apr 18, 2025

Code Coverage Summary

Coverage changed in 3 files.

File Coverage Δ% Δ Uncovered
projects/plugins/boost/app/lib/class-environment-change-detector.php 0/39 (0.00%) 0.00% 6 💔
projects/plugins/boost/app/modules/optimizations/lcp/class-lcp.php 0/124 (0.00%) 0.00% 3 ❤️‍🩹
projects/plugins/boost/app/modules/optimizations/cloud-css/class-cloud-css.php 1/81 (1.23%) 0.03% -2 💚

1 file is newly checked for coverage.

File Coverage
projects/plugins/boost/app/modules/optimizations/lcp/class-lcp-invalidator.php 0/9 (0.00%) 💔

Full summary · PHP report · JS report

Coverage check overridden by Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR .

@dilirity dilirity force-pushed the add/boost/lcp/invalidate-conditions branch from 42270de to 9194bd7 Compare April 22, 2025 11:44
@LiamSarsfield LiamSarsfield changed the title Add invalidator Boost: Reset LCP State/Storage Upon Boost deactivate/Theme change Apr 23, 2025
@LiamSarsfield LiamSarsfield added the [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it label Apr 23, 2025
@LiamSarsfield LiamSarsfield changed the title Boost: Reset LCP State/Storage Upon Boost deactivate/Theme change Boost: Invalidate LCP Data on Environment Changes Apr 24, 2025
@LiamSarsfield LiamSarsfield added the Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR label Apr 24, 2025
@LiamSarsfield LiamSarsfield requested a review from a team April 24, 2025 09:06
@LiamSarsfield LiamSarsfield added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Apr 24, 2025
@LiamSarsfield LiamSarsfield marked this pull request as ready for review April 24, 2025 09:14
Copy link
Member

@dilirity dilirity left a comment

Choose a reason for hiding this comment

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

I think this is ready for a merge, but considering I wrote the majority of the code, I'd like for someone else to review it as well.

@dilirity dilirity requested review from a team and haqadn May 5, 2025 13:25
/**
* Handle when a critical CSS environment change is detected.
*/
public function handle_environment_change( $is_major_change, $change_type ) {
Copy link
Member

Choose a reason for hiding this comment

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

I noticed this was added in https://github.com/Automattic/jetpack/pull/41516/files but was unused even there.

Copy link
Contributor Author

@LiamSarsfield LiamSarsfield left a comment

Choose a reason for hiding this comment

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

I fixed a merge conflict after merging #43150, but these changes LGTM @dilirity

@LiamSarsfield LiamSarsfield requested a review from dilirity May 6, 2025 13:49
@donnchawp
Copy link
Contributor

I tested everything but the cloud css test at the end and they all worked. I'll take a look at the cloud css part tomorrow morning.

@dilirity dilirity merged commit 8789de4 into trunk May 7, 2025
69 checks passed
@dilirity dilirity deleted the add/boost/lcp/invalidate-conditions branch May 7, 2025 07:21
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label May 7, 2025
jeherve added a commit that referenced this pull request May 13, 2025
@haqadn haqadn added this to the boost/3.14.0 milestone May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Boost Feature] Cloud CSS [Boost Feature] Lcp Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR [Plugin] Boost A feature to speed up the site and improve performance. [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants