wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
702 stars 219 forks source link

Closes #7066: 3.18 - Preconnect and prefetch removal #7101

Closed Miraeld closed 1 week ago

Miraeld commented 1 week ago

Description

https://github.com/wp-media/wp-rocket/pull/7091 needs to be merged first. This PR is based on the current build of this PR. Only the last two commits are for this PR.

Fixes #7066 We are removing the prefetch and preconnect tag from the html if we are serving local hosted google fonts.

Type of change

Detailed scenario

  1. Enable the Host Local Font option under the Media tab of WPR settings
  2. Load a page with google fonts and prefetch/preconnect tags
  3. Check the HTML, both of these tags should be removed.

Technical description

Documentation

This enhancement introduces a method to remove preconnect and prefetch links for Google Fonts from the HTML content. The purpose of this change is to optimize the loading process by eliminating unnecessary DNS prefetch and preconnect requests for Google Fonts.

How It Works

Method Introduction: A new private method remove_preconnect_and_prefetch is added to the Controller class.

HTML Content Check: The method first checks if the provided HTML content is empty. If it is, the original HTML is returned.

Filter Application: A filter rocket_remove_font_pre_links is applied to determine whether the preconnect and prefetch links should be removed. If the filter returns false, the original HTML is returned.

Pattern Matching: A regular expression pattern is used to match preconnect and prefetch links for Google Fonts.

Tags Removal: The matched tags are removed from the HTML content using preg_replace.

Return Modified HTML: The modified HTML content, without the preconnect and prefetch links, is returned.

New dependencies

None

Risks

None

Mandatory Checklist

Code validation

Code style

codacy-production[bot] commented 1 week ago

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
Report missing for 4160df17680a0cb36febcb4aa55261d9e00ea9e4[^1] :white_check_mark: 56.64% (target: 50.00%)
Coverage variation details | | Coverable lines | Covered lines | Coverage | | ------------- | ------------- | ------------- | ------------- | | Common ancestor commit (4160df17680a0cb36febcb4aa55261d9e00ea9e4) | Report Missing | Report Missing | Report Missing | | | Head commit (a417d688f0bc146d23b5421ae7d8dbfff0e3e8c8) | 38239 | 16768 | 43.85% | **Coverage variation** is the difference between the coverage for the head and common ancestor commits of the pull request branch: ` - `
Diff coverage details | | Coverable lines | Covered lines | Diff coverage | | ------------- | ------------- | ------------- | ------------- | | Pull request (#7101) | 113 | 64 | **56.64%** | **Diff coverage** is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: `/ * 100%`

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more [^1]: Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.

codacy-production[bot] commented 1 week ago

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
Report missing for 6fcdd7acfdf847716ba6810cc83330a9cddfc7be[^1] :white_check_mark: 85.71% (target: 50.00%)
Coverage variation details | | Coverable lines | Covered lines | Coverage | | ------------- | ------------- | ------------- | ------------- | | Common ancestor commit (6fcdd7acfdf847716ba6810cc83330a9cddfc7be) | Report Missing | Report Missing | Report Missing | | | Head commit (9e97dbbb457643419f9965f840d073f5c609e252) | 38237 | 16767 | 43.85% | **Coverage variation** is the difference between the coverage for the head and common ancestor commits of the pull request branch: ` - `
Diff coverage details | | Coverable lines | Covered lines | Diff coverage | | ------------- | ------------- | ------------- | ------------- | | Pull request (#7101) | 7 | 6 | **85.71%** | **Diff coverage** is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: `/ * 100%`

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more [^1]: Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.

jeawhanlee commented 1 week ago

I tested that this works as expected.