diff --git a/inc/Engine/Common/JobManager/APIHandler/APIClient.php b/inc/Engine/Common/JobManager/APIHandler/APIClient.php index afcfdaff97..920440f594 100644 --- a/inc/Engine/Common/JobManager/APIHandler/APIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/APIClient.php @@ -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'] ); + } + $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 @@ -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 ); diff --git a/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php b/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php index 22814b11b6..42802af987 100644 --- a/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php +++ b/inc/Engine/Media/AboveTheFold/WarmUp/APIClient.php @@ -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 ); diff --git a/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php b/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php index 0cdff3bcfe..50b1a3758f 100644 --- a/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php +++ b/inc/Engine/Media/AboveTheFold/WarmUp/Controller.php @@ -67,30 +67,20 @@ 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( $device = 'desktop' ): array { + public function fetch_links(): array { 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 = [ @@ -180,16 +170,21 @@ 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 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. * @@ -197,10 +192,18 @@ private function send_to_saas( $links, $device = 'desktop' ) { * * @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 ); } diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php index 3709383b8e..679d54528a 100644 --- a/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php @@ -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' => [ @@ -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' => [ @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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', - ], - ], ], ]; diff --git a/tests/Unit/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php b/tests/Unit/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php index ac4ef1d8a6..32445c6a90 100644 --- a/tests/Unit/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php +++ b/tests/Unit/inc/Engine/Media/AboveTheFold/WarmUp/Controller/fetchLinks.php @@ -83,7 +83,7 @@ function ( $link ) { $this->assertSame( $expected, - $this->controller->fetch_links( $config['device'] ) + $this->controller->fetch_links() ); } }