sandulat/laratron

Laravel-Rendertron SSR Middleware

v0.1 2019-07-08 10:54 UTC

This package is auto-updated.

Last update: 2024-04-08 22:36:03 UTC


README

🖼Laravel SSR using Rendertron 🖼

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f73616e64756c61742f6c61726174726f6e2e737667 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f73616e64756c61742f6c61726174726f6e2e737667 68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f73616e64756c61742e7376673f7374796c653d736f6369616c

About

Since we know that not all crawlers can render Javascript, we have to implement SSR (Server-Side Rendering). Implementing SSR in Laravel may be a headache and the easiest solution could be Rendertron.

Laratron is a tiny middleware for your Laravel app that detects whether the visitor is a crawler and passes the request to Rendertron.

About Rendertron (from official Readme):

Rendertron is designed to enable your Progressive Web App (PWA) to serve the correct content to any bot that doesn't render or execute JavaScript. Rendertron runs as a standalone HTTP server. Rendertron renders requested pages using Headless Chrome, auto-detecting when your PWA has completed loading and serializes the response back to the original request. To use Rendertron, your application configures middleware to determine whether to proxy a request to Rendertron. Rendertron is compatible with all client side technologies, including web components.

Installation

First of all we need to install Rendertron. Please consult the official documentation for more info. To install Laratron run this inside your project:

composer require sandulat/laratron

Configuration

Laratron exposes only one simple option, the URL of Rendertron, which can be set in your env file:

RENDERTRON_URL=http://localhost:3000

Usage

After installation you can use the middleware laratron on any route that you'd like to be server-side rendered.

Route::get('/', function () {
    return view('home');
})->middleware('laratron');

Or you can apply it on the entire web middleware group in App\Http\Kernel:

'web' => [
    // ...
    \Sandulat\Laratron\Http\Middleware\LaratronMiddleware::class,
],

Credits

Created by Stratulat Alexandru.

68747470733a2f2f636f6c746f72617070732e636f6d2f696d616765732f6c6f676f5f7472616e73706172656e742e706e67