Skip to content

Commit b65be42

Browse files
committed
Improve logic for checking directives
1 parent 96eac80 commit b65be42

File tree

1 file changed

+11
-14
lines changed
  • plugins/performance-lab/includes/site-health/bfcache-compatibility-headers

1 file changed

+11
-14
lines changed

plugins/performance-lab/includes/site-health/bfcache-compatibility-headers/helper.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,22 @@ function perflab_bfcache_compatibility_headers_check(): array {
5959
return $result;
6060
}
6161

62-
$cache_control_header = wp_remote_retrieve_header( $response, 'cache-control' );
63-
if ( is_string( $cache_control_header ) && '' === $cache_control_header ) {
62+
$cache_control_headers = wp_remote_retrieve_header( $response, 'cache-control' );
63+
if ( '' === $cache_control_headers ) {
6464
// The Cache-Control header is not set, so it does not prevent bfcache. Return the default result.
6565
return $result;
6666
}
6767

68-
$cache_control_header = (array) $cache_control_header;
69-
foreach ( $cache_control_header as $header ) {
70-
$header = strtolower( $header );
71-
$headers_to_check = array( 'no-store', 'no-cache', 'max-age=0' );
72-
$flagged_headers = array();
73-
74-
foreach ( $headers_to_check as $header_to_check ) {
75-
if ( is_int( strpos( $header, $header_to_check ) ) ) {
76-
$flagged_headers[] = $header_to_check;
68+
foreach ( (array) $cache_control_headers as $cache_control_header ) {
69+
$cache_control_header = strtolower( $cache_control_header );
70+
$found_directives = array();
71+
foreach ( array( 'no-store', 'no-cache', 'max-age=0' ) as $directive ) {
72+
if ( str_contains( $cache_control_header, $directive ) ) {
73+
$found_directives[] = $directive;
7774
}
7875
}
7976

80-
if ( count( $flagged_headers ) > 0 ) {
77+
if ( count( $found_directives ) > 0 ) {
8178
$result['label'] = __( 'The Cache-Control page header is preventing fast back/forward navigations', 'performance-lab' );
8279
$result['status'] = 'recommended';
8380
$result['description'] = sprintf(
@@ -88,7 +85,7 @@ function perflab_bfcache_compatibility_headers_check(): array {
8885
_n(
8986
'The <code>Cache-Control</code> response header for the home page includes the following directive: %s.',
9087
'The <code>Cache-Control</code> response header for the home page includes the following directives: %s.',
91-
count( $flagged_headers ),
88+
count( $found_directives ),
9289
'performance-lab'
9390
),
9491
implode(
@@ -97,7 +94,7 @@ function perflab_bfcache_compatibility_headers_check(): array {
9794
static function ( $header ) {
9895
return "<code>$header</code>";
9996
},
100-
$flagged_headers
97+
$found_directives
10198
)
10299
)
103100
),

0 commit comments

Comments
 (0)