Skip to content

Commit

Permalink
Added Prerender as bot
Browse files Browse the repository at this point in the history
  • Loading branch information
hisorange committed May 20, 2022
1 parent b7ffc45 commit a9adacf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Change in 4.5.1

---

- Added Prerender as bot

### Change in 4.5.0

---
Expand Down
38 changes: 27 additions & 11 deletions src/Stages/BrowserDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -61,18 +77,18 @@ 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'),
]
)
)
);

$payload->setValue('browserName', trim(
$payload->getValue('browserFamily') .
' ' .
$payload->getValue('browserVersion')
' ' .
$payload->getValue('browserVersion')
));

// Human readable platform version.
Expand All @@ -82,18 +98,18 @@ 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'),
]
)
)
);

$payload->setValue('platformName', trim(
$payload->getValue('platformFamily') .
' ' .
$payload->getValue('platformVersion')
' ' .
$payload->getValue('platformVersion')
));

// Popular os vendors.
Expand Down
38 changes: 38 additions & 0 deletions tests/Stages/BrowserDetectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit a9adacf

Please sign in to comment.