Skip to content

Commit

Permalink
Fixes #6590 Add LCP/ATF warmup for mobile version (#6600)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Lee <[email protected]>
Co-authored-by: Michael Lee <[email protected]>
Co-authored-by: Gael Robin <[email protected]>
Co-authored-by: WordPress Fan <[email protected]>
Co-authored-by: Opeyemi Ibrahim <[email protected]>
Co-authored-by: Mathieu Lamiot <[email protected]>
Co-authored-by: WordPressFan <[email protected]>
  • Loading branch information
8 people authored May 2, 2024
1 parent 67b06ab commit 74186d3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion assets/js/wpr-admin.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class APIClient extends BaseAPIClient {
* Send the link to Above the fold SaaS.
*
* @param string $url Url to be sent.
* @param string $device Device type.
*
* @return array
*/
public function add_to_atf_queue( string $url ): array {
public function add_to_atf_queue( string $url, $device = 'desktop' ): array {
$is_home = Utils::is_home( $url );

$url = add_query_arg(
Expand All @@ -27,6 +29,7 @@ public function add_to_atf_queue( string $url ): array {
$config = [
'optimization_list' => '',
'is_home' => $is_home,
'is_mobile' => 'mobile' === $device,
];

return $this->add_to_queue( $url, $config );
Expand Down
33 changes: 28 additions & 5 deletions inc/Engine/Media/AboveTheFold/WarmUp/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,34 @@ public function warm_up(): void {
return;
}

if ( $this->is_mobile() ) {
$this->send_to_saas( $this->fetch_links( 'mobile' ), 'mobile' );
}

$this->send_to_saas( $this->fetch_links() );
}

/**
* Fetch links from homepage.
*
* @param string $device Device type.
*
* @return array
*/
public function fetch_links(): array {
public function fetch_links( $device = 'desktop' ): array {

Check warning on line 84 in inc/Engine/Media/AboveTheFold/WarmUp/Controller.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/AboveTheFold/WarmUp/Controller.php#L84

The method fetch_links() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.

Check warning on line 84 in inc/Engine/Media/AboveTheFold/WarmUp/Controller.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/AboveTheFold/WarmUp/Controller.php#L84

The method fetch_links() has an NPath complexity of 1152. The configured NPath complexity threshold is 200.
if ( $this->user->is_license_expired_grace_period() ) {
return [];
}

$user_agent = 'WP Rocket/Pre-fetch Home Links';

if ( 'mobile' === $device ) {
$user_agent = 'WP Rocket/Pre-fetch Home Links Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';
}

$home_url = home_url();
$args = [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'user-agent' => $user_agent,
'timeout' => 60,
];

Expand Down Expand Up @@ -168,10 +180,12 @@ function ( $link ) use ( $home_url, $reject_uri_pattern ) {
/**
* Send fetched links to SaaS to do the warmup.
*
* @param array $links Array of links to be sent.
* @param array $links Array of links to be sent.
* @param string $device Device type.
*
* @return void
*/
private function send_to_saas( $links ) {
private function send_to_saas( $links, $device = 'desktop' ) {
if ( empty( $links ) ) {
return;
}
Expand All @@ -186,7 +200,7 @@ private function send_to_saas( $links ) {
$delay_between = (int) apply_filters( 'rocket_delay_between_requests', 500000 );

foreach ( $links as $link ) {
$this->api_client->add_to_atf_queue( $link );
$this->api_client->add_to_atf_queue( $link, $device );

usleep( $delay_between );
}
Expand All @@ -211,4 +225,13 @@ public function add_wpr_imagedimensions_query_arg( string $url ): string {
$url
);
}

/**
* Check if the current request is for mobile.
*
* @return bool
*/
private function is_mobile(): bool {
return $this->options->get( 'cache_mobile', 0 ) && $this->options->get( 'do_caching_mobile_files', 0 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'shouldReturnEmptyWhenLicenseExpired' => [
'config' => [
'license_expired' => true,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -29,6 +30,7 @@
'shouldReturnEmptyWhenNot200' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -44,6 +46,7 @@
'shouldReturnEmptyWhenNoFoundLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -61,6 +64,7 @@
'shouldReturnOnlyHomeWithNoValidLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -80,6 +84,7 @@
'shouldReturnValidLinksAmongInvalidLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -102,6 +107,7 @@
'shouldReturnOnlyHomeWithExternalLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -121,6 +127,7 @@
'shouldReturnValidLinksAmongExternalLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -143,6 +150,7 @@
'shouldReturnLinksWithoutDuplicate' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -166,6 +174,7 @@
'shouldReturnLinksWithRelativeUrl' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -189,6 +198,7 @@
'shouldReturnTenLinksPlusHome' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand Down Expand Up @@ -218,6 +228,7 @@
'shouldReturnTenLinksWithExternalLinksBeforeInternal' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand Down Expand Up @@ -247,6 +258,7 @@
'shouldReturnLinksWithoutRSSAndRestAPILink' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'timeout' => 60,
Expand All @@ -270,5 +282,28 @@
'https://example.org/rebecca-brown-he-came-to-set-the-captives-free',
'https://example.org',
],
'shouldReturnLinksWithMobileHeader' => [
'config' => [
'license_expired' => false,
'device' => 'mobile',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
'timeout' => 60,
],
'found_link' => true,
'response' => [
'body' => $html_valid_links_among_invalid_links,
'response' => [
'code' => 200,
],
],
],
'expected' => [
'https://example.org/hello-world',
'https://example.org/another-day',
'https://example.org/rich-dad-poor-dad',
'https://example.org',
],
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use Brain\Monkey\{Filters, Functions};
use Mockery;
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Media\AboveTheFold\WarmUp\APIClient;
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\License\API\User;
use WP_Rocket\Engine\Media\AboveTheFold\WarmUp\Controller;
use WP_Rocket\Engine\Media\AboveTheFold\WarmUp\{APIClient, Controller};
use WP_Rocket\Tests\Unit\TestCase;

/**
Expand All @@ -24,10 +23,10 @@ class Test_FetchLinks extends TestCase {
protected function setUp(): void {
parent::setUp();

$context = Mockery::mock( ContextInterface::class );
$options = Mockery::mock( Options_Data::class );
$api_client = Mockery::mock( APIClient::class );
$this->user = Mockery::mock( User::class );
$context = Mockery::mock( ContextInterface::class );
$options = Mockery::mock( Options_Data::class );
$api_client = Mockery::mock( APIClient::class );
$this->user = Mockery::mock( User::class );
$this->controller = new Controller( $context, $options, $api_client, $this->user );
}

Expand All @@ -43,9 +42,11 @@ public function testShouldReturnExpected( $config, $expected ) {
->once()
->andReturn( $config['license_expired'] );

Functions\when( 'home_url' )->alias( function( $link = '' ) {
return '' === $link ? 'https://example.org' : 'https://example.org' . $link;
} );
Functions\when( 'home_url' )->alias(
function ( $link = '' ) {
return '' === $link ? 'https://example.org' : 'https://example.org' . $link;
}
);

Functions\expect( 'wp_remote_get' )
->atMost()
Expand All @@ -67,12 +68,13 @@ public function testShouldReturnExpected( $config, $expected ) {
}

if ( isset( $config['found_link'] ) && $config['found_link'] ) {

$this->stubWpParseUrl();

Functions\when( 'wp_http_validate_url' )->alias( function( $link ) {
return false !== strpos( $link, 'https' ) ? $link : false;
} );
Functions\when( 'wp_http_validate_url' )->alias(
function ( $link ) {
return false !== strpos( $link, 'https' ) ? $link : false;
}
);

Filters\expectApplied( 'rocket_atf_warmup_links_number' )
->once()
Expand All @@ -81,7 +83,7 @@ public function testShouldReturnExpected( $config, $expected ) {

$this->assertSame(
$expected,
$this->controller->fetch_links()
$this->controller->fetch_links( $config['device'] )
);
}
}

0 comments on commit 74186d3

Please sign in to comment.