Fix #7417: Preload fonts external domains exclusions#63
Conversation
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the external font processing logic in the BeaconPreloadFonts class from an allowlist-based approach to an exclusion-based approach, addressing issue #7417. The change enables processing of all external CSS links from different origins unless they are explicitly excluded through the new external_font_exclusions configuration.
- Replaced hardcoded allowlist of font providers with configurable exclusion-based filtering
- Added origin validation to ensure only external domains are processed
- Enhanced test coverage with origin mocking and exclusion-based test scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/BeaconPreloadFonts.js |
Replaced allowlist filtering with exclusion-based logic and added origin validation for external CSS links |
test/BeaconPreloadFonts.test.js |
Added external_font_exclusions config property and comprehensive tests for origin filtering and exclusion behavior |
Description
Fixes #7417 from WP Rocket repo
Refer to wp-media/wp-rocket#7519
Type of change
Detailed scenario
What was tested
Implemented test scenarios:
✅ External CSS links from different origins (e.g.,
fonts.googleapis.com) are correctly identified and processed✅ Same-origin CSS links are properly excluded from external font processing
✅ Proper
window.location.hrefmocking ensures accurate origin comparison in test environment2. Exclusion-Based Configuration
✅ Links matching
external_font_exclusionspatterns are correctly filtered out✅ Empty exclusion arrays default to processing all external domains
✅ Substring matching works correctly for exclusion patterns
How to test
Refer to wp-media/wp-rocket#7519
Technical description
Documentation
This pull request refactors the logic for processing external font stylesheets in the
BeaconPreloadFontsclass, replacing the allowlist-based approach with an exclusion-based approach. It also updates the test suite to ensure the new behavior is properly validated. The most important changes include the introduction of theexternal_font_exclusionsconfiguration, enhancements to origin checks for external links, and the addition of new test cases to cover the updated functionality.Updates to font processing logic:
external_font_exclusionsconfiguration. This change ensures that all external CSS links are processed unless explicitly excluded. (src/BeaconPreloadFonts.js, src/BeaconPreloadFonts.jsL261-R280)Enhancements to origin checks:
src/BeaconPreloadFonts.js, src/BeaconPreloadFonts.jsL261-R280)Test suite updates:
external_font_exclusionsproperty in the test configuration to validate the exclusion-based filtering mechanism. (test/BeaconPreloadFonts.test.js, test/BeaconPreloadFonts.test.jsL37-R38)external_font_exclusionsconfiguration and that only links from different origins are processed. (test/BeaconPreloadFonts.test.js, test/BeaconPreloadFonts.test.jsR1232-R1267)window.locationand theURLconstructor in tests to simulate origin checks and ensure accurate comparisons. (test/BeaconPreloadFonts.test.js, [1] [2]New dependencies
None
Risks
None
Mandatory Checklist
Code validation
Code style