Improve robustness of page cache test for Site Health#10855
Improve robustness of page cache test for Site Health#10855westonruter wants to merge 20 commits into
Conversation
This commit updates the page cache detection in Site Health by: - Using a stricter regex (\bhit\b) for detecting cache hits in response headers, preventing false positives (e.g., 'shit'). - Adding support for the X-Varnish header, identifying a cache hit when it contains two request IDs. - Adding documentation and source links for the supported headers. - Adding PHPUnit test cases to verify the new detection logic. Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
The x-srcache-store-status header uses 'STORE' or 'BYPASS', not 'HIT' or 'MISS'. This update ensures that 'STORE' is correctly identified as a positive page cache indicator. Also includes: - Documentation for srcache headers. - Explicit list of generic caching proxies (Squid, Go, Fastly, LiteSpeed) in comments. - Test cases for x-srcache-store-status and x-srcache-fetch-status. Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Gemini review: I have completed the review of the changes committed to the Commit ContextThe changes aim to improve the robustness of page cache detection in WordPress Site Health. Key improvements include:
Code Review
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| 'x-cache-disabled' => static function ( $header_value ) { | ||
| return ( 'on' !== strtolower( $header_value ) ); | ||
| }, | ||
| 'x-srcache-store-status' => $cache_hit_callback, |
There was a problem hiding this comment.
This was incorrect. Now fixed.
| * @covers ::get_page_cache_headers() | ||
| * @covers ::check_for_page_caching() | ||
| */ | ||
| public function test_get_page_cache( $responses, $expected_status, $expected_label, $good_basic_auth = null, $delay_the_response = false ) { |
There was a problem hiding this comment.
Note that $good_basic_auth was previously only either null or false, so this is now changed to be an actual boolean with true or false.
dmsnell
left a comment
There was a problem hiding this comment.
sorry I can’t give a more thorough review at the moment. it’s nice seeing iteration on the screen.
| ), | ||
| 'expected_status' => 'good', | ||
| 'expected_label' => $good_label, | ||
| ), |
There was a problem hiding this comment.
because I’m not familiar with all of these systems, and don’t have any in front of me, I am not reviewing or verifying these tests.
I’m trusting that you confirmed that the responses correspond to what those projects actually produce and that the values here are factually representative.
the CommonCrawl archive would potentially be a valuable archive to mine for this kind of header behavior.
Co-authored-by: Dennis Snell <dennis.snell@automattic.com>
… value Co-authored-by: Dennis Snell <dennis.snell@automattic.com>
Co-authored-by: Dennis Snell <dennis.snell@automattic.com>
| /** | ||
| * Custom caching headers. | ||
| * | ||
| * These do not seem to be actually used by any caching layers. There were first introduced in a Site Health | ||
| * test in the AMP plugin. They were copied into the Performance Lab plugin's Site Health test before they | ||
| * were merged into core. | ||
| * | ||
| * @link https://github.com/ampproject/amp-wp/pull/6849 | ||
| * @link https://github.com/WordPress/performance/pull/263 | ||
| * @link https://core.trac.wordpress.org/changeset/54043 | ||
| */ | ||
| 'x-cache-enabled' => static function ( $header_value ) { | ||
| return 'true' === strtolower( $header_value ); | ||
| return ( 'true' === strtolower( $header_value ) ); | ||
| }, | ||
| 'x-cache-disabled' => static function ( $header_value ) { | ||
| return ( 'on' !== strtolower( $header_value ) ); | ||
| }, |
There was a problem hiding this comment.
I'm inclined to remove these entirely since it seems they were originally introduced erroneously in the AMP plugin. I can't find any caching layers that actually use them. There are a few hundred mentions of them on GitHub, nevertheless: https://github.com/search?q=%2Fx-cache-(enabled%7Cdisabled)%2F&type=code
There was a problem hiding this comment.
🤷♂️ if they exist out there then I wouldn’t think there’s a problem leaving them in here. all we need is a single point of evidence showing they’re in use to be valuable here. maybe someone looks at one of those projects as an example and then copies the header without knowing better?
regardless, we shouldn’t need to feel obliged to foresee every possible setup, so if we have reason to believe this is a fringe header we don’t anticipate seeing, then it seems reasonable to remove them.
There was a problem hiding this comment.
I'll leave for now 👍🏻
dmsnell
left a comment
There was a problem hiding this comment.
although I’ve left some comments, I don’t see any reason to block this work. the header information is much more helpful with the comments you added 🙇♂️
| */ | ||
| 'x-cache-enabled' => static function ( $header_value ) { | ||
| return 'true' === strtolower( $header_value ); | ||
| return ( 'true' === strtolower( $header_value ) ); |
There was a problem hiding this comment.
what’s the purpose of the added parentheses?
There was a problem hiding this comment.
Just improved readability, perhaps, to avoid going down a garden path to think it returns the string 'true'. Also, it makes it consistent with the subsequent check.
| /** | ||
| * Custom caching headers. | ||
| * | ||
| * These do not seem to be actually used by any caching layers. There were first introduced in a Site Health | ||
| * test in the AMP plugin. They were copied into the Performance Lab plugin's Site Health test before they | ||
| * were merged into core. | ||
| * | ||
| * @link https://github.com/ampproject/amp-wp/pull/6849 | ||
| * @link https://github.com/WordPress/performance/pull/263 | ||
| * @link https://core.trac.wordpress.org/changeset/54043 | ||
| */ | ||
| 'x-cache-enabled' => static function ( $header_value ) { | ||
| return 'true' === strtolower( $header_value ); | ||
| return ( 'true' === strtolower( $header_value ) ); | ||
| }, | ||
| 'x-cache-disabled' => static function ( $header_value ) { | ||
| return ( 'on' !== strtolower( $header_value ) ); | ||
| }, |
There was a problem hiding this comment.
🤷♂️ if they exist out there then I wouldn’t think there’s a problem leaving them in here. all we need is a single point of evidence showing they’re in use to be valuable here. maybe someone looks at one of those projects as an example and then copies the header without knowing better?
regardless, we shouldn’t need to feel obliged to foresee every possible setup, so if we have reason to believe this is a fringe header we don’t anticipate seeing, then it seems reasonable to remove them.
| /** | ||
| * CloudFlare. | ||
| * | ||
| * @link https://developers.cloudflare.com/cache/concepts/cache-responses/ |
Co-authored-by: Dennis Snell <dmsnell@git.wordpress.org>
…ed verification logic. - Use a more precise regular expression for identifying "HIT" statuses to avoid false positives (e.g., "no-hit"). - Add specific verification logic for Varnish's `X-Varnish` header. - Use a `null` value in the header mapping to indicate a header existence check, when no callback is supplied. - Improve documentation with links for information about certain headers. Developed in #10855 Follow-up to [61355], [54043]. Props westonruter, dmsnell. See #63748. Fixes #64370. git-svn-id: https://develop.svn.wordpress.org/trunk@61711 602fd350-edb4-49c9-b593-d223f7449a82
…ed verification logic. - Use a more precise regular expression for identifying "HIT" statuses to avoid false positives (e.g., "no-hit"). - Add specific verification logic for Varnish's `X-Varnish` header. - Use a `null` value in the header mapping to indicate a header existence check, when no callback is supplied. - Improve documentation with links for information about certain headers. Developed in WordPress/wordpress-develop#10855 Follow-up to [61355], [54043]. Props westonruter, dmsnell. See #63748. Fixes #64370. Built from https://develop.svn.wordpress.org/trunk@61711 git-svn-id: http://core.svn.wordpress.org/trunk@61019 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This improves the robustness of page cache detection in Site Health by refining the response header validation and expanding support for common caching layers.
Logic & Header Improvements
$cache_hit_callbacknow uses a word-boundary regular expression (/\bhit\b/i) instead of a simple substring check. This prevents false positives from header values likeshit. 💩X-SRCache-Store-StatusCorrection: Fixed the validation for this header to check for the correct valueSTORE(the module's indicator for a successful cache storage) instead ofHIT.X-Varnishheader, specifically identifying a hit when the header contains two request IDs (e.g.,123 456), as documented in the Varnish FAQ.srcache,Varnish) and explicitly listed supported generic proxies (Squid, Go, Fastly, LiteSpeed) in comments.PHPUnit Test Enhancements
Tests_Admin_wpSiteHealthto verify:X-Varnishhits vs. misses.X-SRCache-Store-Status(STORE) andX-SRCache-Fetch-Status(HIT).x-cache: shit).ETag,Last-Modified, andVia.Code Quality
class-wp-site-health.phpand the test suite usingcomposer formatto ensure adherence to WordPress Coding Standards.Trac ticket: https://core.trac.wordpress.org/ticket/64370
Use of AI Tooling
The above PR description was drafted by Gemini, and Gemini CLI was involved in authoring some commits (as attributed via
co-authored-by).This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.