Skip to content

Commit

Permalink
add lighthouse to crawlers
Browse files Browse the repository at this point in the history
  • Loading branch information
vlados committed Aug 19, 2022
1 parent f02ceba commit 1f74bfa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
9 changes: 2 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": "^8.1",
"illuminate/contracts": "^9.0",
"jaybizzle/crawler-detect": "^1.2",
"spatie/invade": "^1.1",
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
Expand All @@ -44,13 +45,7 @@
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint",
"post-autoload-dump": [
"@php artisan view:clear"
],
"post-update-cmd": [
"@php artisan view:clear"
]
"format": "vendor/bin/pint"
},
"config": {
"sort-packages": true,
Expand Down
25 changes: 23 additions & 2 deletions src/LaravelBladeCrawlerDetectServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Support\Facades\Blade;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers;
use Jaybizzle\CrawlerDetect\Fixtures\Exclusions;
use Jaybizzle\CrawlerDetect\Fixtures\Headers;
use Jaybizzle\LaravelCrawlerDetect\Facades\LaravelCrawlerDetect;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand All @@ -19,8 +22,26 @@ public function packageRegistered()

public function packageBooted()
{
Blade::if('user', function () {
return !app(CrawlerDetect::class)->isCrawler();
$crawlerDetect = app(CrawlerDetect::class);

$crawlers = new Crawlers();
$exclusions = new Exclusions();

$crawlerList = $crawlers->getAll();
$crawlerList[] = 'Chrome-Lighthouse';
$crawlerList[] = 'Google Page Speed';
$compiledRegex = $crawlerDetect->compileRegex($crawlerList);
$compiledExclusions = $crawlerDetect->compileRegex($exclusions->getAll());

$agent = trim(preg_replace(
"/{$compiledExclusions}/i",
'',
invade($crawlerDetect)->userAgent ?: ''
));


Blade::if('user', function () use ($agent, $compiledRegex) {
return (bool) !preg_match("/{$compiledRegex}/i", $agent);
});
}

Expand Down

0 comments on commit 1f74bfa

Please sign in to comment.