Skip to content

Commit

Permalink
Closes #7066: 3.18 - Preconnect and prefetch removal (#7101)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Lee <michaelleemichaellee408@gmail.com>
  • Loading branch information
Miraeld and jeawhanlee authored Nov 15, 2024
1 parent 9a55086 commit a6e21f5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
31 changes: 31 additions & 0 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function rewrite_fonts( string $html ): string {
$html = $this->replace_font( $font, $html );
}

$html = $this->remove_preconnect_and_prefetch( $html );

return $html;
}

Expand Down Expand Up @@ -111,6 +113,35 @@ protected function get_optimized_markup( string $hash, string $original_url ): s
);
}

/**
* Removes preconnect and prefetch links for Google Fonts from the HTML content.
*
* @param string $html HTML content.
*
* @return string Modified HTML content without preconnect and prefetch links.
*/
private function remove_preconnect_and_prefetch( string $html ) {
/**
* Filters the removal of Google preconnect/prefetch links.
*
* @since 3.18
*
* @param bool $enable_removal Enable or disable removal of Google preconnect/prefetch links.
*/
$remove_links = wpm_apply_filters_typed( 'boolean', 'rocket_remove_font_pre_links', true );

if ( ! $remove_links ) {
return $html;
}

$pattern = '/<link(?:[^>]*)(?:rel=["\'](?:dns-prefetch|preconnect)["\'])(?:[^>]*)(?:href=["\'](?:https?:)?\/\/(?:fonts\.(?:googleapis|gstatic)\.com)["\'])(?:[^>]*)>/i';

$html = preg_replace( $pattern, '', $html );

return $html;
}

/**
* Disables the preload of Google Fonts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V1 Template</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<style>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V1 and V2 Template</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css?family=Roboto|Open+Sans" rel="stylesheet"> <!-- V1 Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Montserrat:wght@400;700&display=swap" rel="stylesheet"> <!-- V2 Fonts -->
<style>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V2 Template</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&display=swap" rel="stylesheet">
<style>
Expand Down

0 comments on commit a6e21f5

Please sign in to comment.