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: Implement Cache Preloading - Version One #41988

Draft
wants to merge 10 commits into
base: trunk
Choose a base branch
from

Conversation

LiamSarsfield
Copy link
Contributor

@LiamSarsfield LiamSarsfield commented Feb 24, 2025

Fixes

#42111

Proposed changes:

  • Adds a Cache Preloading system for Cornerstone Pages
  • Introduces a new submodule Cache_Preload.php to handle cache preloading logic (Submodule of Page Cache)
  • Updates cache invalidation process to trigger preloading for cornerstone pages when they're invalidated
  • Improves the structure of the invalidate method in File_Storage.php with better action handling

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-3nV-p2

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

N/A

Testing instructions:

1. Saving Cornerstone Pages list, Clearing Page Cache

Instructions

Before Applying Branch

  1. Go to Boost Settings page.
  2. Ensure Boost Premium is on the WordPress site.
  3. Enable Optimize CSS Loading (Cloud CSS).
  4. Enable Cache Site Pages (Page Cache).
  5. Add at least 2 valid Cornerstone Pages.
  6. Clear Page Cache.
  7. Within the WordPress site, head to ./wp-content/boost-cache/cache/(domain)
  8. Verify that no page caching files are present.
  9. Head to one of the valid Cornerstone Pages added in Incognito.
  10. Verify that page caching is generated for that Cornerstone Page visited.
  11. Clear Page Cache.
  12. Within the WordPress site, head to ./wp-content/boost-cache/cache/(domain)
  13. Verify that no page caching files are present.

After Applying Branch

  1. Go to Boost Settings page.
  2. Add a third valid page to the Cornerstone Pages list
  3. Wait 2 seconds, refresh the page (to ensure Preloading occurs)
  4. Within the WordPress site, head to ./wp-content/boost-cache/cache/(domain)
  5. You should now see that 3 caching files have been generated automagically ✨
  6. Clear the Page Cache
  7. Wait 2 seconds, refresh the page (to ensure Preloading occurs within the Cron)
  8. You should now see that 3 caching files have been generated automagically again ✨

2. Updating Cornerstone Page

Instructions

Before Applying Branch

  1. Go to Boost Settings page.
  2. Ensure Boost Premium is on the WordPress site.
  3. Enable Optimize CSS Loading (Cloud CSS).
  4. Enable Cache Site Pages (Page Cache).
  5. Add at least 2 valid Cornerstone Pages.
  6. Clear Page Cache.
  7. Within the WordPress site, head to ./wp-content/boost-cache/cache/(domain)
  8. Verify that no page caching files are present.
  9. Edit one of the posts/pages you added to the Cornerstone Page list.
  10. Within the WordPress site, head to ./wp-content/boost-cache/cache/(domain)
  11. Verify that the page you edited is not cached.

After Applying Branch

  1. Go to Boost Settings page.
  2. Clear Page Cache.
  3. Edit one of the posts/pages you added to the Cornerstone Page list.
  4. Wait 2 seconds, refresh the post edit page (to ensure Preloading occurs within the Cron).
  5. Within the WordPress site, head to ./wp-content/boost-cache/cache/(domain)
  6. You should now see that specific page's caching file generated automagically ✨

@github-actions github-actions bot added [Boost Feature] Page Cache [Plugin] Boost A feature to speed up the site and improve performance. [Status] In Progress labels Feb 24, 2025
Copy link
Contributor

github-actions bot commented Feb 24, 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 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


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 Feb 24, 2025
Copy link

jp-launch-control bot commented Feb 24, 2025

Code Coverage Summary

Cannot generate coverage summary while tests are failing. 🤐

Please fix the tests, or re-run the Code coverage job if it was something being flaky.

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 added [Type] Feature Development of a new feature 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 Feb 24, 2025
…date mechanism

