diff --git a/CHANGELOG.md b/CHANGELOG.md index c70d339..5be3bc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +### Change in 4.5.1 + +--- + +- Added Prerender as bot + ### Change in 4.5.0 --- diff --git a/src/Stages/BrowserDetect.php b/src/Stages/BrowserDetect.php index b811d4b..6b0d0a8 100644 --- a/src/Stages/BrowserDetect.php +++ b/src/Stages/BrowserDetect.php @@ -21,7 +21,7 @@ class BrowserDetect implements StageInterface public function __invoke(PayloadInterface $payload): ResultInterface { // Fix issue when the device is detected at tablet and mobile in the same time. - if (! $payload->getValue('isMobile') && ! $payload->getValue('isTablet')) { + if (!$payload->getValue('isMobile') && !$payload->getValue('isTablet')) { $payload->setValue('isMobile', false); $payload->setValue('isTablet', false); $payload->setValue('isDesktop', true); @@ -35,6 +35,22 @@ public function __invoke(PayloadInterface $payload): ResultInterface $payload->setValue('isDesktop', false); } + // Prerender desktop bot checker + if (strpos($payload->getValue('userAgent'), 'Prerender') !== false) { + $payload->setValue('isBot', true); + $payload->setValue('isMobile', false); + $payload->setValue('isTable', false); + $payload->setValue('isDesktop', true); + } + + // Prerender mobile bot checker + if (stripos($payload->getValue('userAgent'), 'Prerender') !== false && stripos($payload->getValue('userAgent'), 'Android') !== false) { + $payload->setValue('isBot', true); + $payload->setValue('isMobile', true); + $payload->setValue('isTable', false); + $payload->setValue('isDesktop', false); + } + // Popular browser vendors. if (false !== stripos($payload->getValue('browserFamily') ?? '', 'chrom')) { $payload->setValue('isChrome', true); @@ -61,9 +77,9 @@ public function __invoke(PayloadInterface $payload): ResultInterface implode( '.', [ - $payload->getValue('browserVersionMajor'), - $payload->getValue('browserVersionMinor'), - $payload->getValue('browserVersionPatch'), + $payload->getValue('browserVersionMajor'), + $payload->getValue('browserVersionMinor'), + $payload->getValue('browserVersionPatch'), ] ) ) @@ -71,8 +87,8 @@ public function __invoke(PayloadInterface $payload): ResultInterface $payload->setValue('browserName', trim( $payload->getValue('browserFamily') . - ' ' . - $payload->getValue('browserVersion') + ' ' . + $payload->getValue('browserVersion') )); // Human readable platform version. @@ -82,9 +98,9 @@ public function __invoke(PayloadInterface $payload): ResultInterface implode( '.', [ - $payload->getValue('platformVersionMajor'), - $payload->getValue('platformVersionMinor'), - $payload->getValue('platformVersionPatch'), + $payload->getValue('platformVersionMajor'), + $payload->getValue('platformVersionMinor'), + $payload->getValue('platformVersionPatch'), ] ) ) @@ -92,8 +108,8 @@ public function __invoke(PayloadInterface $payload): ResultInterface $payload->setValue('platformName', trim( $payload->getValue('platformFamily') . - ' ' . - $payload->getValue('platformVersion') + ' ' . + $payload->getValue('platformVersion') )); // Popular os vendors. diff --git a/tests/Stages/BrowserDetectTest.php b/tests/Stages/BrowserDetectTest.php index 20f83b2..89267cd 100644 --- a/tests/Stages/BrowserDetectTest.php +++ b/tests/Stages/BrowserDetectTest.php @@ -46,6 +46,44 @@ public function testInvoke($scenario, $expectations) } } + /** + * Check if the Prerender agents are recognized as desktop bot + * + * @return void + */ + public function testPrerenderBot() + { + $stage = new BrowserDetect; + $payload = new Payload('Unknown'); + + $payload->setValue('userAgent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/W.X.Y.Z Safari/537.36 Prerender (+https://github.com/prerender/prerender)'); + $result = $stage($payload); + + $this->assertTrue($result->isBot()); + $this->assertFalse($result->isMobile()); + $this->assertFalse($result->isTablet()); + $this->assertTrue($result->isDesktop()); + } + + /** + * Check if the Prerender agents are recognized as desktop bot + * + * @return void + */ + public function testPrerenderMobileBot() + { + $stage = new BrowserDetect; + $payload = new Payload('Unknown'); + + $payload->setValue('userAgent', 'Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 Prerender (+https://github.com/prerender/prerender)'); + $result = $stage($payload); + + $this->assertTrue($result->isBot()); + $this->assertTrue($result->isMobile()); + $this->assertFalse($result->isTablet()); + $this->assertFalse($result->isDesktop()); + } + /** * Check for inApp browsers. *