Prerender Laravel pages using Clusteer and this nice package.
If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦
You can install the package via composer:
composer require renoki-co/laravel-prerender
This package leverages Clusteer, a Puppeteer wrapper written for PHP which is way faster than traditional packages by running a long-lived process that can execute Puppeteer commands by opening new pages instead of opening new browsers for each website.
Clusteer is already installed when you will be installing Laravel Prerender, all you need to do is to read the installation guide for Clusteer
After you have configured Clusteer, you may start the server with:
php artisan clusteer:serve
Laravel Prerender comes with a middleware you can attach to your web routes.
use App\Http\Controller;
use RenokiCo\LaravelPrerender\Middleware\ShouldPrerender;
class Dashboard extends Controller
{
public function __construct()
{
$this->middleware([
ShouldPrerender::class,
]);
}
public function index()
{
return view('welcome');
}
}
The default prerendering instructions are the following:
use Illuminate\Http\Request;
use Jenssegers\Agent\Facades\Agent;
use RenokiCo\LaravelPrerender\Prerender;
Prerender::shouldPrerender(function (Request $request) {
// Avoid infinite loop by excluding Clusteerbot/3.0 from prerendering,
// because Clusteer is mimicking the browser.
if ($request->isFromClusteerBot()) {
return false;
}
if (! is_null($request->query('_escaped_fragment_'))) {
return true;
}
if (Agent::isRobot() || $request->hasHeader('X-BUFFERBOT')) {
return true;
}
return false;
});
You may overwrite the prerender technique in your AppServiceProvider
's boot()
method. Make sure to also avoid prerendering in case the User-Agent
header is Clusteerbot/3.0
. In the future, this header might change according to the package updates, but they will be marked as breaking changes.
use Illuminate\Http\Request;
use RenokiCo\LaravelPrerender\Prerender;
Prerender::shouldPrerender(function (Request $request) {
// Avoid infinite loop by excluding Clusteerbot/3.0 from prerendering,
// because Clusteer is mimicking the browser.
if ($request->isFromClusteerBot()) {
return false;
}
return $request->isGoogleBot();
});
The prerendering will be made using Clusteer's built-in PHP functions to get the rendered HTML. In some cases, you might want to modify the Clusteer's state with additional configs.
The $clusteer
object is already initialized like below. You are free to append your own methods via the mutateClusteerOnRequest
method in your AppServiceProvider
's boot
method:
Clusteer::to($request->fullUrl())
->waitUntilAllRequestsFinish()
->setUserAgent('Clusteerbot/3.0')
->withHtml();
use RenokiCo\LaravelPrerender\Prerender;
Prerender::mutateClusteerOnRequest(function (Clusteer $clusteer, Request $request) {
return $clusteer->blockExtensions(['.css']);
});
vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.