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: Add filter on cache parameters so caching is more flexible #40894

Merged
merged 25 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
19c052c
Add "jetpack_boost_cache_parameters" filter on request parameters
donnchawp Jan 8, 2025
cfc8a42
Use filter to ignore certain cookies when building cache file
donnchawp Jan 8, 2025
2c68bd6
changelog
donnchawp Jan 8, 2025
525b113
Display removed cookies on the debug cache log.
donnchawp Jan 8, 2025
b9a8c57
Add a comment explaining why using a static variable here
donnchawp Jan 13, 2025
8e9e30d
Make cookies unique and trim whitespace.
donnchawp Jan 13, 2025
d01b749
Put all this cookie code together.
donnchawp Jan 13, 2025
0c49164
Merge branch 'trunk' into update/boost/cookie_filter
donnchawp Jan 13, 2025
a154d54
Don't nest to many function calls, for readability
donnchawp Jan 13, 2025
a3c53c8
Removed -> Ignored in cookie variables
donnchawp Jan 14, 2025
c292ab1
Change cookie list to regexes
donnchawp Jan 14, 2025
e339e65
Add docblocks for function and filter
donnchawp Jan 14, 2025
b46c5d4
Remove the JETPACK_BOOST_IGNORE_COOKIES constant and simplify code
donnchawp Jan 14, 2025
b9a1b6c
Replace boost_cache_key_components with jetpack_boost_cache_parameters
donnchawp Jan 14, 2025
276f8b6
Added deprecated notice
donnchawp Jan 14, 2025
8b11903
Fix the arguments on the deprecated filter
donnchawp Jan 15, 2025
a79db0f
Add function to ignore get parameters
donnchawp Jan 16, 2025
7ae48a4
Merge branch 'update/boost/cookie_filter' of github.com:Automattic/je…
donnchawp Jan 16, 2025
a347278
Fix the sourcebuster cookie regex
donnchawp Jan 21, 2025
e59685d
Ignore these analytics cookies
donnchawp Jan 21, 2025
ad6696d
Fix the eucookielaw value so those visitors share a cache file
donnchawp Jan 22, 2025
60790fc
Fix the personalized ads consent cookie too
donnchawp Jan 22, 2025
0ec9515
Check that cookies is valid before using it. Fix PHAN check.
donnchawp Jan 22, 2025
7607f95
Fixes for PHAN
donnchawp Jan 22, 2025
f700e72
Update changelog
dilirity Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Boost_Cache {
*/
private $do_cache = false;

/**
* @var string - The cookies that were removed from the cache parameters.
*/
private $removed_cookies = '';
donnchawp marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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 @@ -81,6 +86,7 @@ public function init_actions() {
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' ) );
add_filter( 'jetpack_boost_cache_parameters', array( $this, 'ignore_cookies' ) );
}

/**
Expand Down Expand Up @@ -141,7 +147,8 @@ public function serve_cached() {

if ( is_string( $cached ) ) {
$this->send_header( 'X-Jetpack-Boost-Cache: hit' );
Logger::debug( 'Serving cached page' );
$removed_cookies_message = $this->removed_cookies === '' ? '' : " and removed cookies: {$this->removed_cookies}";
Logger::debug( 'Serving cached page' . $removed_cookies_message );
echo $cached; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
die();
}
Expand Down Expand Up @@ -198,7 +205,8 @@ public function ob_callback( $buffer ) {
if ( $result instanceof Boost_Cache_Error ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
Logger::debug( 'Error writing cache file: ' . $result->get_error_message() );
} else {
Logger::debug( 'Cache file created' );
$removed_cookies_message = $this->removed_cookies === '' ? '' : " and removed cookies: {$this->removed_cookies}";
Logger::debug( 'Cache file created' . $removed_cookies_message );
}
}

Expand Down Expand Up @@ -473,6 +481,57 @@ public function invalidate_cache( $action = Filesystem_Utils::REBUILD_ALL ) {
return $this->invalidate_cache_for_url( home_url(), $action );
}

/**
* Ignore certain cookies in the cache parameters so cached pages can be served to these visitors.
*
* @param array $parameters - The parameters with the cookies array to filter.
* @return array - The parameters with cookies removed.
*/
public function ignore_cookies( $parameters ) {
static $params = false;

if ( $params ) {
return $params;
}
donnchawp marked this conversation as resolved.
Show resolved Hide resolved

// JETPACK_BOOST_IGNORE_COOKIES is a comma-separated list of cookies to ignore.
if ( defined( 'JETPACK_BOOST_IGNORE_COOKIES' ) ) {
$cookies = explode( ',', constant( 'JETPACK_BOOST_IGNORE_COOKIES' ) );
$cookies = array_map( 'trim', $cookies );
} else {
$cookies = array();
}
donnchawp marked this conversation as resolved.
Show resolved Hide resolved

/**
* Filters the browser cookies so cached pages can be served to these visitors.
*
* @since $$next-version$$
*
* @param array $cookies An array of cookie names to remove from the cookie list.
*/
$cookies = apply_filters(
'jetpack_boost_ignore_cookies',
array_merge(
$cookies,
array( 'cf_clearance', 'cf_chl_rc_i', 'cf_chl_rc_ni', 'cf_chl_rc_m', '_cfuvid', '__cfruid', '__cfwaitingroom', 'cf_ob_info', 'cf_use_ob', '__cfseq', '__cf_bm', '__cflb', 'sbsj_' )
donnchawp marked this conversation as resolved.
Show resolved Hide resolved
donnchawp marked this conversation as resolved.
Show resolved Hide resolved
)
);
donnchawp marked this conversation as resolved.
Show resolved Hide resolved

foreach ( $cookies as $cookie ) {
if ( isset( $parameters['cookies'][ $cookie ] ) ) {
unset( $parameters['cookies'][ $cookie ] );
$this->removed_cookies .= $cookie . ',';
}
}
if ( $this->removed_cookies !== '' ) {
$this->removed_cookies = rtrim( $this->removed_cookies, ',' );
}

$params = $parameters;

return $parameters;
}

public function disable_caching_on_error( $message ) {
if ( ! defined( 'DONOTCACHEPAGE' ) ) {
define( 'DONOTCACHEPAGE', true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function get_uri() {
}

public function get_parameters() {
return $this->request_parameters;
return apply_filters( 'jetpack_boost_cache_parameters', $this->request_parameters );
donnchawp marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -84,7 +84,8 @@ public function is_url_excluded( $request_uri = '' ) {
}

// Check if the query parameters `jb-disable-modules` or `jb-generate-critical-css` exist.
$query_params = isset( $this->request_parameters['get'] ) ? $this->request_parameters['get'] : array();
$request_parameters = $this->get_parameters();
$query_params = isset( $request_parameters['get'] ) ? $request_parameters['get'] : array();
if ( isset( $query_params ) &&
( isset( $query_params['jb-disable-modules'] ) || isset( $query_params['jb-generate-critical-css'] ) )
) {
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/boost/changelog/update-boost-cookie_filter
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Cache: filter cookies so they do not cause a cache miss.
dilirity marked this conversation as resolved.
Show resolved Hide resolved
Loading