Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #6613 Improve LCP warmup process #6615

Merged
merged 8 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions inc/Engine/Common/JobManager/APIHandler/APIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,28 @@ public function add_to_queue( string $url, array $options ): array {
*/
$url = apply_filters( 'rocket_saas_api_queued_url', $url );

$blocking = true;

if ( isset( $options['blocking'] ) ) {
$blocking = $options['blocking'];

unset( $options['blocking'] );
}
wordpressfan marked this conversation as resolved.
Show resolved Hide resolved

$args = [
'body' => [
'body' => [
'url' => $url,
'config' => $options,
],
'timeout' => 5,
'timeout' => 5,
'blocking' => true,
];

if ( ! $blocking ) {
$args['blocking'] = false;
$args['timeout'] = 0.01;
}

$this->logger::debug(
'Add to queue request arguments',
$args
Expand Down Expand Up @@ -86,7 +100,7 @@ public function add_to_queue( string $url, array $options ): array {
$result = json_decode( $this->response_body, true );

$this->logger::debug(
'Add to queue response body',
$url . ' - Add to queue response body',
$result
);

Expand Down
1 change: 1 addition & 0 deletions inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function add_to_atf_queue( string $url, $device = 'desktop' ): array {
'optimization_list' => '',
'is_home' => $is_home,
'is_mobile' => 'mobile' === $device,
'blocking' => rocket_get_constant( 'WP_ROCKET_DEBUG', false ),
];

return $this->add_to_queue( $url, $config );
Expand Down
37 changes: 20 additions & 17 deletions inc/Engine/Media/AboveTheFold/WarmUp/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,20 @@
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( $device = 'desktop' ): array {
public function fetch_links(): array {

Check warning on line 78 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#L78

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

Check warning on line 78 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#L78

The method fetch_links() has an NPath complexity of 384. 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';
}
$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 = [
Expand Down Expand Up @@ -180,27 +170,40 @@
/**
* Send fetched links to SaaS to do the warmup.
*
* @param array $links Array of links to be sent.
* @param string $device Device type.
* @param array $links Array of links to be sent.
*
* @return void
*/
private function send_to_saas( $links, $device = 'desktop' ) {
private function send_to_saas( $links ) {
if ( empty( $links ) ) {
return;
}

$default_delay = 5000;

if ( rocket_get_constant( 'WP_ROCKET_DEBUG' ) ) {
$default_delay = 500000;
}

/**
* Filter the delay between each request.
*
* @param int $delay_between the defined delay.
*
* @returns int
*/
$delay_between = (int) apply_filters( 'rocket_delay_between_requests', 500000 );
$delay_between = (int) apply_filters( 'rocket_lcp_warmup_delay_between_requests', $default_delay );

if ( ! is_int( $delay_between ) || $delay_between < 0 ) {
$delay_between = $default_delay;
}

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

if ( $this->is_mobile() ) {
$this->api_client->add_to_atf_queue( $link, 'mobile' );
}

usleep( $delay_between );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
'shouldReturnEmptyWhenLicenseExpired' => [
'config' => [
'license_expired' => true,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
],
'response' => [
Expand All @@ -30,9 +29,8 @@
'shouldReturnEmptyWhenNot200' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
],
'response' => [
Expand All @@ -46,9 +44,8 @@
'shouldReturnEmptyWhenNoFoundLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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' => false,
Expand All @@ -64,9 +61,8 @@
'shouldReturnOnlyHomeWithNoValidLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand All @@ -84,9 +80,8 @@
'shouldReturnValidLinksAmongInvalidLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand All @@ -107,9 +102,8 @@
'shouldReturnOnlyHomeWithExternalLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand All @@ -127,9 +121,8 @@
'shouldReturnValidLinksAmongExternalLinks' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand All @@ -150,9 +143,8 @@
'shouldReturnLinksWithoutDuplicate' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand All @@ -174,9 +166,8 @@
'shouldReturnLinksWithRelativeUrl' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand All @@ -198,9 +189,8 @@
'shouldReturnTenLinksPlusHome' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand Down Expand Up @@ -228,9 +218,8 @@
'shouldReturnTenLinksWithExternalLinksBeforeInternal' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand Down Expand Up @@ -258,9 +247,8 @@
'shouldReturnLinksWithoutRSSAndRestAPILink' => [
'config' => [
'license_expired' => false,
'device' => 'desktop',
'headers' => [
'user-agent' => 'WP Rocket/Pre-fetch Home Links',
'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,
Expand All @@ -282,28 +270,5 @@
'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 @@ -83,7 +83,7 @@ function ( $link ) {

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