Skip to content

Commit

Permalink
Closes #6202: Stop preloading fonts when it's excluded (#6445)
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld authored and wordpressfan committed Mar 15, 2024
1 parent 0357eea commit c5f317c
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 12 deletions.
31 changes: 31 additions & 0 deletions inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,18 @@ private function add_used_fonts_preload( string $html, string $used_css ): strin
return $html;
}

/**
* Filters the list of fonts to exclude from preload
*
* @since 3.15.10
*
* @param array $excluded_fonts_preload List of fonts to exclude from preload
*/
$exclude_fonts_preload = apply_filters( 'rocket_exclude_rucss_fonts_preload', [] );
if ( ! is_array( $exclude_fonts_preload ) ) {
$exclude_fonts_preload = [];
}

$urls = [];

foreach ( $font_faces as $font_face ) {
Expand All @@ -483,6 +495,25 @@ private function add_used_fonts_preload( string $html, string $used_css ): strin
continue;
}

// Making sure the excluded fonts array isn't empty to avoid excluding all fonts.
if ( ! empty( $exclude_fonts_preload ) ) {
// Combine the array elements into a single string with | as a separator and returning a pattern.
$exclude_fonts_preload_pattern = implode(
'|',
array_map(
function ( $item ) {
return is_string( $item ) ? preg_quote( $item, '/' ) : '';
},
$exclude_fonts_preload
)
);

// Check if the font URL matches any part of the exclude_fonts_preload array.
if ( ! empty( $exclude_fonts_preload_pattern ) && preg_match( '/' . $exclude_fonts_preload_pattern . '/i', $font_url ) ) {
continue; // Skip this iteration as the font URL is in the exclusion list.
}
}

$urls[] = $font_url;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@font-face {
font-family: 'MyFont';
src: url('https://domain.com/fonts/MyFont.woff') format('woff');
}

