Skip to content

Commit cad0b92

Browse files
committed
Simplify logic now that only one directive is checked
1 parent dafbc7e commit cad0b92

File tree

2 files changed

+10
-36
lines changed

2 files changed

+10
-36
lines changed

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

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function perflab_bfcache_compatibility_headers_check(): array {
2929
'color' => 'blue',
3030
),
3131
'description' => '<p>' . wp_kses(
32-
__( 'If the <code>Cache-Control</code> page response header includes directives like <code>no-store</code>, <code>no-cache</code>, or <code>max-age=0</code> then it can prevent instant back/forward navigations (using the browser bfcache). These are not present for unauthenticated requests on your site, so it is configured properly. Note that WordPress adds these directives for logged-in page responses.', 'performance-lab' ),
32+
__( "If the <code>Cache-Control</code> page response header includes the <code>no-store</code> directive then it can prevent instant back/forward navigations (using the browser's bfcache). This is not present for unauthenticated requests on your site, so it is configured properly. Note that there are other ways that bfcache can be disabled (e.g. you have JavaScript which uses a <code>unload</code> event listener). Also note that WordPress adds this directive for logged-in page responses for privacy/security reasons.", 'performance-lab' ),
3333
array( 'code' => array() )
3434
) . '</p>',
3535
'actions' => '',
@@ -66,41 +66,15 @@ function perflab_bfcache_compatibility_headers_check(): array {
6666
}
6767

6868
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' ) as $directive ) {
72-
if ( str_contains( $cache_control_header, $directive ) ) {
73-
$found_directives[] = $directive;
74-
}
75-
}
76-
77-
if ( count( $found_directives ) > 0 ) {
69+
if ( str_contains( strtolower( $cache_control_header ), 'no-store' ) ) {
7870
$result['label'] = __( 'The Cache-Control page header is preventing fast back/forward navigations', 'performance-lab' );
7971
$result['status'] = 'recommended';
8072
$result['description'] = sprintf(
81-
'<p>%s %s</p>',
73+
'<p>%s</p>',
8274
wp_kses(
83-
sprintf(
84-
/* translators: %s: problematic directive(s) */
85-
_n(
86-
'The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directive: %s.',
87-
'The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directives: %s.',
88-
count( $found_directives ),
89-
'performance-lab'
90-
),
91-
implode(
92-
', ',
93-
array_map(
94-
static function ( $header ) {
95-
return "<code>$header</code>";
96-
},
97-
$found_directives
98-
)
99-
)
100-
),
75+
__( "The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the <code>no-store</code> directive. This can affect the performance of your site by preventing fast back/forward navigations (via the browser's bfcache).", 'performance-lab' ),
10176
array( 'code' => array() )
102-
),
103-
esc_html__( 'This can affect the performance of your site by preventing fast back/forward navigations (via browser bfcache).', 'performance-lab' )
77+
)
10478
);
10579
break;
10680
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,27 @@ public function data_test_bfcache_compatibility(): array {
8282
'headers_not_set' => array(
8383
$this->build_response( 200, array( 'cache-control' => '' ) ),
8484
'good',
85-
'If the <code>Cache-Control</code> page response header includes directives like',
85+
'If the <code>Cache-Control</code> page response header includes',
8686
),
8787
'no_store' => array(
8888
$this->build_response( 200, array( 'cache-control' => 'no-store' ) ),
8989
'recommended',
90-
'<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directive: <code>no-store</code>',
90+
'<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes',
9191
),
9292
'no_cache' => array(
9393
$this->build_response( 200, array( 'cache-control' => 'no-cache' ) ),
9494
'good',
95-
'If the <code>Cache-Control</code> page response header includes directives like',
95+
'If the <code>Cache-Control</code> page response header includes',
9696
),
9797
'max_age_0' => array(
9898
$this->build_response( 200, array( 'cache-control' => 'no-cache' ) ),
9999
'good',
100-
'If the <code>Cache-Control</code> page response header includes directives like',
100+
'If the <code>Cache-Control</code> page response header includes',
101101
),
102102
'max_age_0_no_store' => array(
103103
$this->build_response( 200, array( 'cache-control' => 'max-age=0, no-store' ) ),
104104
'recommended',
105-
'<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes the following directive: <code>no-store</code>',
105+
'<p>The <code>Cache-Control</code> response header for an unauthenticated request to the home page includes',
106106
),
107107
'error' => array(
108108
new WP_Error( 'http_request_failed', 'HTTP request failed' ),

0 commit comments

Comments
 (0)