Skip to content

Commit

Permalink
Boost: don't cache pages with errors that exit very early (#38054)
Browse files Browse the repository at this point in the history
* Check for early fatal errors that should stop caching

* changelog

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/9675839062

Upstream-Ref: Automattic/jetpack@ee67524
  • Loading branch information
donnchawp authored and matticbot committed Jun 26, 2024
1 parent 8aadf79 commit 54d043d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

This is an alpha version! The changes listed here are not final.

### Added
- Jetpack Boost: ensure that "init" fires before allowing a page to be cached to avoid caching error pages

### Fixed
- Detect when WP_CACHE is defined with "const" in wp-config.php
- General: Fix instance where Boost can break caching for other caching plugins when getting deactivated.
Expand Down
21 changes: 21 additions & 0 deletions app/modules/optimizations/page-cache/pre-wordpress/Boost_Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class Boost_Cache {
*/
private static $cache_engine_loaded = false;

/**
* @var bool - Indicates whether WordPress initialized correctly and we can cache the page.
*/
private $do_cache = false;

/**
* @param ?Storage\Storage $storage - Optionally provide a Storage subclass to handle actually storing and retrieving cached content. Defaults to a new instance of File_Storage.
*/
Expand All @@ -75,6 +80,7 @@ public function init_actions() {
add_action( 'switch_theme', array( $this, 'invalidate_cache' ) );
add_action( 'wp_trash_post', array( $this, 'delete_on_post_trash' ), 10, 2 );
add_filter( 'wp_php_error_message', array( $this, 'disable_caching_on_error' ) );
add_filter( 'init', array( $this, 'init_do_cache' ) );
}

/**
Expand Down Expand Up @@ -176,6 +182,11 @@ public function ob_start() {
public function ob_callback( $buffer ) {
if ( strlen( $buffer ) > 0 && $this->request->is_cacheable() ) {

// Do not cache the page as WordPress did not initialize correctly.
if ( ! $this->do_cache ) {
return $buffer;
}

if ( false === stripos( $buffer, '</html>' ) ) {
Logger::debug( 'Closing HTML tag not found, not caching' );
return $buffer;
Expand Down Expand Up @@ -468,4 +479,14 @@ public function disable_caching_on_error( $message ) {
Logger::debug( 'Fatal error detected, caching disabled' );
return $message;
}

/**
* This function is called after WordPress is loaded, on "init".
* It is used to indicate that it is safe to cache and that no
* fatal errors occurred.
*/
public function init_do_cache() {
Logger::debug( 'postload: init succeeded' );
$this->do_cache = true;
}
}

0 comments on commit 54d043d

Please sign in to comment.