wp-rocket
wp-rocket copied to clipboard
Closes #6202: Stop preloading fonts when it's excluded
Description
This PR gives users a filter to be able to exclude fonts urls to be preloaded.
Fixes #6202
Documentation
User documentation
With this PR, you now have the ability to control which fonts get preloaded on your website. This is done using the rocket_exclude_rucss_fonts_preload
filter.
Let's say your website uses two fonts: one from https://domainA.com/fonts/myFontA.woff
and another from https://domainB.com/fonts/myFontsB.woff
. If you want to prevent the font from domainA.com
from being preloaded, you can use the filter and return ['https://domainA.com']
. This means that myFontA.woff
won't be preloaded, but myFontB.woff
will be.
This gives you more control over the performance of your website.
Technical documentation
We apply a filter to get the excluded fonts urls, and in the loop we check if the current url is within the excluded array. If that's the case we bail-out.
Type of change
- [x] Enhancement (non-breaking change which improves an existing functionality).
Checklists
Feature validation
- [x] I validated all the Acceptance Criteria. If possible, provide sreenshots or videos.
- [x] I triggered all changed lines of code at least once without new errors/warnings/notices.
- [x] I added tests
Code style
- [x] I wrote self-explanatory code about what it does.
- [x] I wrote comments to explain why it does it.
- [x] I named variables and functions explicitely.
- [x] I protected entry points against unexpected inputs.
- [x] I did not introduce unecessary complexity.
- [x] I listed the introduced external dependencies explicitely on the PR.
- [x] I validated the repo-specific guidelines from CONTRIBUTING.md.
@Miraeld a few feedbacks:
- I implemented built-in tests to cover the new/changed code. -> Seems that it disappeared from the checklist? I think new hooks should always have basic test coverage, no?
- About the user documentation, what a user or the support team would need is:
- the name of the filter
- what data to pass (maybe with one example?) Providing this directly within the PR would ease future work to build the release note and update the documentation. (cc @piotrbak :D )
@MathieuLamiot , I've added integrations tests to the PR and modified the User documentation. :)
@Miraeld Thanks for the PR. While doing exploratory testing, can see that used font in used CSS isnot preloaded by default. Can you please check?
@Mai-Saad,
The issue was imploding an empty array gives an empty string. And it would match everything in the preg_match
here.
It's been fixed now as I've added a check if it's empty.
And I also added another check in case we passe ['']
to the filter, and empty([''])
wouldn't be true. So the same issue would occur.
Related test plan https://wpmediaqa.testrail.io/index.php?/runs/view/817&group_by=cases:section_id&group_order=asc
Testing in Progress
@Miraeld Thanks For the PR Please find points below while executing test plan
- When we enable CDN rewriting, we will only rewrite to point to the CDN if the font style is having a relative url but if we have an absolute url, it will be ignored. e.g /wp-content/rocket-test-data/fonts/ballet-v3-latin-regular.woff2 -> cdn.example.com/wp-content/rocket-test-data/fonts/ballet-v3-latin-regular.woff2 but example.com/wp-content/rocket-test-data/fonts/ballet-v3-latin-regular.woff2 will remain the same. This is not a regression as it is already on trunk. @piotrbak @Mai-Saad Do we need to handle this?
- Adding a null value to the array to be returned in this filter is throwing a fatal error in debug.log.
add_filter( 'rocket_exclude_rucss_fonts_preload', function( $fonts ) {
$fonts[]='woocommerce/assets/fonts';
$fonts[]=null;
return $fonts;
} );
PHP Fatal error: Uncaught TypeError: preg_quote() expects parameter 1 to be string, null given in /home/test/wp-content/plugins/wp-rocket/inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php:829
We need to guard this here: https://github.com/wp-media/wp-rocket/blob/66daba4d57e5f576ae3450c947bc0cdab528cce2/inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php#L829
Umm, For the 1st point, I've noticed it, but as it is on trunk, I supposed it was normal.
For the 2nd point, it's fixed in the latest commit.
The fatal error is fixed and all test plan passed. Opened a new issue for point 1 above here