-
Notifications
You must be signed in to change notification settings - Fork 139
Skip stylesheets with empty href attributes in Site Health audit #2281
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
Skip stylesheets with empty href attributes in Site Health audit #2281
Conversation
Updated the check for the href attribute in perflab_aea_audit_blocking_assets to skip processing when href is an empty string, preventing potential issues with invalid asset URLs.
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## trunk #2281 +/- ##
==========================================
+ Coverage 68.85% 68.87% +0.02%
==========================================
Files 90 90
Lines 7612 7618 +6
==========================================
+ Hits 5241 5247 +6
Misses 2371 2371
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
plugins/performance-lab/includes/site-health/audit-enqueued-assets/helper.php
Outdated
Show resolved
Hide resolved
|
Thank you for the PR. Let's also update the unit test to add an assertion for this change. |
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Introduces a test case to verify that styles enqueued with an empty href are handled correctly. This addresses issue WordPress#2278 and ensures that such styles are not included in the audited assets.
Test AddedI've added test coverage for the empty What the test does:
Test assertion:$empty_href_style = array_filter(
$assets['styles'],
static function ( $item ) {
return 'https://empty-href-style.com' === $item['src'];
}
);
$this->assertEmpty( $empty_href_style );This ensures that themes/plugins using performance optimization techniques with href="" won't trigger false positives in Site Health checks. Thanks to @westonruter for the valuable feedback on implementing this test case! Files changed: |
Corrects whitespace in the test for enqueued assets and adds a docblock line for clarity. No functional changes to the test logic.
| $tag = str_replace( | ||
| 'href=\'https://empty-href-style.com\'', | ||
| "href=''", | ||
| $tag | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the HTML Tag Processor for this as it is more robust than doing a replacement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 65b2746
… stylesheets-with-empty-href-attributes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function ( $tag, $handle ) { | ||
| if ( 'style-no-href' === $handle ) { | ||
| $tag = str_replace( 'href=\'https://no-href-style.com\'', ' ', $tag ); | ||
| $processor = new WP_HTML_Tag_Processor( $tag ); | ||
| $this->assertTrue( $processor->next_tag( 'LINK' ), 'Expected a LINK to be present.' ); | ||
| $this->assertSame( 'stylesheet', $processor->get_attribute( 'rel' ), 'Expected LINK to be a stylesheet.' ); | ||
| $processor->remove_attribute( 'href' ); | ||
| $tag = $processor->get_updated_html(); | ||
| } elseif ( 'style-empty-href' === $handle ) { | ||
| $processor = new WP_HTML_Tag_Processor( $tag ); | ||
| $this->assertTrue( $processor->next_tag( 'LINK' ), 'Expected a LINK to be present.' ); | ||
| $this->assertSame( 'stylesheet', $processor->get_attribute( 'rel' ), 'Expected LINK to be a stylesheet.' ); | ||
| $processor->set_attribute( 'href', '' ); | ||
| $tag = $processor->get_updated_html(); | ||
| } | ||
| return $tag; | ||
| }, |
Copilot
AI
Jan 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change from static function to function is necessary here because the callback now uses $this to access test assertion methods (lines 184-185, 190-191). However, this pattern is inconsistent with other filter callbacks in this test file, which all use static function (e.g., lines 62, 232, 240, 248, 256, 264).
While the test assertions inside the filter callback can help catch issues earlier, they create coupling between the filter and test context and break the established pattern. Consider either:
- Removing the assertions from inside the filter callback (they're not strictly necessary since the test verifies the end result), or
- If assertions are needed for debugging, extract the HTML manipulation logic into a helper method that can be called with $this context while keeping the assertions, making the pattern more explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, thanks.
...formance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets.php
Outdated
Show resolved
Hide resolved
plugins/performance-lab/includes/site-health/audit-enqueued-assets/helper.php
Outdated
Show resolved
Hide resolved
...formance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
plugins/performance-lab/includes/site-health/audit-enqueued-assets/helper.php
Show resolved
Hide resolved
plugins/performance-lab/includes/site-health/audit-enqueued-assets/helper.php
Show resolved
Hide resolved
...formance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets.php
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...formance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets.php
Outdated
Show resolved
Hide resolved
...formance-lab/tests/includes/site-health/audit-enqueued-assets/test-audit-enqueued-assets.php
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
What:
Skip stylesheet link elements that have empty
hrefattributes during the Enqueued Assets Health Check audit.Why:
Some themes and plugins (like Mesmerize Companion) intentionally use empty
hrefattributes withdata-hrefattributes as a performance optimization technique to defer CSS loading. These stylesheets are non-render-blocking by design, but were being incorrectly flagged as errors ("A valid URL was not provided") in the Site Health Status, negatively impacting site scores.How:
Added a check for empty string
hrefvalues inperflab_aea_audit_blocking_assets(). The condition now reads:This skips stylesheets with href="" from being audited, similar to how async CSS with non-standard media attributes is already excluded.
Files changed:
helper.php
Fixes #2278