body {
font-family: 'MyFont';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@font-face {
font-family: 'MyFontExcluded';
src: url('https://domaina.com/fonts/MyFontExcluded.woff') format('woff');
}

@font-face {
font-family: 'MyFont';
src: url('https://domain.com/fonts/MyFont.woff') format('woff');
}

body {
font-family: 'MyFont';
}

p {
font-family: 'MyFontExcluded';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title><link rel="preload" as="font" href="https://domain.com/fonts/MyFont.woff" crossorigin><style id="wpr-usedcss">@font-face {
font-family: 'MyFontExcluded';
src: url('https://domaina.com/fonts/MyFontExcluded.woff') format('woff');
}

@font-face {
font-family: 'MyFont';
src: url('https://domain.com/fonts/MyFont.woff') format('woff');
}

body {
font-family: 'MyFont';
}

p {
font-family: 'MyFontExcluded';
}
</style>
</head>
<body>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title><link rel="preload" as="font" href="https://domain.com/fonts/MyFont.woff" crossorigin><style id="wpr-usedcss">@font-face {
font-family: 'MyFont';
src: url('https://domain.com/fonts/MyFont.woff') format('woff');
}

body {
font-family: 'MyFont';
}
</style>
</head>
<body>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

$html_input = file_get_contents(__DIR__ . '/HTML/input.html');
$html_output = file_get_contents(__DIR__ . '/HTML/output.html');
$html_output_font_excluded = file_get_contents(__DIR__ . '/HTML/outputFontExcluded.html');
$html_output_font_preloaded = file_get_contents(__DIR__ . '/HTML/outputFontPreloaded.html');


return [
Expand All @@ -15,8 +17,8 @@
],
'files' => [

]

],
'font_excluded' => [],
],
'expected' => [
'html' => $html_output,
Expand Down Expand Up @@ -51,7 +53,8 @@
],
'files' => [

]
],
'font_excluded' => [],

],
'expected' => [
Expand All @@ -68,6 +71,120 @@
],
'files' => [

]
]
],
'shouldNotPreloadFontExcluded' => [
'config' => [
'rucss' => true,
'html' => $html_input,
'rows' => [
[
'url' => 'http://example.org',
'job_id' => '1234',
'queue_name' => 'eu',
'is_mobile' => false,
'status' => 'completed',
'retries' => 0,
'hash' => '1234abcd',
]
],
'font_excluded' => ['https://domaina.com'],
'files' => [
'wp-content/cache/used-css/1/1/2/3/4abcd.css.gz' => gzencode( file_get_contents(__DIR__ . '/CSS/test.css') )
],
],
'expected' => [
'html' => $html_output_font_excluded,
'rows' => [
[
'url' => 'http://example.org',
'job_id' => '1234',
'queue_name' => 'eu',
'is_mobile' => false,
'status' => 'completed',
'retries' => 0,
'hash' => '1234abcd',
]
],
'files' => [

]
]
],
'shouldPreloadFont' => [
'config' => [
'rucss' => true,
'html' => $html_input,
'rows' => [
[
'url' => 'http://example.org',
'job_id' => '1234',
'queue_name' => 'eu',
'is_mobile' => false,
'status' => 'completed',
'retries' => 0,
'hash' => '1234abcd',
]
],
'font_excluded' => [],
'files' => [
'wp-content/cache/used-css/1/1/2/3/4abcd.css.gz' => gzencode( file_get_contents(__DIR__ . '/CSS/FontPreloaded.css') )
],
],
'expected' => [
'html' => $html_output_font_preloaded,
'rows' => [
[
'url' => 'http://example.org',
'job_id' => '1234',
'queue_name' => 'eu',
'is_mobile' => false,
'status' => 'completed',
'retries' => 0,
'hash' => '1234abcd',
]
],
'files' => [

]
]
],
'shouldPreloadFontEvenWithEmptyExcludeArray' => [
'config' => [
'rucss' => true,
'html' => $html_input,
'rows' => [
[
'url' => 'http://example.org',
'job_id' => '1234',
'queue_name' => 'eu',
'is_mobile' => false,
'status' => 'completed',
'retries' => 0,
'hash' => '1234abcd',
]
],
'font_excluded' => [''],
'files' => [
'wp-content/cache/used-css/1/1/2/3/4abcd.css.gz' => gzencode( file_get_contents(__DIR__ . '/CSS/FontPreloaded.css') )
],
],
'expected' => [
'html' => $html_output_font_preloaded,
'rows' => [
[
'url' => 'http://example.org',
'job_id' => '1234',
'queue_name' => 'eu',
'is_mobile' => false,
'status' => 'completed',
'retries' => 0,
'hash' => '1234abcd',
]
],
'files' => [

]
]
],
Expand Down
3 changes: 3 additions & 0 deletions tests/Integration/DBTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public static function addResource(array $resource) {
if(key_exists('status', $resource) && 'pending' === $resource['status']) {
$resource_query->make_status_pending($job_id);
}
if(key_exists('status', $resource) && 'completed' === $resource['status']) {
$resource_query->make_status_completed($job_id, $resource['hash']);
}
return $job_id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use WP_Rocket\Tests\Integration\TestCase;

/**
* @group RUCSS
* @covers \WP_Rocket\Engine\Optimization\RUCSS\Frontend\Subscriber::treeshake
*/
class Test_treeshake extends FilesystemTestCase {
class Test_treeshake extends FilesystemTestCase
{

use DBTrait;

Expand All @@ -33,21 +35,29 @@ public function set_up()
{
parent::set_up();
add_filter('pre_get_rocket_option_remove_unused_css', [$this, 'rucss']);
add_filter('rocket_exclude_rucss_fonts_preload', [$this, 'exclude_fonts_preload']);
add_filter('rocket_used_css_dir_level', [$this, 'used_css_dir_level']);
}

public function tear_down()
{
remove_filter('pre_get_rocket_option_remove_unused_css', [$this, 'rucss']);
remove_filter('rocket_exclude_rucss_fonts_preload', [$this, 'exclude_fonts_preload']);
remove_filter('rocket_used_css_dir_level', [$this, 'used_css_dir_level']);

parent::tear_down();
}

/**
* @dataProvider providerTestData
*/
public function testShouldReturnAsExpected( $config, $expected )
{
/**
* @dataProvider providerTestData
*/
public function testShouldReturnAsExpected($config, $expected)
{
$this->config = $config;

foreach ($config['files'] as $path => $file) {
rocket_mkdir_p(dirname($path), $this->filesystem);
$this->filesystem->put_contents($path, $file);
}
foreach ($config['rows'] as $row) {
self::addResource($row);
}
Expand All @@ -57,9 +67,19 @@ public function testShouldReturnAsExpected( $config, $expected )
foreach ($expected['rows'] as $row) {
$this->assertTrue(self::resourceFound($row), json_encode($row) . ' not found');
}
}
}

public function rucss() {
public function rucss()
{
return $this->config['rucss'];
}

public function exclude_fonts_preload()
{
return $this->config['font_excluded'];
}

public function used_css_dir_level() {
return 3;
}
}

0 comments on commit c5f317c

Please sign in to comment.