Download the PHP package estey/laravel-react-middleware without Composer
On this page you can find all versions of the php package estey/laravel-react-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download estey/laravel-react-middleware
More information about estey/laravel-react-middleware
Files in estey/laravel-react-middleware
Package laravel-react-middleware
Short Description Middleware for React.js server-side rendering in Laravel.
License MIT
Homepage http://www.bradestey.com/projects/laravel-react-middleware
Informations about the package laravel-react-middleware
Laravel React Compiler Middleware
This is a Laravel middleware used to help with React Server-side Rendering. This middleware makes an HTTP request to a local Node.js server compiling the React application. It then injects the returned HTML into your Laravel view. Use Blade Templates as you would normally.
- How it Works
- Installation
- Configuration
- Merge JSON Response
How it Works
First install Node.js on your production server or on a server on the same local network. Then write a small Node.js application to render your React application. For simple applications, you can use ReactDOM.server.renderToString(App)
, for more complex React applications you may need to do something more like this.
On the Laravel side, build a Blade or plain PHP layout file as usual.
Render your views in your controller method as usual.
Add this middleware to whichever routes contain a React app.
From there, the React middleware will make a request to your Node.js compiler server and replace in the $content
variable with the returned HTML.
Installation
Install this package via Composer.
Add the service provider to your config/app.php
file.
Add the middleware class to your app/Http/Kernel.php
file.
Configuration
To publish the configuration file, run:
In the config file you can change the host and port of the compiler server and change the connection and response timeout times. The default behavior is for timeouts set be to 0
, which wait indefinitely. I highly recommend that you change these to be more aggressive.
AJAX requests to this route will return the data array passed to your view as plain JSON. So the example above would return:
To disable the JSON response on AJAX requests, pass "disable_json" to the first parameter.
Merge JSON Response
If your Node.js application responds with JSON instead of a plain HTML string, then the JSON will be parsed and merged into your view. So for example, if your Node.js application responds with:
Then, both the $content
variable and the $metaKeywords
variable will be accessible in your view file.