- Refactor preload mechanism to support dynamic post scheduling
- Add method to handle post updates and schedule preloading for cornerstone pages
- Implement more flexible preload post tracking using WordPress options
- Improve error handling during page preloading
@LiamSarsfield LiamSarsfield self-assigned this Feb 27, 2025
@LiamSarsfield LiamSarsfield added this to the boost/3.10.0 milestone Feb 27, 2025
@LiamSarsfield LiamSarsfield marked this pull request as ready for review February 27, 2025 11:32
@LiamSarsfield LiamSarsfield marked this pull request as draft February 27, 2025 11:32
- Add new method `is_cornerstone_page_by_url()` in Cornerstone_Utils to check cornerstone pages by URL
- Modify `is_cornerstone_page()` to use the new URL-based method
- Update Cache_Preload to handle cache invalidation and preloading of cornerstone pages
- Add action hook for successful cache invalidation
- Improve method names and add comments for clarity
@LiamSarsfield LiamSarsfield changed the title Add/boost/cache-preloading-cornerstone-pages Boost: Implement Cache Preloading - Version One Feb 27, 2025
@LiamSarsfield LiamSarsfield linked an issue Feb 27, 2025 that may be closed by this pull request
- Enhance `handle_cache_invalidation()` method to support different cache invalidation types
- Add support for full cache deletion by preloading all Cornerstone Pages
- Improve documentation with detailed comments and @SInCE tags
- Update method signature to include invalidation type parameter
- Refactor cache preloading logic for more robust handling of cache events
// Clear the preload posts so they're not preloaded again.
$this->set_posts_to_preload( array() );

foreach ( $posts as $post ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if we should limit how many pages/posts are being preloaded here within the cron to prevent potentially overloading the site. Theoritically the limit of URLs should only be 10 as that's our current Cornerstone Page limit.
We could only preload 10 URLs at a time, and then schedule another cron job after 2 seconds to repeat the same.
Not sure if it's worth doing now, or wait until Version 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented this now with batching, as seen below:

/**
* Prepares the next batch of URLs to preload.
*
* Takes the full list of posts to preload, extracts the first batch,
* updates the preload queue, and schedules the next run if needed.
*
* @since $$next-version$$
* @param array $posts Full list of posts to preload.
* @return array The batch of posts to process now.
*/
private function prepare_next_batch( $posts ) {
// Process in batches of 10 to reduce server load
$batches = array_chunk( $posts, 10 );
$current_batch = array_shift( $batches );
// Calculate remaining posts
$remaining = $this->flatten_batches( $batches );
// Update the preload queue
$this->set_posts_to_preload( $remaining );
// Schedule the next batch if needed
if ( ! empty( $remaining ) ) {
$this->schedule_preload_cronjob();
}
return $current_batch;
}

- Refactor page preloading to process URLs in batches of 10
- Add batch processing to reduce server load and improve reliability
- Enhance error handling with detailed logging for preload failures
- Improve HTTP response validation during page preloading
- Add methods to manage batch processing and track remaining URLs
- Create new test suite for Cache_Preload class in Jetpack Boost
- Implement comprehensive test coverage for cache preloading methods
- Test scenarios including cornerstone page preloading, cache invalidation, and scheduling
- Verify interactions with WordPress core functions and utility classes
- Ensure robust testing of preload scheduling, post update handling, and cache management
- Refactor test cases to use direct method calls instead of partial mocks
- Replace Mockery-based mocking with explicit WordPress function expectations
- Improve test coverage by directly testing Cache_Preload class methods
- Simplify test setup and reduce complexity of test assertions
- Ensure consistent testing approach for cache preloading functionality
@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 Feb 28, 2025
- Enhance page preloading by adding cache-control headers
@LiamSarsfield LiamSarsfield removed this from the boost/3.10.0 milestone Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Boost Feature] Page Cache 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. [Status] In Progress [Tests] Includes Tests [Type] Feature Development of a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Boost: Cache Preloading - Version One, Cornerstone Pages
2 participants