Skip to content

renoki-co/laravel-prerender

Repository files navigation

Laravel Clusteer Prerender

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Prerender Laravel pages using Clusteer and this nice package.

🤝 Supporting

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. 📦

🚀 Installation

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

🙌 Usage

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');
    }
}

Prerendering Technique

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();
});

Mutating the Clusteer request

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']);
});

🐛 Testing

vendor/bin/phpunit

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

🎉 Credits

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages